From 490f9406c3eb0f3be9d157aaf20b487b627ce589 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Mon, 21 Sep 2015 00:15:05 +0700 Subject: [PATCH] added durability fix --- src/main/java/fr/xephi/authme/Utils.java | 16 ++++++++++ .../authme/listener/AuthMeBlockListener.java | 7 ++++- .../authme/listener/AuthMeEntityListener.java | 29 +++++++++++++++++-- .../authme/listener/AuthMePlayerListener.java | 29 +++++++++++++++++-- 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/xephi/authme/Utils.java b/src/main/java/fr/xephi/authme/Utils.java index bdd156ff2..be103fa84 100644 --- a/src/main/java/fr/xephi/authme/Utils.java +++ b/src/main/java/fr/xephi/authme/Utils.java @@ -12,6 +12,7 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import java.io.File; import java.io.FileOutputStream; @@ -290,4 +291,19 @@ public class Utils { return false; } } + + public static void fixDurability(final ItemStack item) { + if (item == null || item.getType().getMaxDurability() == 0) + return; + final short old = item.getDurability(); + plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() { + @Override + public void run() { + int diff = old - item.getDurability(); + if (diff != 0) { + item.setDurability((short) (item.getDurability() + diff)); + } + } + }, 1); + } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java index ca5f68d31..58a056830 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java @@ -2,10 +2,12 @@ package fr.xephi.authme.listener; import fr.xephi.authme.AuthMe; import fr.xephi.authme.Utils; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.inventory.ItemStack; public class AuthMeBlockListener implements Listener { @@ -25,8 +27,11 @@ public class AuthMeBlockListener implements Listener { @EventHandler(ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent event) { - if (Utils.checkAuth(event.getPlayer())) + Player player = event.getPlayer(); + if (player == null || Utils.checkAuth(player)) { return; + } + Utils.fixDurability(player.getItemInHand()); event.setCancelled(true); } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java index 7ace12c93..ae8385881 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java @@ -8,7 +8,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.*; -import org.bukkit.event.player.PlayerEggThrowEvent; +import org.bukkit.inventory.ItemStack; public class AuthMeEntityListener implements Listener { @@ -29,7 +29,11 @@ public class AuthMeEntityListener implements Listener { if (Utils.checkAuth(player)) { return; } - + for (ItemStack item : player.getInventory().getArmorContents()) { + if (item != null) { + Utils.fixDurability(item); + } + } player.setFireTicks(0); event.setDamage(0.0); event.setCancelled(true); @@ -57,10 +61,12 @@ public class AuthMeEntityListener implements Listener { return; } - if (Utils.checkAuth((Player) entity)) { + Player player = (Player) entity; + if (Utils.checkAuth(player)) { return; } + Utils.fixDurability(player.getItemInHand()); event.setCancelled(true); } @@ -134,4 +140,21 @@ public class AuthMeEntityListener implements Listener { event.setCancelled(true); } + + @EventHandler + public void onShoot(EntityShootBowEvent event) { + Entity entity = event.getEntity(); + if (entity == null || !(entity instanceof Player)) { + return; + } + + Player player = (Player) entity; + if (Utils.checkAuth(player)) { + return; + } + + Utils.fixDurability(player.getItemInHand()); + event.setCancelled(true); + } + } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index b7723522e..ccb11a286 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -20,6 +20,7 @@ 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.SignChangeEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.inventory.InventoryClickEvent; @@ -400,8 +401,14 @@ public class AuthMePlayerListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onPlayerInteract(PlayerInteractEvent event) { - if (Utils.checkAuth(event.getPlayer())) + Player player = event.getPlayer(); + if (player == null || Utils.checkAuth(player)) return; + + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + Utils.fixDurability(player.getItemInHand()); + } + event.setCancelled(true); } @@ -487,7 +494,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerRespawn(PlayerRespawnEvent event) { Player player = event.getPlayer(); - if (Utils.checkAuth(player)) + if (player == null || Utils.checkAuth(player)) return; String name = player.getName().toLowerCase(); Location spawn = plugin.getSpawnLocation(player); @@ -517,4 +524,22 @@ public class AuthMePlayerListener implements Listener { } event.setCancelled(true); } + + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerShear(PlayerShearEntityEvent event) { + Player player = event.getPlayer(); + if (player == null || Utils.checkAuth(player)) + return; + Utils.fixDurability(player.getItemInHand()); + event.setCancelled(true); + } + + @EventHandler + public void onPlayerFish(PlayerFishEvent event) { + Player player = event.getPlayer(); + if (player == null || Utils.checkAuth(player)) + return; + Utils.fixDurability(player.getItemInHand()); + event.setCancelled(true); + } }