Split event listener

This commit is contained in:
Shevchik 2013-12-15 18:33:38 +04:00
parent adde90b9eb
commit 03c683af03
3 changed files with 86 additions and 70 deletions

View File

@ -23,7 +23,6 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Arrow; import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -41,7 +40,6 @@ public class Chairs extends JavaPlugin {
public int sitEffectInterval; public int sitEffectInterval;
public HashSet<String> disabledRegions = new HashSet<String>(); public HashSet<String> disabledRegions = new HashSet<String>();
private Logger log; private Logger log;
public PluginManager pm;
public ChairsIgnoreList ignoreList; public ChairsIgnoreList ignoreList;
public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled; public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled;
@ -68,11 +66,11 @@ public class Chairs extends JavaPlugin {
} }
ignoreList = new ChairsIgnoreList(this); ignoreList = new ChairsIgnoreList(this);
ignoreList.load(); ignoreList.load();
pm = this.getServer().getPluginManager();
getConfig().options().copyDefaults(true); getConfig().options().copyDefaults(true);
saveConfig(); saveConfig();
loadConfig(); 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)); getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList));
if (sitEffectsEnabled) { if (sitEffectsEnabled) {
logInfo("Enabling sitting effects."); logInfo("Enabling sitting effects.");

View File

@ -1,8 +1,5 @@
package com.cnaude.chairs; package com.cnaude.chairs;
import java.util.HashSet;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -11,84 +8,23 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame; import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; 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.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.material.Stairs; import org.bukkit.material.Stairs;
import org.bukkit.material.Step; import org.bukkit.material.Step;
import org.bukkit.material.WoodenStep; import org.bukkit.material.WoodenStep;
public class EventListener implements Listener { public class TrySitEventListener implements Listener {
public Chairs plugin; public Chairs plugin;
public ChairsIgnoreList ignoreList; public ChairsIgnoreList ignoreList;
public EventListener(Chairs plugin, ChairsIgnoreList ignoreList) { public TrySitEventListener(Chairs plugin, ChairsIgnoreList ignoreList) {
this.plugin = plugin; this.plugin = plugin;
this.ignoreList = ignoreList; 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<String> queueUnsit = new HashSet<String>();
@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 @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {

View File

@ -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<String> queueUnsit = new HashSet<String>();
@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);
}
}
}