|
| 1 | +package us.thezircon.play.autopickup.listeners; |
| 2 | + |
| 3 | +import org.bukkit.Bukkit; |
| 4 | +import org.bukkit.Location; |
| 5 | +import org.bukkit.entity.Item; |
| 6 | +import org.bukkit.entity.Player; |
| 7 | +import org.bukkit.event.EventHandler; |
| 8 | +import org.bukkit.event.Listener; |
| 9 | +import org.bukkit.event.player.PlayerFishEvent; |
| 10 | +import org.bukkit.inventory.ItemStack; |
| 11 | +import us.thezircon.play.autopickup.AutoPickup; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class PlayerFishEventListener implements Listener { |
| 17 | + |
| 18 | + private static final AutoPickup PLUGIN = AutoPickup.getPlugin(AutoPickup.class); |
| 19 | + |
| 20 | + @EventHandler |
| 21 | + public void onFish(PlayerFishEvent e) { |
| 22 | + Player player = e.getPlayer(); |
| 23 | + if(!(e.getState().equals(PlayerFishEvent.State.CAUGHT_FISH))) return; |
| 24 | + if(!(e.getCaught() instanceof Item)) return; |
| 25 | + |
| 26 | + if (!PLUGIN.autopickup_list_fishing.contains(player)) return; |
| 27 | + |
| 28 | + Bukkit.getScheduler().runTaskAsynchronously(PLUGIN, new Runnable() { |
| 29 | + @Override |
| 30 | + public void run() { |
| 31 | + boolean requirePermsAUTO = PLUGIN.getConfig().getBoolean("requirePerms.autopickup"); |
| 32 | + if (!requirePermsAUTO) { |
| 33 | + return; |
| 34 | + } |
| 35 | + if (!player.hasPermission("autopickup.pickup.fishing") && !player.hasPermission("autopickup.pickup.fishing.autoenabled")) { |
| 36 | + PLUGIN.autopickup_list_fishing.remove(player); |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + boolean doFullInvMSG = PLUGIN.getConfig().getBoolean("doFullInvMSG"); |
| 42 | + |
| 43 | + Location loc = e.getPlayer().getLocation(); |
| 44 | + if (AutoPickup.worldsBlacklist!=null && AutoPickup.worldsBlacklist.contains(loc.getWorld().getName())) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + Bukkit.getScheduler().runTask(PLUGIN, () -> { |
| 49 | + Item caught = (Item) e.getCaught(); |
| 50 | + |
| 51 | + if (PLUGIN.getBlacklistConf().contains("BlacklistedFishing", true)) { |
| 52 | + boolean doBlacklist = PLUGIN.getBlacklistConf().getBoolean("BlacklistedFishing"); |
| 53 | + List<String> blacklist = PLUGIN.getBlacklistConf().getStringList("BlacklistedFishing"); |
| 54 | + |
| 55 | + if (doBlacklist && blacklist.contains(caught.getItemStack().getType().toString())) { |
| 56 | + return; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + HashMap<Integer, ItemStack> leftOver = player.getInventory().addItem(caught.getItemStack()); |
| 61 | + caught.remove(); |
| 62 | + if (!leftOver.isEmpty()) { |
| 63 | + for (ItemStack item : leftOver.values()) { |
| 64 | + player.getWorld().dropItemNaturally(loc, item); |
| 65 | + } |
| 66 | + if (doFullInvMSG) { |
| 67 | + long secondsLeft; |
| 68 | + long cooldown = 15000; // 15 sec |
| 69 | + if (AutoPickup.lastInvFullNotification.containsKey(player.getUniqueId())) { |
| 70 | + secondsLeft = (AutoPickup.lastInvFullNotification.get(player.getUniqueId()) / 1000) + cooldown / 1000 - (System.currentTimeMillis() / 1000); |
| 71 | + } else { |
| 72 | + secondsLeft = 0; |
| 73 | + } |
| 74 | + if (secondsLeft <= 0) { |
| 75 | + player.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getFullInventory()); |
| 76 | + AutoPickup.lastInvFullNotification.put(player.getUniqueId(), System.currentTimeMillis()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments