|
| 1 | +package us.thezircon.play.autopickup.listeners; |
| 2 | + |
| 3 | +import org.bukkit.Bukkit; |
| 4 | +import org.bukkit.entity.Player; |
| 5 | +import org.bukkit.event.EventHandler; |
| 6 | +import org.bukkit.event.Listener; |
| 7 | +import org.bukkit.event.entity.EntityDropItemEvent; |
| 8 | +import org.bukkit.event.player.PlayerShearEntityEvent; |
| 9 | +import org.bukkit.inventory.ItemStack; |
| 10 | +import us.thezircon.play.autopickup.AutoPickup; |
| 11 | + |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Iterator; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +public class EntityDropItemEventListener implements Listener { |
| 17 | + |
| 18 | + private static final AutoPickup PLUGIN = AutoPickup.getPlugin(AutoPickup.class); |
| 19 | + |
| 20 | + private static HashMap<UUID, UUID> player_sheep_map = new HashMap<>(); |
| 21 | + |
| 22 | + @EventHandler |
| 23 | + public void onSheer(PlayerShearEntityEvent e) { |
| 24 | + player_sheep_map.put(e.getEntity().getUniqueId(), e.getPlayer().getUniqueId()); |
| 25 | + } |
| 26 | + |
| 27 | + @EventHandler |
| 28 | + public void onDrop(EntityDropItemEvent e) { |
| 29 | + |
| 30 | + boolean doFullInvMSG = PLUGIN.getConfig().getBoolean("doFullInvMSG"); |
| 31 | + |
| 32 | + if (AutoPickup.worldsBlacklist!=null && AutoPickup.worldsBlacklist.contains(e.getEntity().getWorld().getName())) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + UUID sheep = e.getEntity().getUniqueId(); |
| 37 | + if (player_sheep_map.containsKey(sheep)) { |
| 38 | + Player player = Bukkit.getPlayer(player_sheep_map.get(sheep)); |
| 39 | + if (!PLUGIN.autopickup_list.contains(player)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Drops |
| 44 | + ItemStack drops = e.getItemDrop().getItemStack(); |
| 45 | + e.getItemDrop().remove(); |
| 46 | + HashMap<Integer, ItemStack> leftOver = player.getInventory().addItem(drops); |
| 47 | + if (leftOver.keySet().size()>0) { |
| 48 | + for (ItemStack item : leftOver.values()) { |
| 49 | + player.getWorld().dropItemNaturally(e.getItemDrop().getLocation(), item); |
| 50 | + } |
| 51 | + if (doFullInvMSG) { |
| 52 | + long secondsLeft; |
| 53 | + long cooldown = 15000; // 15 sec |
| 54 | + if (AutoPickup.lastInvFullNotification.containsKey(player.getUniqueId())) { |
| 55 | + secondsLeft = (AutoPickup.lastInvFullNotification.get(player.getUniqueId())/1000)+ cooldown/1000 - (System.currentTimeMillis()/1000); |
| 56 | + } else { |
| 57 | + secondsLeft = 0; |
| 58 | + } |
| 59 | + if (secondsLeft<=0) { |
| 60 | + player.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getFullInventory()); |
| 61 | + AutoPickup.lastInvFullNotification.put(player.getUniqueId(), System.currentTimeMillis()); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Bukkit.getScheduler().runTaskAsynchronously(PLUGIN, new Runnable() { |
| 67 | + @Override |
| 68 | + public void run() { |
| 69 | + if (!player.hasPermission("autopickup.pickup.mined")) { |
| 70 | + PLUGIN.autopickup_list.remove(player); |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments