From 03c683af03cf5f247e1b407a79b032d60e29da44 Mon Sep 17 00:00:00 2001 From: Shevchik Date: Sun, 15 Dec 2013 18:33:38 +0400 Subject: [PATCH] Split event listener --- src/com/cnaude/chairs/Chairs.java | 6 +- ...Listener.java => TrySitEventListener.java} | 68 +-------------- .../cnaude/chairs/TryUnsitEventListener.java | 82 +++++++++++++++++++ 3 files changed, 86 insertions(+), 70 deletions(-) rename src/com/cnaude/chairs/{EventListener.java => TrySitEventListener.java} (84%) create mode 100644 src/com/cnaude/chairs/TryUnsitEventListener.java diff --git a/src/com/cnaude/chairs/Chairs.java b/src/com/cnaude/chairs/Chairs.java index 0685b90..60b389c 100644 --- a/src/com/cnaude/chairs/Chairs.java +++ b/src/com/cnaude/chairs/Chairs.java @@ -23,7 +23,6 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Arrow; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.Vector; @@ -41,7 +40,6 @@ public class Chairs extends JavaPlugin { public int sitEffectInterval; public HashSet disabledRegions = new HashSet(); private Logger log; - public PluginManager pm; public ChairsIgnoreList ignoreList; public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled; @@ -68,11 +66,11 @@ public class Chairs extends JavaPlugin { } ignoreList = new ChairsIgnoreList(this); ignoreList.load(); - pm = this.getServer().getPluginManager(); getConfig().options().copyDefaults(true); saveConfig(); loadConfig(); - getServer().getPluginManager().registerEvents(new EventListener(this, ignoreList), this); + getServer().getPluginManager().registerEvents(new TrySitEventListener(this, ignoreList), this); + getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList)); if (sitEffectsEnabled) { logInfo("Enabling sitting effects."); diff --git a/src/com/cnaude/chairs/EventListener.java b/src/com/cnaude/chairs/TrySitEventListener.java similarity index 84% rename from src/com/cnaude/chairs/EventListener.java rename to src/com/cnaude/chairs/TrySitEventListener.java index bf9aa88..0d22e9a 100644 --- a/src/com/cnaude/chairs/EventListener.java +++ b/src/com/cnaude/chairs/TrySitEventListener.java @@ -1,8 +1,5 @@ package com.cnaude.chairs; -import java.util.HashSet; - -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -11,84 +8,23 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.ItemFrame; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.vehicle.VehicleExitEvent; import org.bukkit.material.Stairs; import org.bukkit.material.Step; import org.bukkit.material.WoodenStep; -public class EventListener implements Listener { +public class TrySitEventListener implements Listener { public Chairs plugin; public ChairsIgnoreList ignoreList; - public EventListener(Chairs plugin, ChairsIgnoreList ignoreList) { + public TrySitEventListener(Chairs plugin, ChairsIgnoreList ignoreList) { this.plugin = plugin; this.ignoreList = ignoreList; } - @EventHandler(priority=EventPriority.LOWEST) - public void onPlayerQuit(PlayerQuitEvent event) - { - Player player = event.getPlayer(); - if (plugin.sit.containsKey(player.getName())) - { - plugin.unSitPlayer(player,true); - } - } - - @EventHandler(priority=EventPriority.LOWEST) - public void onPlayerDeath(PlayerDeathEvent event) - { - Player player = event.getEntity(); - if (plugin.sit.containsKey(player.getName())) - { - plugin.unSitPlayer(player,true); - } - } - - private HashSet queueUnsit = new HashSet(); - @EventHandler(priority=EventPriority.LOWEST) - public void onExitVehicle(VehicleExitEvent e) - { - if (e.getVehicle().getPassenger() instanceof Player) - { - final Player player = (Player) e.getVehicle().getPassenger(); - if (plugin.sit.containsKey(player.getName())) - { - e.setCancelled(true); - if (!queueUnsit.contains(player.getName())) - { - queueUnsit.add(player.getName()); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() - { - public void run() - { - queueUnsit.remove(player.getName()); - plugin.unSitPlayer(player, false); - } - }); - } - } - } - } - - @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true) - public void onBlockBreak(BlockBreakEvent event) - { - Block b = event.getBlock(); - if (plugin.sitblock.containsKey(b)) - { - Player player = Bukkit.getPlayerExact(plugin.sitblock.get(b)); - plugin.unSitPlayer(player,false); - } - } @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { diff --git a/src/com/cnaude/chairs/TryUnsitEventListener.java b/src/com/cnaude/chairs/TryUnsitEventListener.java new file mode 100644 index 0000000..0d03215 --- /dev/null +++ b/src/com/cnaude/chairs/TryUnsitEventListener.java @@ -0,0 +1,82 @@ +package com.cnaude.chairs; + +import java.util.HashSet; + +import org.bukkit.Bukkit; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.vehicle.VehicleExitEvent; + +public class TryUnsitEventListener implements Listener { + + public Chairs plugin; + + public TryUnsitEventListener(Chairs plugin) { + this.plugin = plugin; + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onPlayerQuit(PlayerQuitEvent event) + { + Player player = event.getPlayer(); + if (plugin.sit.containsKey(player.getName())) + { + plugin.unSitPlayer(player,true); + } + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onPlayerDeath(PlayerDeathEvent event) + { + Player player = event.getEntity(); + if (plugin.sit.containsKey(player.getName())) + { + plugin.unSitPlayer(player,true); + } + } + + private HashSet queueUnsit = new HashSet(); + @EventHandler(priority=EventPriority.LOWEST) + public void onExitVehicle(VehicleExitEvent e) + { + if (e.getVehicle().getPassenger() instanceof Player) + { + final Player player = (Player) e.getVehicle().getPassenger(); + if (plugin.sit.containsKey(player.getName())) + { + e.setCancelled(true); + if (!queueUnsit.contains(player.getName())) + { + queueUnsit.add(player.getName()); + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() + { + public void run() + { + queueUnsit.remove(player.getName()); + plugin.unSitPlayer(player, false); + } + }); + } + } + } + } + + @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true) + public void onBlockBreak(BlockBreakEvent event) + { + Block b = event.getBlock(); + if (plugin.sitblock.containsKey(b)) + { + Player player = Bukkit.getPlayerExact(plugin.sitblock.get(b)); + plugin.unSitPlayer(player,false); + } + } + + +}