From 7cc0d12ed61c0fd8a3932a8882c9a8dd9028b58c Mon Sep 17 00:00:00 2001 From: Shevchik Date: Thu, 15 Jan 2015 12:51:13 +0400 Subject: [PATCH] Force unsit player on teleport. Workaround spigot bug. Other improvements. --- src/com/cnaude/chairs/core/Chairs.java | 348 +++++++++--------- src/com/cnaude/chairs/core/PlayerSitData.java | 305 +++++++-------- .../listeners/TryUnsitEventListener.java | 139 ++++--- 3 files changed, 371 insertions(+), 421 deletions(-) diff --git a/src/com/cnaude/chairs/core/Chairs.java b/src/com/cnaude/chairs/core/Chairs.java index 96c8a34..185c470 100644 --- a/src/com/cnaude/chairs/core/Chairs.java +++ b/src/com/cnaude/chairs/core/Chairs.java @@ -1,174 +1,174 @@ -package com.cnaude.chairs.core; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; - -import com.cnaude.chairs.api.APIInit; -import com.cnaude.chairs.commands.ChairsCommand; -import com.cnaude.chairs.commands.ChairsIgnoreList; -import com.cnaude.chairs.listeners.NANLoginListener; -import com.cnaude.chairs.listeners.TrySitEventListener; -import com.cnaude.chairs.listeners.TryUnsitEventListener; -import com.cnaude.chairs.sitaddons.ChairEffects; -import com.cnaude.chairs.sitaddons.CommandRestrict; -import com.cnaude.chairs.vehiclearrow.NMSAccess; - -public class Chairs extends JavaPlugin { - - public ChairEffects chairEffects; - public List allowedBlocks; - public List validSigns; - public boolean autoRotate, signCheck, notifyplayer; - public boolean ignoreIfBlockInHand; - public double distance; - public HashSet disabledRegions = new HashSet(); - public int maxChairWidth; - public boolean sitHealEnabled; - public int sitMaxHealth; - public int sitHealthPerInterval; - public int sitHealInterval; - public boolean sitPickupEnabled; - public boolean sitDisableAllCommands = false; - public HashSet sitDisabledCommands = new HashSet(); - private Logger log; - public ChairsIgnoreList ignoreList; - public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled, msgCommandRestricted; - - - private PlayerSitData psitdata; - public PlayerSitData getPlayerSitData() { - return psitdata; - } - private NMSAccess nmsaccess = new NMSAccess(); - protected NMSAccess getNMSAccess() { - return nmsaccess; - } - - @Override - public void onEnable() { - log = this.getLogger(); - try { - nmsaccess.setupChairsArrow(); - } catch (Exception e) { - e.printStackTrace(); - getServer().getPluginManager().disablePlugin(this); - return; - } - chairEffects = new ChairEffects(this); - ignoreList = new ChairsIgnoreList(); - psitdata = new PlayerSitData(this); - getConfig().options().copyDefaults(true); - saveConfig(); - loadConfig(); - if (sitHealEnabled) { - chairEffects.startHealing(); - } - if (sitPickupEnabled) { - chairEffects.startPickup(); - } - getServer().getPluginManager().registerEvents(new NANLoginListener(), this); - getServer().getPluginManager().registerEvents(new TrySitEventListener(this, ignoreList), this); - getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); - getServer().getPluginManager().registerEvents(new CommandRestrict(this), this); - getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList)); - new APIInit().initAPI(getPlayerSitData()); - } - - @Override - public void onDisable() { - if (psitdata != null) { - for (Player player : Utils.getOnlinePlayers()) { - if (psitdata.isSitting(player)) { - psitdata.unsitPlayerNow(player); - } - } - } - if (chairEffects != null) { - chairEffects.cancelHealing(); - chairEffects.cancelPickup(); - chairEffects = null; - } - log = null; - nmsaccess = null; - psitdata = null; - } - - public void loadConfig() { - FileConfiguration config = YamlConfiguration.loadConfiguration(new File(this.getDataFolder(),"config.yml")); - autoRotate = config.getBoolean("auto-rotate"); - signCheck = config.getBoolean("sign-check"); - distance = config.getDouble("distance"); - maxChairWidth = config.getInt("max-chair-width"); - notifyplayer = config.getBoolean("notify-player"); - ignoreIfBlockInHand = config.getBoolean("ignore-if-item-in-hand"); - - disabledRegions = new HashSet(config.getStringList("disabledWGRegions")); - - sitHealEnabled = config.getBoolean("sit-effects.healing.enabled", false); - sitHealInterval = config.getInt("sit-effects.healing.interval",20); - sitMaxHealth = config.getInt("sit-effects.healing.max-percent",100); - sitHealthPerInterval = config.getInt("sit-effects.healing.amount",1); - - sitPickupEnabled = config.getBoolean("sit-effects.itempickup.enabled", false); - - sitDisableAllCommands = config.getBoolean("sit-restrictions.commands.all"); - sitDisabledCommands = new HashSet(config.getStringList("sit-restrictions.commands.list")); - - msgSitting = ChatColor.translateAlternateColorCodes('&',config.getString("messages.sitting")); - msgStanding = ChatColor.translateAlternateColorCodes('&',config.getString("messages.standing")); - msgOccupied = ChatColor.translateAlternateColorCodes('&',config.getString("messages.occupied")); - msgNoPerm = ChatColor.translateAlternateColorCodes('&',config.getString("messages.no-permission")); - msgEnabled = ChatColor.translateAlternateColorCodes('&',config.getString("messages.enabled")); - msgDisabled = ChatColor.translateAlternateColorCodes('&',config.getString("messages.disabled")); - msgReloaded = ChatColor.translateAlternateColorCodes('&',config.getString("messages.reloaded")); - msgCommandRestricted = ChatColor.translateAlternateColorCodes('&',config.getString("messages.command-restricted")); - - allowedBlocks = new ArrayList(); - for (String s : config.getStringList("sit-blocks")) { - String type; - double sh = 0.7; - String tmp[] = s.split("[:]"); - type = tmp[0]; - if (tmp.length == 2) { - sh = Double.parseDouble(tmp[1]); - } - Material mat = Material.matchMaterial(type); - if (mat != null) { - logInfo("Allowed block: " + mat.toString() + " => " + sh); - allowedBlocks.add(new ChairBlock(mat,sh)); - } else { - logError("Invalid block: " + type); - } - } - - validSigns = new ArrayList(); - for (String type : config.getStringList("valid-signs")) { - try { - validSigns.add(Material.matchMaterial(type)); - } - catch (Exception e) { - logError(e.getMessage()); - } - } - } - - public void logInfo(String _message) { - log.log(Level.INFO, _message); - } - - public void logError(String _message) { - log.log(Level.SEVERE, _message); - } - -} +package com.cnaude.chairs.core; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import com.cnaude.chairs.api.APIInit; +import com.cnaude.chairs.commands.ChairsCommand; +import com.cnaude.chairs.commands.ChairsIgnoreList; +import com.cnaude.chairs.listeners.NANLoginListener; +import com.cnaude.chairs.listeners.TrySitEventListener; +import com.cnaude.chairs.listeners.TryUnsitEventListener; +import com.cnaude.chairs.sitaddons.ChairEffects; +import com.cnaude.chairs.sitaddons.CommandRestrict; +import com.cnaude.chairs.vehiclearrow.NMSAccess; + +public class Chairs extends JavaPlugin { + + public ChairEffects chairEffects; + public List allowedBlocks; + public List validSigns; + public boolean autoRotate, signCheck, notifyplayer; + public boolean ignoreIfBlockInHand; + public double distance; + public HashSet disabledRegions = new HashSet(); + public int maxChairWidth; + public boolean sitHealEnabled; + public int sitMaxHealth; + public int sitHealthPerInterval; + public int sitHealInterval; + public boolean sitPickupEnabled; + public boolean sitDisableAllCommands = false; + public HashSet sitDisabledCommands = new HashSet(); + private Logger log; + public ChairsIgnoreList ignoreList; + public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled, msgCommandRestricted; + + + private PlayerSitData psitdata; + public PlayerSitData getPlayerSitData() { + return psitdata; + } + private NMSAccess nmsaccess = new NMSAccess(); + protected NMSAccess getNMSAccess() { + return nmsaccess; + } + + @Override + public void onEnable() { + log = this.getLogger(); + try { + nmsaccess.setupChairsArrow(); + } catch (Exception e) { + e.printStackTrace(); + getServer().getPluginManager().disablePlugin(this); + return; + } + chairEffects = new ChairEffects(this); + ignoreList = new ChairsIgnoreList(); + psitdata = new PlayerSitData(this); + getConfig().options().copyDefaults(true); + saveConfig(); + loadConfig(); + if (sitHealEnabled) { + chairEffects.startHealing(); + } + if (sitPickupEnabled) { + chairEffects.startPickup(); + } + getServer().getPluginManager().registerEvents(new NANLoginListener(), this); + getServer().getPluginManager().registerEvents(new TrySitEventListener(this, ignoreList), this); + getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); + getServer().getPluginManager().registerEvents(new CommandRestrict(this), this); + getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList)); + new APIInit().initAPI(getPlayerSitData()); + } + + @Override + public void onDisable() { + if (psitdata != null) { + for (Player player : Utils.getOnlinePlayers()) { + if (psitdata.isSitting(player)) { + psitdata.unsitPlayerForce(player); + } + } + } + if (chairEffects != null) { + chairEffects.cancelHealing(); + chairEffects.cancelPickup(); + chairEffects = null; + } + log = null; + nmsaccess = null; + psitdata = null; + } + + public void loadConfig() { + FileConfiguration config = YamlConfiguration.loadConfiguration(new File(this.getDataFolder(),"config.yml")); + autoRotate = config.getBoolean("auto-rotate"); + signCheck = config.getBoolean("sign-check"); + distance = config.getDouble("distance"); + maxChairWidth = config.getInt("max-chair-width"); + notifyplayer = config.getBoolean("notify-player"); + ignoreIfBlockInHand = config.getBoolean("ignore-if-item-in-hand"); + + disabledRegions = new HashSet(config.getStringList("disabledWGRegions")); + + sitHealEnabled = config.getBoolean("sit-effects.healing.enabled", false); + sitHealInterval = config.getInt("sit-effects.healing.interval",20); + sitMaxHealth = config.getInt("sit-effects.healing.max-percent",100); + sitHealthPerInterval = config.getInt("sit-effects.healing.amount",1); + + sitPickupEnabled = config.getBoolean("sit-effects.itempickup.enabled", false); + + sitDisableAllCommands = config.getBoolean("sit-restrictions.commands.all"); + sitDisabledCommands = new HashSet(config.getStringList("sit-restrictions.commands.list")); + + msgSitting = ChatColor.translateAlternateColorCodes('&',config.getString("messages.sitting")); + msgStanding = ChatColor.translateAlternateColorCodes('&',config.getString("messages.standing")); + msgOccupied = ChatColor.translateAlternateColorCodes('&',config.getString("messages.occupied")); + msgNoPerm = ChatColor.translateAlternateColorCodes('&',config.getString("messages.no-permission")); + msgEnabled = ChatColor.translateAlternateColorCodes('&',config.getString("messages.enabled")); + msgDisabled = ChatColor.translateAlternateColorCodes('&',config.getString("messages.disabled")); + msgReloaded = ChatColor.translateAlternateColorCodes('&',config.getString("messages.reloaded")); + msgCommandRestricted = ChatColor.translateAlternateColorCodes('&',config.getString("messages.command-restricted")); + + allowedBlocks = new ArrayList(); + for (String s : config.getStringList("sit-blocks")) { + String type; + double sh = 0.7; + String tmp[] = s.split("[:]"); + type = tmp[0]; + if (tmp.length == 2) { + sh = Double.parseDouble(tmp[1]); + } + Material mat = Material.matchMaterial(type); + if (mat != null) { + logInfo("Allowed block: " + mat.toString() + " => " + sh); + allowedBlocks.add(new ChairBlock(mat,sh)); + } else { + logError("Invalid block: " + type); + } + } + + validSigns = new ArrayList(); + for (String type : config.getStringList("valid-signs")) { + try { + validSigns.add(Material.matchMaterial(type)); + } + catch (Exception e) { + logError(e.getMessage()); + } + } + } + + public void logInfo(String _message) { + log.log(Level.INFO, _message); + } + + public void logError(String _message) { + log.log(Level.SEVERE, _message); + } + +} diff --git a/src/com/cnaude/chairs/core/PlayerSitData.java b/src/com/cnaude/chairs/core/PlayerSitData.java index 4f20c4f..6f3b114 100644 --- a/src/com/cnaude/chairs/core/PlayerSitData.java +++ b/src/com/cnaude/chairs/core/PlayerSitData.java @@ -1,177 +1,128 @@ -package com.cnaude.chairs.core; - -import java.util.HashMap; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; - -import com.cnaude.chairs.api.PlayerChairSitEvent; -import com.cnaude.chairs.api.PlayerChairUnsitEvent; - -public class PlayerSitData { - - private Chairs plugin; - public PlayerSitData(Chairs plugin) { - this.plugin = plugin; - } - - private HashMap sit = new HashMap(); - private HashMap sitblock = new HashMap(); - - public boolean isSitting(Player player) { - return sit.containsKey(player) && sit.get(player).sitting; - } - - public boolean isBlockOccupied(Block block) { - return sitblock.containsKey(block); - } - - public Player getPlayerOnChair(Block chair) { - return sitblock.get(chair); - } - - public boolean sitPlayer(final Player player, Block blocktooccupy, Location sitlocation) { - PlayerChairSitEvent playersitevent = new PlayerChairSitEvent(player, sitlocation.clone()); - Bukkit.getPluginManager().callEvent(playersitevent); - if (playersitevent.isCancelled()) { - return false; - } - sitlocation = playersitevent.getSitLocation().clone(); - if (plugin.notifyplayer) { - player.sendMessage(plugin.msgSitting); - } - SitData sitdata = new SitData(); - Location arrowloc = sitlocation.getBlock().getLocation().add(0.5, 0 , 0.5); - Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc); - sitdata.arrow = arrow; - sitdata.teleportloc = player.getLocation(); - int task = Bukkit.getScheduler().scheduleSyncRepeatingTask( - plugin, - new Runnable() { - @Override - public void run() { - reSitPlayer(player); - } - }, - 1000, 1000 - ); - sitdata.resittask = task; - player.teleport(sitlocation); - arrow.setPassenger(player); - sitdata.sitting = true; - sit.put(player, sitdata); - sitblock.put(blocktooccupy, player); - return true; - } - - public void reSitPlayer(final Player player) { - SitData sitdata = sit.get(player); - sitdata.sitting = false; - final Entity prevarrow = sit.get(player).arrow; - player.eject(); - Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation()); - arrow.setPassenger(player); - sitdata.arrow = arrow; - sitdata.sitting = true; - Bukkit.getScheduler().scheduleSyncDelayedTask( - plugin, - new Runnable() { - @Override - public void run() { - prevarrow.remove(); - } - }, - 100 - ); - } - - public boolean unsitPlayerNormal(Player player) { - UnsitParams params = new UnsitParams(false, true, false); - return unsitPlayer(player, true, params); - } - - public void unsitPlayerForce(Player player) { - UnsitParams params = new UnsitParams(true, true, false); - unsitPlayer(player, false, params); - } - - public void unsitPlayerNow(Player player) { - UnsitParams params = new UnsitParams(true, false, true); - unsitPlayer(player, false, params); - } - - private boolean unsitPlayer(final Player player, boolean canCancel, UnsitParams params) { - SitData sitdata = sit.get(player); - final PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, sitdata.teleportloc.clone(), canCancel); - Bukkit.getPluginManager().callEvent(playerunsitevent); - if (playerunsitevent.isCancelled() && playerunsitevent.canBeCancelled()) { - return false; - } - sitdata.sitting = false; - if (params.eject()) { - player.eject(); - } - sitdata.arrow.remove(); - if (params.restorePostion()) { - Bukkit.getScheduler().scheduleSyncDelayedTask( - plugin, - new Runnable() { - @Override - public void run() { - player.teleport(playerunsitevent.getTeleportLocation().clone()); - player.setSneaking(false); - } - }, - 1 - ); - } else if (params.correctLeavePosition()) { - player.teleport(playerunsitevent.getTeleportLocation()); - } - sitblock.values().remove(player); - Bukkit.getScheduler().cancelTask(sitdata.resittask); - sit.remove(player); - if (plugin.notifyplayer) { - player.sendMessage(plugin.msgStanding); - } - return true; - } - - private class UnsitParams { - - private boolean eject; - private boolean restoreposition; - private boolean correctleaveposition; - - public UnsitParams(boolean eject, boolean restoreposition, boolean correctleaveposition) { - this.eject = eject; - this.restoreposition = restoreposition; - this.correctleaveposition = correctleaveposition; - } - - public boolean eject() { - return eject; - } - - public boolean restorePostion() { - return restoreposition; - } - - public boolean correctLeavePosition() { - return correctleaveposition; - } - - } - - private class SitData { - - private boolean sitting; - private Entity arrow; - private Location teleportloc; - private int resittask; - - } - -} +package com.cnaude.chairs.core; + +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; + +import com.cnaude.chairs.api.PlayerChairSitEvent; +import com.cnaude.chairs.api.PlayerChairUnsitEvent; + +public class PlayerSitData { + + private Chairs plugin; + public PlayerSitData(Chairs plugin) { + this.plugin = plugin; + } + + private HashMap sit = new HashMap(); + private HashMap sitblock = new HashMap(); + + public boolean isSitting(Player player) { + return sit.containsKey(player) && sit.get(player).sitting; + } + + public boolean isBlockOccupied(Block block) { + return sitblock.containsKey(block); + } + + public Player getPlayerOnChair(Block chair) { + return sitblock.get(chair); + } + + public boolean sitPlayer(final Player player, Block blocktooccupy, Location sitlocation) { + PlayerChairSitEvent playersitevent = new PlayerChairSitEvent(player, sitlocation.clone()); + Bukkit.getPluginManager().callEvent(playersitevent); + if (playersitevent.isCancelled()) { + return false; + } + sitlocation = playersitevent.getSitLocation().clone(); + if (plugin.notifyplayer) { + player.sendMessage(plugin.msgSitting); + } + SitData sitdata = new SitData(); + Location arrowloc = sitlocation.getBlock().getLocation().add(0.5, 0 , 0.5); + Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc); + sitdata.arrow = arrow; + sitdata.teleportloc = player.getLocation(); + int task = Bukkit.getScheduler().scheduleSyncRepeatingTask( + plugin, + new Runnable() { + @Override + public void run() { + reSitPlayer(player); + } + }, + 1000, 1000 + ); + sitdata.resittask = task; + player.teleport(sitlocation); + arrow.setPassenger(player); + sitdata.sitting = true; + sit.put(player, sitdata); + sitblock.put(blocktooccupy, player); + return true; + } + + public void reSitPlayer(final Player player) { + SitData sitdata = sit.get(player); + sitdata.sitting = false; + final Entity prevarrow = sit.get(player).arrow; + Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation()); + arrow.setPassenger(player); + sitdata.arrow = arrow; + sitdata.sitting = true; + Bukkit.getScheduler().scheduleSyncDelayedTask( + plugin, + new Runnable() { + @Override + public void run() { + prevarrow.remove(); + } + }, + 100 + ); + } + + public boolean unsitPlayer(Player player) { + return unsitPlayer(player, true); + } + + public void unsitPlayerForce(Player player) { + unsitPlayer(player, false); + } + + private boolean unsitPlayer(final Player player, boolean canCancel) { + SitData sitdata = sit.get(player); + final PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, sitdata.teleportloc.clone(), canCancel); + Bukkit.getPluginManager().callEvent(playerunsitevent); + if (playerunsitevent.isCancelled() && playerunsitevent.canBeCancelled()) { + return false; + } + sitdata.sitting = false; + player.leaveVehicle(); + sitdata.arrow.remove(); + player.teleport(playerunsitevent.getTeleportLocation().clone()); + player.setSneaking(false); + sitblock.values().remove(player); + Bukkit.getScheduler().cancelTask(sitdata.resittask); + sit.remove(player); + if (plugin.notifyplayer) { + player.sendMessage(plugin.msgStanding); + } + return true; + } + + private class SitData { + + private boolean sitting; + private Entity arrow; + private Location teleportloc; + private int resittask; + + } + +} diff --git a/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java b/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java index cd86893..465c903 100644 --- a/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java +++ b/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java @@ -1,70 +1,69 @@ -package com.cnaude.chairs.listeners; - -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.player.PlayerTeleportEvent; -import org.bukkit.event.vehicle.VehicleExitEvent; - -import com.cnaude.chairs.core.Chairs; - -public class TryUnsitEventListener implements Listener { - - public Chairs plugin; - - public TryUnsitEventListener(Chairs plugin) { - this.plugin = plugin; - } - - //spigot... - @EventHandler(priority=EventPriority.LOWEST) - public void onPlayerTeleport(PlayerTeleportEvent event) { - final Player player = event.getPlayer(); - if (plugin.getPlayerSitData().isSitting(player)) { - event.setCancelled(true); - } - } - - @EventHandler(priority=EventPriority.LOWEST) - public void onPlayerQuit(PlayerQuitEvent event) { - Player player = event.getPlayer(); - if (plugin.getPlayerSitData().isSitting(player)) { - plugin.getPlayerSitData().unsitPlayerNow(player); - } - } - - @EventHandler(priority=EventPriority.LOWEST) - public void onPlayerDeath(PlayerDeathEvent event) { - Player player = event.getEntity(); - if (plugin.getPlayerSitData().isSitting(player)) { - plugin.getPlayerSitData().unsitPlayerNow(player); - } - } - - @EventHandler(priority=EventPriority.LOWEST) - public void onExitVehicle(VehicleExitEvent e) { - if (e.getVehicle().getPassenger() instanceof Player) { - final Player player = (Player) e.getVehicle().getPassenger(); - if (plugin.getPlayerSitData().isSitting(player)) { - if (!plugin.getPlayerSitData().unsitPlayerNormal(player)) { - e.setCancelled(true); - } - } - } - } - - @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true) - public void onBlockBreak(BlockBreakEvent event) { - Block b = event.getBlock(); - if (plugin.getPlayerSitData().isBlockOccupied(b)) { - Player player = plugin.getPlayerSitData().getPlayerOnChair(b); - plugin.getPlayerSitData().unsitPlayerForce(player); - } - } - -} +package com.cnaude.chairs.listeners; + +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.player.PlayerTeleportEvent; +import org.bukkit.event.vehicle.VehicleExitEvent; + +import com.cnaude.chairs.core.Chairs; + +public class TryUnsitEventListener implements Listener { + + public Chairs plugin; + + public TryUnsitEventListener(Chairs plugin) { + this.plugin = plugin; + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onPlayerTeleport(PlayerTeleportEvent event) { + final Player player = event.getPlayer(); + if (plugin.getPlayerSitData().isSitting(player)) { + plugin.getPlayerSitData().unsitPlayerForce(player); + } + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onPlayerQuit(PlayerQuitEvent event) { + Player player = event.getPlayer(); + if (plugin.getPlayerSitData().isSitting(player)) { + plugin.getPlayerSitData().unsitPlayerForce(player); + } + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onPlayerDeath(PlayerDeathEvent event) { + Player player = event.getEntity(); + if (plugin.getPlayerSitData().isSitting(player)) { + plugin.getPlayerSitData().unsitPlayerForce(player); + } + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onExitVehicle(VehicleExitEvent e) { + if (e.getVehicle().getPassenger() instanceof Player) { + final Player player = (Player) e.getVehicle().getPassenger(); + if (plugin.getPlayerSitData().isSitting(player)) { + if (!plugin.getPlayerSitData().unsitPlayer(player)) { + e.setCancelled(true); + } + } + } + } + + @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true) + public void onBlockBreak(BlockBreakEvent event) { + Block b = event.getBlock(); + if (plugin.getPlayerSitData().isBlockOccupied(b)) { + Player player = plugin.getPlayerSitData().getPlayerOnChair(b); + plugin.getPlayerSitData().unsitPlayerForce(player); + } + } + +}