From 4539b3d2d3d4e15582adec552a792ad6aa319bcd Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Tue, 13 Apr 2021 15:55:18 +0200 Subject: [PATCH 1/8] Don't modify hoppers until they have been loaded (SD-6861) --- .../com/songoda/epichoppers/EpicHoppers.java | 4 +- .../epichoppers/handlers/TeleportHandler.java | 53 ++++++++++--------- .../epichoppers/hopper/HopperManager.java | 34 +++++++++--- .../epichoppers/listeners/BlockListeners.java | 13 ++++- .../listeners/HopperListeners.java | 9 ++-- .../listeners/InteractListeners.java | 9 +++- .../songoda/epichoppers/tasks/HopTask.java | 4 -- src/main/resources/en_US.lang | 1 + 8 files changed, 84 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/songoda/epichoppers/EpicHoppers.java b/src/main/java/com/songoda/epichoppers/EpicHoppers.java index 3af5c72..38cc8c8 100644 --- a/src/main/java/com/songoda/epichoppers/EpicHoppers.java +++ b/src/main/java/com/songoda/epichoppers/EpicHoppers.java @@ -58,7 +58,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - public class EpicHoppers extends SongodaPlugin { private static EpicHoppers INSTANCE; @@ -173,6 +172,8 @@ public class EpicHoppers extends SongodaPlugin { this.dataManager.getHoppers((hoppers) -> { this.hopperManager.addHoppers(hoppers.values()); this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts)); + + this.hopperManager.setReady(); }); } @@ -229,7 +230,6 @@ public class EpicHoppers extends SongodaPlugin { } else if (key.equals("AutoSmelting")) { modules.add(new ModuleAutoSmelter(this, levels.getInt("AutoSmelting"))); } - } levelManager.addLevel(level, costExperience, costEconomy, radius, amount, filter, teleport, linkAmount, modules); } diff --git a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java b/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java index c7e30fe..6ffbda4 100644 --- a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java +++ b/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java @@ -32,33 +32,35 @@ public class TeleportHandler { } private void teleportRunner() { - for (World world : Bukkit.getWorlds()) { - for (Entity entity : world.getEntities()) { - if (!(entity instanceof LivingEntity) || entity.getType() == EntityType.ARMOR_STAND) - continue; - - if (!this.plugin.getConfig().getBoolean("Main.Allow Players To Teleport Through Hoppers") - || (entity instanceof Player && !entity.hasPermission("EpicHoppers.Teleport"))) - continue; - - Location location = entity.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation(); - - if (!this.plugin.getHopperManager().isHopper(location)) - continue; - - Hopper hopper = this.plugin.getHopperManager().getHopper(location); - - if (hopper.getTeleportTrigger() != TeleportTrigger.WALK_ON) - continue; - - if (this.lastTeleports.containsKey(entity.getUniqueId())) { - long duration = (new Date()).getTime() - new Date(this.lastTeleports.get(entity.getUniqueId())).getTime(); - if (duration <= 5 * 1000) + if (plugin.getHopperManager().isReady()) { + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (!(entity instanceof LivingEntity) || entity.getType() == EntityType.ARMOR_STAND) continue; - } - this.tpEntity(entity, hopper); - this.lastTeleports.put(entity.getUniqueId(), System.currentTimeMillis()); + if (!this.plugin.getConfig().getBoolean("Main.Allow Players To Teleport Through Hoppers") + || (entity instanceof Player && !entity.hasPermission("EpicHoppers.Teleport"))) + continue; + + Location location = entity.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation(); + + if (!this.plugin.getHopperManager().isHopper(location)) + continue; + + Hopper hopper = this.plugin.getHopperManager().getHopper(location); + + if (hopper.getTeleportTrigger() != TeleportTrigger.WALK_ON) + continue; + + if (this.lastTeleports.containsKey(entity.getUniqueId())) { + long duration = (new Date()).getTime() - new Date(this.lastTeleports.get(entity.getUniqueId())).getTime(); + if (duration <= 5 * 1000) + continue; + } + + this.tpEntity(entity, hopper); + this.lastTeleports.put(entity.getUniqueId(), System.currentTimeMillis()); + } } } } @@ -77,6 +79,7 @@ public class TeleportHandler { * * @param lastHopper The previous hopper found in the chain * @param currentChainLength The current length of the chain, used to cap the search length + * * @return The hopper at the end of the chain (or up to 15 in depth) */ private Hopper getChain(Hopper lastHopper, int currentChainLength) { diff --git a/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java b/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java index 42a29dd..41ffa18 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java +++ b/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java @@ -13,9 +13,25 @@ import java.util.HashMap; import java.util.Map; public class HopperManager { + protected boolean ready; private final Map registeredHoppers = new HashMap<>(); + /** + * Sets {@link #isReady()} to {@code true}.
+ * Called by {@link EpicHoppers#onDataLoad()} + */ + public void setReady() { + this.ready = true; + } + + /** + * @return true, if all the data has been loaded from the DB + */ + public boolean isReady() { + return this.ready; + } + public Hopper addHopper(Hopper hopper) { registeredHoppers.put(roundLocation(hopper.getLocation()), hopper); return hopper; @@ -50,41 +66,47 @@ public class HopperManager { return removed; } - public Hopper getHopper(Location location) { if (!registeredHoppers.containsKey(location = roundLocation(location))) { + if (!this.ready) { + throw new IllegalStateException("Hoppers are still being loaded"); + } + Hopper hopper = addHopper(new Hopper(location)); EpicHoppers.getInstance().getDataManager().createHopper(hopper); } return registeredHoppers.get(location); } - public Hopper getHopper(Block block) { return getHopper(block.getLocation()); } - + /** + * Returns {@code false} if {@link #isReady()} is false too + */ public boolean isHopper(Location location) { return registeredHoppers.containsKey(roundLocation(location)); } - public Map getHoppers() { return Collections.unmodifiableMap(registeredHoppers); } - public Hopper getHopperFromPlayer(Player player) { + if (!this.ready) { + throw new IllegalStateException("Hoppers are still being loaded"); + } + for (Hopper hopper : registeredHoppers.values()) { if (hopper.getLastPlayerOpened() == player.getUniqueId()) { return hopper; } } + return null; } - private Location roundLocation(Location location) { location = location.clone(); location.setX(location.getBlockX()); diff --git a/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java b/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java index 90b24bc..e880258 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java @@ -17,7 +17,6 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.permissions.PermissionAttachmentInfo; - /** * Created by songoda on 3/14/2017. */ @@ -54,6 +53,12 @@ public class BlockListeners implements Listener { if (Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getLevelManager().isEpicHopper(item)) return; + if (!plugin.getHopperManager().isReady()) { + player.sendMessage(plugin.getLocale().getMessage("event.hopper.notready").getMessage()); + e.setCancelled(true); + return; + } + Hopper hopper = plugin.getHopperManager().addHopper( new HopperBuilder(e.getBlock()) .setLevel(plugin.getLevelManager().getLevel(item)) @@ -93,6 +98,12 @@ public class BlockListeners implements Listener { if (event.getBlock().getType() != Material.HOPPER) return; + if (!plugin.getHopperManager().isReady()) { + player.sendMessage(plugin.getLocale().getMessage("event.hopper.notready").getMessage()); + event.setCancelled(true); + return; + } + if (Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getHopperManager().isHopper(block.getLocation())) return; diff --git a/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java b/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java index e0d3250..9e895f7 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java @@ -69,6 +69,10 @@ public class HopperListeners implements Listener { if (destinationLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(destinationLocation)) return; + // Calling HopperManager#getHopper() automatically creates a new Hopper and we don't need to iterate over default-valued hoppers + if (!instance.getHopperManager().isHopper(destinationLocation)) + return; + Hopper toHopper = instance.getHopperManager().getHopper(destinationLocation); // minecraft 1.8 doesn't have a method to get the hopper's location from the inventory, so we use the holder instead final ItemStack toMove = event.getItem(); @@ -89,7 +93,7 @@ public class HopperListeners implements Listener { if (toHopper != null && toHopper.getFilter().getEndPoint() == null && !(toHopper.getFilter().getWhiteList().isEmpty() && toHopper.getFilter().getBlackList().isEmpty())) { - // this hopper has a filter with no rejection endpoint, so don't absorb disalowed items + // this hopper has a filter with no rejection endpoint, so don't absorb disallowed items boolean allowItem; ItemStack moveInstead = null; // whitelist has priority @@ -113,10 +117,9 @@ public class HopperListeners implements Listener { } } else { // check the blacklist - allowItem = !toHopper.getFilter().getBlackList().stream().anyMatch(item -> Methods.isSimilarMaterial(toMove, item)); + allowItem = toHopper.getFilter().getBlackList().stream().noneMatch(item -> Methods.isSimilarMaterial(toMove, item)); if (!allowItem) { // can we change the item to something else? - searchReplacement: for (ItemStack sourceItem : source.getContents()) { if (sourceItem != null && Methods.canMove(destination, sourceItem)) { boolean blacklisted = toHopper.getFilter().getBlackList().stream().anyMatch(item -> Methods.isSimilarMaterial(sourceItem, item)); diff --git a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java index cb9b152..83873d1 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java @@ -36,7 +36,7 @@ public class InteractListeners implements Listener { @EventHandler public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent event) { Player player = event.getPlayer(); - if (player.isSneaking()) { + if (player.isSneaking() && instance.getHopperManager().isReady()) { Location location = player.getLocation().getBlock().getRelative(BlockFace.SELF).getLocation(); Location down = location.getBlock().getRelative(BlockFace.DOWN).getLocation(); if (instance.getHopperManager().isHopper(down)) { @@ -82,9 +82,14 @@ public class InteractListeners implements Listener { PlayerData playerData = instance.getPlayerDataManager().getPlayerData(player); - if (playerData.getSyncType() == null) { if (event.getClickedBlock().getType() == Material.HOPPER) { + if (!instance.getHopperManager().isReady()) { + player.sendMessage(instance.getLocale().getMessage("event.hopper.notready").getMessage()); + event.setCancelled(true); + return; + } + if (Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(event.getClickedBlock().getLocation())) return; diff --git a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java index 92971d5..22aecf8 100644 --- a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java +++ b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java @@ -3,7 +3,6 @@ package com.songoda.epichoppers.tasks; import com.songoda.core.compatibility.CompatibleMaterial; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.boost.BoostData; -import com.songoda.epichoppers.hopper.HopperManager; import com.songoda.epichoppers.hopper.levels.modules.Module; import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoCrafting; import com.songoda.epichoppers.settings.Settings; @@ -29,10 +28,8 @@ import org.bukkit.scheduler.BukkitRunnable; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.List; import java.util.Objects; -import java.util.Set; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -174,7 +171,6 @@ public class HopTask extends BukkitRunnable { // Move items into destination containers pushItemsIntoContainers(hopper, hopperCache, maxToMove, blockedMaterials, hopperDirection); - } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index 3dad51b..8b5276c 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -106,3 +106,4 @@ event: syncchest: '&7You have linked your &9%name% &7with this chest.' desyncchest: '&7You have unlinked your &9%name% &7with this chest.' autosell: '&7Your hopper sold &6%items% &7item(s) worth &6$%amount%&7.' + notready: '&cHoppers are still being loaded...' From e5f4869204ea4389006c5ec07bcedeca7611849e Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Tue, 13 Apr 2021 16:46:33 +0200 Subject: [PATCH 2/8] Code cleanup --- .../java/com/songoda/epichoppers/EpicHoppers.java | 1 + .../songoda/epichoppers/commands/CommandBoost.java | 3 +-- .../com/songoda/epichoppers/gui/GUIOverview.java | 2 +- .../java/com/songoda/epichoppers/hopper/Filter.java | 10 +++++----- .../java/com/songoda/epichoppers/hopper/Hopper.java | 2 +- .../epichoppers/hopper/levels/LevelManager.java | 2 +- .../hopper/levels/modules/ModuleAutoCrafting.java | 2 +- .../hopper/levels/modules/ModuleAutoSmelter.java | 2 +- .../hopper/levels/modules/ModuleSuction.java | 13 ++++++------- .../epichoppers/listeners/InteractListeners.java | 2 +- .../com/songoda/epichoppers/player/PlayerData.java | 2 -- .../epichoppers/player/PlayerDataManager.java | 4 +--- .../java/com/songoda/epichoppers/tasks/HopTask.java | 10 +++++----- .../java/com/songoda/epichoppers/utils/Methods.java | 3 ++- .../com/songoda/epichoppers/utils/Serializers.java | 6 ++---- .../epichoppers/utils/StorageContainerCache.java | 10 ++++++++-- 16 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/songoda/epichoppers/EpicHoppers.java b/src/main/java/com/songoda/epichoppers/EpicHoppers.java index 38cc8c8..f24ba11 100644 --- a/src/main/java/com/songoda/epichoppers/EpicHoppers.java +++ b/src/main/java/com/songoda/epichoppers/EpicHoppers.java @@ -258,6 +258,7 @@ public class EpicHoppers extends SongodaPlugin { return nbtItem.finish(); } + @Override public Locale getLocale() { return locale; } diff --git a/src/main/java/com/songoda/epichoppers/commands/CommandBoost.java b/src/main/java/com/songoda/epichoppers/commands/CommandBoost.java index 3000b89..9fdc059 100644 --- a/src/main/java/com/songoda/epichoppers/commands/CommandBoost.java +++ b/src/main/java/com/songoda/epichoppers/commands/CommandBoost.java @@ -35,8 +35,7 @@ public class CommandBoost extends AbstractCommand { long duration = 0L; if (args.length > 2) { - for (int i = 0; i < args.length; i++) { - String line = args[i]; + for (String line : args) { long time = Methods.parseTime(line); duration += time; diff --git a/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java b/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java index 80616b5..7ce40ca 100644 --- a/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java @@ -269,7 +269,7 @@ public class GUIOverview extends CustomizableGui { private void runTask() { task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> { - if (inventory.getViewers().size() != 0) + if (!inventory.getViewers().isEmpty()) this.constructGUI(); }, 5L, 5L); } diff --git a/src/main/java/com/songoda/epichoppers/hopper/Filter.java b/src/main/java/com/songoda/epichoppers/hopper/Filter.java index 02dd456..c740da0 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/Filter.java +++ b/src/main/java/com/songoda/epichoppers/hopper/Filter.java @@ -21,7 +21,7 @@ public class Filter { public List getWhiteList() { - return whiteList != null ? whiteList : Collections.EMPTY_LIST; + return whiteList != null ? whiteList : Collections.emptyList(); } @@ -31,7 +31,7 @@ public class Filter { public List getBlackList() { - return blackList != null ? blackList : Collections.EMPTY_LIST; + return blackList != null ? blackList : Collections.emptyList(); } @@ -41,7 +41,7 @@ public class Filter { public List getVoidList() { - return voidList != null ? voidList : Collections.EMPTY_LIST; + return voidList != null ? voidList : Collections.emptyList(); } @@ -51,7 +51,7 @@ public class Filter { public List getAutoSellWhiteList() { - return autoSellWhiteList != null ? autoSellWhiteList : Collections.EMPTY_LIST; + return autoSellWhiteList != null ? autoSellWhiteList : Collections.emptyList(); } @@ -61,7 +61,7 @@ public class Filter { public List getAutoSellBlackList() { - return autoSellBlackList != null ? autoSellBlackList : Collections.EMPTY_LIST; + return autoSellBlackList != null ? autoSellBlackList : Collections.emptyList(); } diff --git a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java index f4bb22a..ab676cd 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java +++ b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java @@ -92,7 +92,7 @@ public class Hopper { return; } if (!EconomyManager.hasBalance(player, cost)) { - plugin.getInstance().getLocale().getMessage("event.upgrade.cannotafford").sendPrefixedMessage(player); + plugin.getLocale().getMessage("event.upgrade.cannotafford").sendPrefixedMessage(player); return; } EconomyManager.withdrawBalance(player, cost); diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java b/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java index 3c52cd4..2d6ab46 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java @@ -36,7 +36,7 @@ public class LevelManager { // Legacy trash. if (item.hasItemMeta() && item.getItemMeta().getDisplayName().contains(":")) { - String arr[] = item.getItemMeta().getDisplayName().replace(String.valueOf(ChatColor.COLOR_CHAR), "").split(":"); + String[] arr = item.getItemMeta().getDisplayName().replace(String.valueOf(ChatColor.COLOR_CHAR), "").split(":"); return getLevel(Integer.parseInt(arr[0])); } diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java index 050a8e1..68ec208 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java @@ -460,7 +460,7 @@ public class ModuleAutoCrafting extends Module { * Like {@link #equals(Object)} but ignores {@link #additionalAmount} and {@link ItemStack#getAmount()} * * @return If two {@link SimpleIngredient} objects are equal - * while ignoring any item amounts, true otherwise false + * while ignoring any item amounts, true otherwise false */ public boolean isSimilar(Object o) { if (this == o) return true; diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java index 6ef58fa..acc89c6 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java @@ -99,7 +99,7 @@ public class ModuleAutoSmelter extends Module { @Override public List getBlockedItems(Hopper hopper) { if (getTime(hopper) == -9999) - return Collections.EMPTY_LIST; + return Collections.emptyList(); List blockedItems = new ArrayList<>(); for (CompatibleMaterial material : CompatibleMaterial.values()) diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java index 2bb7891..ce0db38 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java @@ -37,8 +37,8 @@ public class ModuleSuction extends Module { private static List blacklist = new ArrayList<>(); - private final static boolean wildStacker = Bukkit.getPluginManager().isPluginEnabled("WildStacker"); - private final static boolean ultimateStacker = Bukkit.getPluginManager().isPluginEnabled("UltimateStacker"); + private static final boolean wildStacker = Bukkit.getPluginManager().isPluginEnabled("WildStacker"); + private static final boolean ultimateStacker = Bukkit.getPluginManager().isPluginEnabled("UltimateStacker"); private static boolean oldUltimateStacker; private static Method oldUltimateStacker_updateItemAmount; @@ -47,7 +47,7 @@ public class ModuleSuction extends Module { try { oldUltimateStacker_updateItemAmount = com.songoda.ultimatestacker.utils.Methods.class.getDeclaredMethod("updateItemAmount", Item.class, int.class); oldUltimateStacker = true; - } catch (NoSuchMethodException | SecurityException ex) { + } catch (NoSuchMethodException | SecurityException ignore) { } } else { oldUltimateStacker = false; @@ -76,7 +76,7 @@ public class ModuleSuction extends Module { && !entity.isDead() && entity.getTicksLived() >= ((Item) entity).getPickupDelay() && entity.getLocation().getBlock().getType() != Material.HOPPER) - .map(entity -> (Item) entity) + .map(Item.class::cast) .collect(Collectors.toSet()); if (itemsToSuck.isEmpty()) @@ -84,10 +84,9 @@ public class ModuleSuction extends Module { boolean filterEndpoint = hopper.getFilter().getEndPoint() != null; - InventoryHolder inventoryHolder = null; Inventory hopperInventory = null; if (Settings.EMIT_INVENTORYPICKUPITEMEVENT.getBoolean()) { - inventoryHolder = (InventoryHolder) hopper.getBlock().getState(); + InventoryHolder inventoryHolder = (InventoryHolder) hopper.getBlock().getState(); hopperInventory = Bukkit.createInventory(inventoryHolder, InventoryType.HOPPER); } @@ -118,7 +117,7 @@ public class ModuleSuction extends Module { // whitelist has priority if (!hopper.getFilter().getWhiteList().isEmpty()) { // is this item on the whitelist? - if (!hopper.getFilter().getWhiteList().stream().anyMatch(filterItem -> Methods.isSimilarMaterial(itemStack, filterItem))) { + if (hopper.getFilter().getWhiteList().stream().noneMatch(filterItem -> Methods.isSimilarMaterial(itemStack, filterItem))) { // nope! continue; } diff --git a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java index 83873d1..2b0807c 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java @@ -106,7 +106,7 @@ public class InteractListeners implements Listener { if (event.getClickedBlock().getState() instanceof InventoryHolder || (event.getClickedBlock().getType().equals(Material.ENDER_CHEST) && instance.getConfig().getBoolean("Main.Support Enderchests"))) { Hopper hopper = playerData.getLastHopper(); if (event.getClickedBlock().getLocation().equals(playerData.getLastHopper().getLocation())) { - if (hopper.getLinkedBlocks().size() != 0) + if (!hopper.getLinkedBlocks().isEmpty()) instance.getLocale().getMessage("event.hopper.syncfinish").sendPrefixedMessage(player); else instance.getLocale().getMessage("event.hopper.synccanceled").sendPrefixedMessage(player); diff --git a/src/main/java/com/songoda/epichoppers/player/PlayerData.java b/src/main/java/com/songoda/epichoppers/player/PlayerData.java index 184f557..28e51f0 100644 --- a/src/main/java/com/songoda/epichoppers/player/PlayerData.java +++ b/src/main/java/com/songoda/epichoppers/player/PlayerData.java @@ -2,8 +2,6 @@ package com.songoda.epichoppers.player; import com.songoda.epichoppers.hopper.Hopper; -import java.util.UUID; - public class PlayerData { private Hopper lastHopper = null; diff --git a/src/main/java/com/songoda/epichoppers/player/PlayerDataManager.java b/src/main/java/com/songoda/epichoppers/player/PlayerDataManager.java index 2463206..091b63c 100644 --- a/src/main/java/com/songoda/epichoppers/player/PlayerDataManager.java +++ b/src/main/java/com/songoda/epichoppers/player/PlayerDataManager.java @@ -13,9 +13,7 @@ public class PlayerDataManager { private final Map registeredPlayers = new HashMap<>(); private PlayerData getPlayerData(UUID uuid) { - if (!registeredPlayers.containsKey(uuid)) - registeredPlayers.put(uuid, new PlayerData()); - return registeredPlayers.get(uuid); + return registeredPlayers.computeIfAbsent(uuid, u -> new PlayerData()); } public PlayerData getPlayerData(Player player) { diff --git a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java index 22aecf8..a8c3b7f 100644 --- a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java +++ b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java @@ -41,14 +41,14 @@ public class HopTask extends BukkitRunnable { // Hop to the bop to the be bop top. - private static EpicHoppers plugin; + private EpicHoppers plugin; private final int hopTicks; private final boolean hasFabledSkyBlock; private final Plugin fabledSkyblockPlugin; public HopTask(EpicHoppers plugin) { - HopTask.plugin = plugin; + this.plugin = plugin; this.hopTicks = Math.max(1, Settings.HOP_TICKS.getInt() / 2); // Purposeful integer division. Don't go below 1. this.runTaskTimer(plugin, 0, 2); this.hasFabledSkyBlock = (fabledSkyblockPlugin = Bukkit.getPluginManager().getPlugin("FabledSkyBlock")) != null; @@ -107,8 +107,8 @@ public class HopTask extends BukkitRunnable { List materials = module.getBlockedItems(hopper); if (materials != null && !materials.isEmpty()) blockedMaterials.addAll(materials); - } catch (Throwable th) { - th.printStackTrace(); + } catch (Exception ex) { + ex.printStackTrace(); } }); @@ -382,7 +382,7 @@ public class HopTask extends BukkitRunnable { if (checkForMinecarts) { for (InventoryHolder minecartInventory : hopper.getWorld().getNearbyEntities(pointingLocation.clone().add(0.5, 0.5, 0.5), 0.5, 0.5, 0.5) .stream().filter(e -> e.getType() == EntityType.MINECART_CHEST || e.getType() == EntityType.MINECART_HOPPER) - .map(e -> (InventoryHolder) e).collect(Collectors.toSet())) { + .map(InventoryHolder.class::cast).collect(Collectors.toSet())) { StorageContainerCache.Cache cache = new StorageContainerCache.Cache(Material.CHEST, minecartInventory.getInventory().getContents()); if (tryPush(hopper, hopperCache, cache, filterCache, maxToMove, blockedMaterials)) { if (cache.isDirty()) diff --git a/src/main/java/com/songoda/epichoppers/utils/Methods.java b/src/main/java/com/songoda/epichoppers/utils/Methods.java index ff7e75f..d5151e6 100644 --- a/src/main/java/com/songoda/epichoppers/utils/Methods.java +++ b/src/main/java/com/songoda/epichoppers/utils/Methods.java @@ -165,8 +165,9 @@ public class Methods { return value * 1000 * 60; case 's': return value * 1000; + default: + return 0; } - return 0; } /** diff --git a/src/main/java/com/songoda/epichoppers/utils/Serializers.java b/src/main/java/com/songoda/epichoppers/utils/Serializers.java index e0d5c70..706df24 100644 --- a/src/main/java/com/songoda/epichoppers/utils/Serializers.java +++ b/src/main/java/com/songoda/epichoppers/utils/Serializers.java @@ -90,8 +90,7 @@ public class Serializers { SkullMeta meta = (SkullMeta) item.getItemMeta(); meta.setOwner(owner); item.setItemMeta(meta); - } catch (Exception exception) { - return; + } catch (Exception ignore) { } } @@ -140,8 +139,7 @@ public class Serializers { LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta(); meta.setColor(Color.fromRGB(red, green, blue)); item.setItemMeta(meta); - } catch (Exception exception) { - return; + } catch (Exception ignore) { } } diff --git a/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java b/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java index f7df53e..d6cff65 100644 --- a/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java +++ b/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java @@ -22,7 +22,7 @@ import java.util.Map; */ public class StorageContainerCache { - private final static Map inventoryCache = new HashMap<>(); + private static final Map inventoryCache = new HashMap<>(); // need to get the topmost inventory for a double chest, and save as that block public static Cache getCachedInventory(Block b) { @@ -71,6 +71,9 @@ public class StorageContainerCache { break; case WEST: other = block.getRelative(c.getType() != Chest.Type.RIGHT ? BlockFace.NORTH : BlockFace.SOUTH); + break; + default: + break; } // double-check if (other != null && other.getType() == block.getType()) { @@ -81,7 +84,7 @@ public class StorageContainerCache { } else { // legacy check Material material = block.getType(); - BlockFace[] faces = new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}; + BlockFace[] faces = new BlockFace[] {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}; for (BlockFace face : faces) { Block adjacentBlock = block.getRelative(face); @@ -291,7 +294,10 @@ public class StorageContainerCache { else check[0] = true; + break; } + default: + break; } // we can reduce calls to ItemStack.isSimilar() by caching what cells to look at From 036882c069a13e12ac127c7024ca4ffeddcac318 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Tue, 13 Apr 2021 17:33:07 +0200 Subject: [PATCH 3/8] Fix accidental call to Math#floor over an int --- .../epichoppers/hopper/levels/modules/ModuleAutoSmelter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java index acc89c6..f0f275d 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSmelter.java @@ -77,7 +77,7 @@ public class ModuleAutoSmelter extends Module { blockmeta.setDisplayName(plugin.getLocale().getMessage("interface.hopper.smelttitle").getMessage()); ArrayList loreblock = new ArrayList<>(); String[] parts = plugin.getLocale().getMessage("interface.hopper.smeltlore").processPlaceholder("timeleft", - getTime(hopper) == -9999 ? "\u221E" : (int) Math.floor(getTime(hopper) / 20)).processPlaceholder("enabled", + getTime(hopper) == -9999 ? "\u221E" : (int) Math.floor(getTime(hopper) / 20.0)).processPlaceholder("enabled", isEnabled(hopper) ? EpicHoppers.getInstance().getLocale().getMessage("general.word.enabled").getMessage() : EpicHoppers.getInstance().getLocale().getMessage("general.word.disabled").getMessage()).getMessage().split("\\|"); for (String line : parts) { @@ -112,7 +112,7 @@ public class ModuleAutoSmelter extends Module { @Override public String getDescription() { return plugin.getLocale().getMessage("interface.hopper.autosmelt") - .processPlaceholder("ticks", (int) Math.floor(timeOut / 20)).getMessage(); + .processPlaceholder("ticks", (int) Math.floor(timeOut / 20.0)).getMessage(); } private int getTime(Hopper hopper) { From 42710c6b3b4b055e8c1cc3ffe519d5c452e22f06 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 1 Jun 2021 12:39:00 -0500 Subject: [PATCH 4/8] Remove legacy support for UltimateStacker and update support. --- pom.xml | 2 +- .../hopper/levels/modules/ModuleSuction.java | 25 ++----------------- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 291babe..2e32ec4 100644 --- a/pom.xml +++ b/pom.xml @@ -122,7 +122,7 @@ com.songoda UltimateStacker - 1.9.6 + 2.1.6 provided diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java index ce0db38..c5997a6 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java @@ -10,6 +10,7 @@ import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.Methods; import com.songoda.epichoppers.utils.StorageContainerCache; +import com.songoda.ultimatestacker.UltimateStacker; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -39,20 +40,6 @@ public class ModuleSuction extends Module { private static final boolean wildStacker = Bukkit.getPluginManager().isPluginEnabled("WildStacker"); private static final boolean ultimateStacker = Bukkit.getPluginManager().isPluginEnabled("UltimateStacker"); - private static boolean oldUltimateStacker; - private static Method oldUltimateStacker_updateItemAmount; - - static { - if (ultimateStacker) { - try { - oldUltimateStacker_updateItemAmount = com.songoda.ultimatestacker.utils.Methods.class.getDeclaredMethod("updateItemAmount", Item.class, int.class); - oldUltimateStacker = true; - } catch (NoSuchMethodException | SecurityException ignore) { - } - } else { - oldUltimateStacker = false; - } - } public ModuleSuction(EpicHoppers plugin, int amount) { super(plugin); @@ -176,15 +163,7 @@ public class ModuleSuction extends Module { private void updateAmount(Item item, int amount) { if (ultimateStacker) { - if (oldUltimateStacker) { - try { - oldUltimateStacker_updateItemAmount.invoke(null, item, amount); - } catch (Exception ex) { - item.remove(); // not the best solution, but they should update, anyway.. - } - } else { - com.songoda.ultimatestacker.utils.Methods.updateItemAmount(item, item.getItemStack(), amount); - } + UltimateStacker.updateItemAmount(item, item.getItemStack(), amount); } else if (wildStacker) WildStackerAPI.getStackedItem(item).setStackAmount(amount, true); else From 9aba0f8fa3c7737f56a3cf771b4c86da83edfcf2 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 1 Jun 2021 12:45:59 -0500 Subject: [PATCH 5/8] Make use of the CompatibleSound API. --- .../java/com/songoda/epichoppers/handlers/TeleportHandler.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java b/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java index 6ffbda4..e01cfcc 100644 --- a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java +++ b/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java @@ -109,7 +109,6 @@ public class TeleportHandler { entity.teleport(location); - if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) - entity.getWorld().playSound(entity.getLocation(), ServerVersion.isServerVersion(ServerVersion.V1_12) ? Sound.valueOf("ENTITY_ENDERMEN_TELEPORT") : Sound.ENTITY_ENDERMAN_TELEPORT, 10, 10); + CompatibleSound.ENTITY_ENDERMAN_TELEPORT.play(entity.getWorld(), entity.getLocation(), 10, 10); } } From 4e7e6f25852b769c59f7b9157fddaafd0db79a89 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 1 Jun 2021 12:50:39 -0500 Subject: [PATCH 6/8] This whole file just wasn't used -.- --- .../epichoppers/utils/Serializers.java | 154 ------------------ 1 file changed, 154 deletions(-) delete mode 100644 src/main/java/com/songoda/epichoppers/utils/Serializers.java diff --git a/src/main/java/com/songoda/epichoppers/utils/Serializers.java b/src/main/java/com/songoda/epichoppers/utils/Serializers.java deleted file mode 100644 index 706df24..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/Serializers.java +++ /dev/null @@ -1,154 +0,0 @@ -package com.songoda.epichoppers.utils; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Color; -import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.LeatherArmorMeta; -import org.bukkit.inventory.meta.SkullMeta; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class Serializers { - public static String serialize(ItemStack item) { - StringBuilder builder = new StringBuilder(); - builder.append(item.getType().toString()); - if (item.getDurability() != 0) builder.append(":" + item.getDurability()); - builder.append(" " + item.getAmount()); - for (Enchantment enchant : item.getEnchantments().keySet()) - builder.append(" " + enchant.getName() + ":" + item.getEnchantments().get(enchant)); - String name = getName(item); - if (name != null) builder.append(" name:" + name); - String lore = getLore(item); - if (lore != null) builder.append(" lore:" + lore); - Color color = getArmorColor(item); - if (color != null) builder.append(" rgb:" + color.getRed() + "|" + color.getGreen() + "|" + color.getBlue()); - String owner = getOwner(item); - if (owner != null) builder.append(" owner:" + owner); - return builder.toString(); - } - - public static ItemStack deserialize(String serializedItem) { - String[] strings = serializedItem.split(" "); - Map enchants = new HashMap<>(); - String[] args; - ItemStack item = new ItemStack(Material.AIR); - for (String str : strings) { - args = str.split(":"); - if (Material.matchMaterial(args[0]) != null && item.getType() == Material.AIR) { - item.setType(Material.matchMaterial(args[0])); - if (args.length == 2) item.setDurability(Short.parseShort(args[1])); - break; - } - } - if (item.getType() == Material.AIR) { - Bukkit.getLogger().info("Could not find a valid material for the item in \"" + serializedItem + "\""); - return null; - } - for (String str : strings) { - args = str.split(":", 2); - if (isNumber(args[0])) item.setAmount(Integer.parseInt(args[0])); - if (args.length == 1) continue; - if (args[0].equalsIgnoreCase("name")) { - setName(item, ChatColor.translateAlternateColorCodes('&', args[1])); - continue; - } - if (args[0].equalsIgnoreCase("lore")) { - setLore(item, ChatColor.translateAlternateColorCodes('&', args[1])); - continue; - } - if (args[0].equalsIgnoreCase("rgb")) { - setArmorColor(item, args[1]); - continue; - } - if (args[0].equalsIgnoreCase("owner")) { - setOwner(item, args[1]); - continue; - } - if (Enchantment.getByName(args[0].toUpperCase()) != null) { - enchants.put(Enchantment.getByName(args[0].toUpperCase()), Integer.parseInt(args[1])); - continue; - } - } - item.addUnsafeEnchantments(enchants); - return item.getType().equals(Material.AIR) ? null : item; - } - - private static String getOwner(ItemStack item) { - if (!(item.getItemMeta() instanceof SkullMeta)) return null; - return ((SkullMeta) item.getItemMeta()).getOwner(); - } - - private static void setOwner(ItemStack item, String owner) { - try { - SkullMeta meta = (SkullMeta) item.getItemMeta(); - meta.setOwner(owner); - item.setItemMeta(meta); - } catch (Exception ignore) { - } - } - - private static String getName(ItemStack item) { - if (!item.hasItemMeta()) return null; - if (!item.getItemMeta().hasDisplayName()) return null; - return item.getItemMeta().getDisplayName().replace(" ", "_").replace(ChatColor.COLOR_CHAR, '&'); - } - - private static void setName(ItemStack item, String name) { - name = name.replace("_", " ").replace('&', ChatColor.COLOR_CHAR); - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(name); - item.setItemMeta(meta); - } - - private static String getLore(ItemStack item) { - if (!item.hasItemMeta()) return null; - if (!item.getItemMeta().hasLore()) return null; - StringBuilder builder = new StringBuilder(); - List lore = item.getItemMeta().getLore(); - for (int ind = 0; ind < lore.size(); ind++) { - builder.append((ind > 0 ? "|" : "") + lore.get(ind).replace(" ", "_").replace(ChatColor.COLOR_CHAR, '&')); - } - return builder.toString(); - } - - private static void setLore(ItemStack item, String lore) { - lore = lore.replace("_", " ").replace('&', ChatColor.COLOR_CHAR); - ItemMeta meta = item.getItemMeta(); - meta.setLore(Arrays.asList(lore.split("\\|"))); - item.setItemMeta(meta); - } - - private static Color getArmorColor(ItemStack item) { - if (!(item.getItemMeta() instanceof LeatherArmorMeta)) return null; - return ((LeatherArmorMeta) item.getItemMeta()).getColor(); - } - - private static void setArmorColor(ItemStack item, String str) { - try { - String[] colors = str.split("\\|"); - int red = Integer.parseInt(colors[0]); - int green = Integer.parseInt(colors[1]); - int blue = Integer.parseInt(colors[2]); - LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta(); - meta.setColor(Color.fromRGB(red, green, blue)); - item.setItemMeta(meta); - } catch (Exception ignore) { - } - } - - private static boolean isNumber(String str) { - try { - Integer.parseInt(str); - } catch (NumberFormatException exception) { - return false; - } - return true; - } -} From c74c222e16499229273b825c32e81a2a29d4d7e6 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 1 Jun 2021 13:32:39 -0500 Subject: [PATCH 7/8] Cleaned up some code. --- .../com/songoda/epichoppers/EpicHoppers.java | 2 +- .../epichoppers/database/DataManager.java | 2 +- .../songoda/epichoppers/gui/GUIOverview.java | 2 +- .../songoda/epichoppers/hopper/Hopper.java | 26 ++++--- .../epichoppers/hopper/HopperBuilder.java | 2 +- .../teleport}/TeleportHandler.java | 67 ++++++++++--------- .../hopper/teleport/TeleportTrigger.java | 7 ++ .../epichoppers/listeners/BlockListeners.java | 2 +- .../listeners/HopperListeners.java | 14 ++-- .../listeners/InteractListeners.java | 44 ++++++------ .../epichoppers/settings/Settings.java | 4 +- .../songoda/epichoppers/utils/CostType.java | 13 +--- .../epichoppers/utils/HopperDirection.java | 17 +++-- .../epichoppers/utils/TeleportTrigger.java | 7 -- 14 files changed, 99 insertions(+), 110 deletions(-) rename src/main/java/com/songoda/epichoppers/{handlers => hopper/teleport}/TeleportHandler.java (62%) create mode 100644 src/main/java/com/songoda/epichoppers/hopper/teleport/TeleportTrigger.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/TeleportTrigger.java diff --git a/src/main/java/com/songoda/epichoppers/EpicHoppers.java b/src/main/java/com/songoda/epichoppers/EpicHoppers.java index f24ba11..f645704 100644 --- a/src/main/java/com/songoda/epichoppers/EpicHoppers.java +++ b/src/main/java/com/songoda/epichoppers/EpicHoppers.java @@ -22,7 +22,7 @@ import com.songoda.epichoppers.commands.CommandReload; import com.songoda.epichoppers.commands.CommandSettings; import com.songoda.epichoppers.database.DataManager; import com.songoda.epichoppers.database.migrations._1_InitialMigration; -import com.songoda.epichoppers.handlers.TeleportHandler; +import com.songoda.epichoppers.hopper.teleport.TeleportHandler; import com.songoda.epichoppers.hopper.HopperManager; import com.songoda.epichoppers.hopper.levels.Level; import com.songoda.epichoppers.hopper.levels.LevelManager; diff --git a/src/main/java/com/songoda/epichoppers/database/DataManager.java b/src/main/java/com/songoda/epichoppers/database/DataManager.java index 073f414..ad1af16 100644 --- a/src/main/java/com/songoda/epichoppers/database/DataManager.java +++ b/src/main/java/com/songoda/epichoppers/database/DataManager.java @@ -9,7 +9,7 @@ import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.hopper.HopperBuilder; import com.songoda.epichoppers.hopper.ItemType; import com.songoda.epichoppers.hopper.LinkType; -import com.songoda.epichoppers.utils.TeleportTrigger; +import com.songoda.epichoppers.hopper.teleport.TeleportTrigger; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; diff --git a/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java b/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java index 7ce40ca..80e563d 100644 --- a/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java @@ -14,7 +14,7 @@ import com.songoda.epichoppers.player.SyncType; import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.CostType; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.TeleportTrigger; +import com.songoda.epichoppers.hopper.teleport.TeleportTrigger; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; diff --git a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java index ab676cd..11f80f2 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java +++ b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java @@ -1,5 +1,7 @@ package com.songoda.epichoppers.hopper; +import com.songoda.core.compatibility.CompatibleParticleHandler; +import com.songoda.core.compatibility.CompatibleSound; import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.gui.GuiManager; import com.songoda.core.hooks.EconomyManager; @@ -7,14 +9,14 @@ import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.api.events.HopperAccessEvent; import com.songoda.epichoppers.gui.GUIOverview; import com.songoda.epichoppers.hopper.levels.Level; +import com.songoda.epichoppers.hopper.teleport.TeleportTrigger; import com.songoda.epichoppers.player.PlayerData; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.CostType; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.TeleportTrigger; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; -import org.bukkit.Sound; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -123,20 +125,16 @@ public class Hopper { } Location loc = location.clone().add(.5, .5, .5); - if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) return; - - player.getWorld().spawnParticle(org.bukkit.Particle.valueOf(plugin.getConfig().getString("Main.Upgrade Particle Type")), loc, 200, .5, .5, .5); + CompatibleParticleHandler.spawnParticles(CompatibleParticleHandler.ParticleType.getParticle(Settings.UPGRADE_PARTICLE_TYPE.getString()), + loc, 100, .5, .5, .5); if (plugin.getLevelManager().getHighestLevel() != level) { - player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 0.6F, 15.0F); + player.playSound(player.getLocation(), CompatibleSound.ENTITY_PLAYER_LEVELUP.getSound(), 0.6F, 15.0F); } else { - player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 2F, 25.0F); - - if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) return; - - player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 2F, 25.0F); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.2F, 35.0F), 5L); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.8F, 35.0F), 10L); + player.playSound(player.getLocation(), CompatibleSound.ENTITY_PLAYER_LEVELUP.getSound(), 2F, 25.0F); + player.playSound(player.getLocation(), CompatibleSound.BLOCK_NOTE_BLOCK_CHIME.getSound(), 2F, 25.0F); + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), CompatibleSound.BLOCK_NOTE_BLOCK_CHIME.getSound(), 1.2F, 35.0F), 5L); + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), CompatibleSound.BLOCK_NOTE_BLOCK_CHIME.getSound(), 1.8F, 35.0F), 10L); } } @@ -155,7 +153,7 @@ public class Hopper { instance.getLocale().getMessage("event.hopper.synctimeout").sendPrefixedMessage(player); playerData.setSyncType(null); } - }, instance.getConfig().getLong("Main.Timeout When Syncing Hoppers") * level.getLinkAmount()); + }, Settings.LINK_TIMEOUT.getLong() * level.getLinkAmount()); } public void link(Block toLink, boolean filtered, Player player) { diff --git a/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java b/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java index b610a45..a0c8331 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java +++ b/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java @@ -1,7 +1,7 @@ package com.songoda.epichoppers.hopper; import com.songoda.epichoppers.hopper.levels.Level; -import com.songoda.epichoppers.utils.TeleportTrigger; +import com.songoda.epichoppers.hopper.teleport.TeleportTrigger; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; diff --git a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java b/src/main/java/com/songoda/epichoppers/hopper/teleport/TeleportHandler.java similarity index 62% rename from src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java rename to src/main/java/com/songoda/epichoppers/hopper/teleport/TeleportHandler.java index e01cfcc..6b634c4 100644 --- a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java +++ b/src/main/java/com/songoda/epichoppers/hopper/teleport/TeleportHandler.java @@ -1,13 +1,13 @@ -package com.songoda.epichoppers.handlers; +package com.songoda.epichoppers.hopper.teleport; +import com.songoda.core.compatibility.CompatibleSound; import com.songoda.core.compatibility.ServerVersion; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.TeleportTrigger; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.Sound; import org.bukkit.World; import org.bukkit.block.BlockFace; import org.bukkit.entity.Entity; @@ -24,43 +24,45 @@ public class TeleportHandler { private final Map lastTeleports = new HashMap<>(); - private EpicHoppers plugin; + private final EpicHoppers plugin; public TeleportHandler(EpicHoppers plugin) { this.plugin = plugin; - Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::teleportRunner, 0, plugin.getConfig().getLong("Main.Amount of Ticks Between Teleport")); + Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::teleportRunner, 0, + Settings.TELEPORT_TICKS.getLong()); } private void teleportRunner() { - if (plugin.getHopperManager().isReady()) { - for (World world : Bukkit.getWorlds()) { - for (Entity entity : world.getEntities()) { - if (!(entity instanceof LivingEntity) || entity.getType() == EntityType.ARMOR_STAND) + if (!plugin.getHopperManager().isReady()) + return; + + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (!(entity instanceof LivingEntity) || entity.getType() == EntityType.ARMOR_STAND) + continue; + + if (!Settings.TELEPORT.getBoolean() + || (entity instanceof Player && !entity.hasPermission("EpicHoppers.Teleport"))) + continue; + + Location location = entity.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation(); + + if (!this.plugin.getHopperManager().isHopper(location)) + continue; + + Hopper hopper = this.plugin.getHopperManager().getHopper(location); + + if (hopper.getTeleportTrigger() != TeleportTrigger.WALK_ON) + continue; + + if (this.lastTeleports.containsKey(entity.getUniqueId())) { + long duration = (new Date()).getTime() - new Date(this.lastTeleports.get(entity.getUniqueId())).getTime(); + if (duration <= 5 * 1000) continue; - - if (!this.plugin.getConfig().getBoolean("Main.Allow Players To Teleport Through Hoppers") - || (entity instanceof Player && !entity.hasPermission("EpicHoppers.Teleport"))) - continue; - - Location location = entity.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation(); - - if (!this.plugin.getHopperManager().isHopper(location)) - continue; - - Hopper hopper = this.plugin.getHopperManager().getHopper(location); - - if (hopper.getTeleportTrigger() != TeleportTrigger.WALK_ON) - continue; - - if (this.lastTeleports.containsKey(entity.getUniqueId())) { - long duration = (new Date()).getTime() - new Date(this.lastTeleports.get(entity.getUniqueId())).getTime(); - if (duration <= 5 * 1000) - continue; - } - - this.tpEntity(entity, hopper); - this.lastTeleports.put(entity.getUniqueId(), System.currentTimeMillis()); } + + this.tpEntity(entity, hopper); + this.lastTeleports.put(entity.getUniqueId(), System.currentTimeMillis()); } } } @@ -79,7 +81,6 @@ public class TeleportHandler { * * @param lastHopper The previous hopper found in the chain * @param currentChainLength The current length of the chain, used to cap the search length - * * @return The hopper at the end of the chain (or up to 15 in depth) */ private Hopper getChain(Hopper lastHopper, int currentChainLength) { diff --git a/src/main/java/com/songoda/epichoppers/hopper/teleport/TeleportTrigger.java b/src/main/java/com/songoda/epichoppers/hopper/teleport/TeleportTrigger.java new file mode 100644 index 0000000..2aecdd5 --- /dev/null +++ b/src/main/java/com/songoda/epichoppers/hopper/teleport/TeleportTrigger.java @@ -0,0 +1,7 @@ +package com.songoda.epichoppers.hopper.teleport; + +public enum TeleportTrigger { + + DISABLED, WALK_ON, SNEAK + +} diff --git a/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java b/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java index e880258..8b7acda 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java @@ -75,7 +75,7 @@ public class BlockListeners implements Listener { if (num > limit) limit = num; } - if (limit == -1) limit = plugin.getConfig().getInt("Main.Max Hoppers Per Chunk"); + if (limit == -1) limit = Settings.MAX_CHUNK.getInt(); return limit; } diff --git a/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java b/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java index 9e895f7..acb56d4 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java @@ -25,10 +25,10 @@ import org.bukkit.inventory.ItemStack; */ public class HopperListeners implements Listener { - private final EpicHoppers instance; + private final EpicHoppers plugin; - public HopperListeners(EpicHoppers instance) { - this.instance = instance; + public HopperListeners(EpicHoppers plugin) { + this.plugin = plugin; } // todo: InventoryMoveItemEvent for filters @@ -40,7 +40,7 @@ public class HopperListeners implements Listener { Location sourceLocation = source.getHolder() instanceof BlockState ? ((BlockState) source.getHolder()).getLocation() : null; Location destinationLocation = destination.getHolder() instanceof BlockState ? ((BlockState) destination.getHolder()).getLocation() : null; - if (sourceLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(sourceLocation)) + if (sourceLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getHopperManager().isHopper(sourceLocation)) return; // Hopper minecarts should be able to take care of themselves @@ -66,14 +66,14 @@ public class HopperListeners implements Listener { // Special cases when a hopper is picking up items if (destination.getHolder() instanceof org.bukkit.block.Hopper) { - if (destinationLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(destinationLocation)) + if (destinationLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getHopperManager().isHopper(destinationLocation)) return; // Calling HopperManager#getHopper() automatically creates a new Hopper and we don't need to iterate over default-valued hoppers - if (!instance.getHopperManager().isHopper(destinationLocation)) + if (!plugin.getHopperManager().isHopper(destinationLocation)) return; - Hopper toHopper = instance.getHopperManager().getHopper(destinationLocation); + Hopper toHopper = plugin.getHopperManager().getHopper(destinationLocation); // minecraft 1.8 doesn't have a method to get the hopper's location from the inventory, so we use the holder instead final ItemStack toMove = event.getItem(); diff --git a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java index 2b0807c..c4e8fcc 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java @@ -7,7 +7,7 @@ import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.player.PlayerData; import com.songoda.epichoppers.player.SyncType; import com.songoda.epichoppers.settings.Settings; -import com.songoda.epichoppers.utils.TeleportTrigger; +import com.songoda.epichoppers.hopper.teleport.TeleportTrigger; import com.songoda.skyblock.SkyBlock; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -27,26 +27,26 @@ import org.bukkit.inventory.InventoryHolder; */ public class InteractListeners implements Listener { - private final EpicHoppers instance; + private final EpicHoppers plugin; - public InteractListeners(EpicHoppers instance) { - this.instance = instance; + public InteractListeners(EpicHoppers plugin) { + this.plugin = plugin; } @EventHandler public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent event) { Player player = event.getPlayer(); - if (player.isSneaking() && instance.getHopperManager().isReady()) { + if (player.isSneaking() && plugin.getHopperManager().isReady()) { Location location = player.getLocation().getBlock().getRelative(BlockFace.SELF).getLocation(); Location down = location.getBlock().getRelative(BlockFace.DOWN).getLocation(); - if (instance.getHopperManager().isHopper(down)) { - Hopper hopper = instance.getHopperManager().getHopper(down); + if (plugin.getHopperManager().isHopper(down)) { + Hopper hopper = plugin.getHopperManager().getHopper(down); if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK) - instance.getTeleportHandler().tpEntity(player, hopper); - } else if (instance.getHopperManager().isHopper(location)) { - Hopper hopper = instance.getHopperManager().getHopper(location); + plugin.getTeleportHandler().tpEntity(player, hopper); + } else if (plugin.getHopperManager().isHopper(location)) { + Hopper hopper = plugin.getHopperManager().getHopper(location); if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK) - instance.getTeleportHandler().tpEntity(player, hopper); + plugin.getTeleportHandler().tpEntity(player, hopper); } } } @@ -66,7 +66,7 @@ public class InteractListeners implements Listener { return; if (Settings.USE_PROTECTION_PLUGINS.getBoolean() && !ProtectionManager.canInteract(player, event.getClickedBlock().getLocation())) { - player.sendMessage(instance.getLocale().getMessage("event.general.protected").getPrefixedMessage()); + player.sendMessage(plugin.getLocale().getMessage("event.general.protected").getPrefixedMessage()); return; } @@ -80,22 +80,22 @@ public class InteractListeners implements Listener { return; } - PlayerData playerData = instance.getPlayerDataManager().getPlayerData(player); + PlayerData playerData = plugin.getPlayerDataManager().getPlayerData(player); if (playerData.getSyncType() == null) { if (event.getClickedBlock().getType() == Material.HOPPER) { - if (!instance.getHopperManager().isReady()) { - player.sendMessage(instance.getLocale().getMessage("event.hopper.notready").getMessage()); + if (!plugin.getHopperManager().isReady()) { + player.sendMessage(plugin.getLocale().getMessage("event.hopper.notready").getMessage()); event.setCancelled(true); return; } - if (Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(event.getClickedBlock().getLocation())) + if (Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getHopperManager().isHopper(event.getClickedBlock().getLocation())) return; - Hopper hopper = instance.getHopperManager().getHopper(event.getClickedBlock()); + Hopper hopper = plugin.getHopperManager().getHopper(event.getClickedBlock()); if (!player.getInventory().getItemInHand().getType().name().contains("PICKAXE")) { - hopper.overview(instance.getGuiManager(), player); + hopper.overview(plugin.getGuiManager(), player); event.setCancelled(true); return; } @@ -103,13 +103,15 @@ public class InteractListeners implements Listener { return; } - if (event.getClickedBlock().getState() instanceof InventoryHolder || (event.getClickedBlock().getType().equals(Material.ENDER_CHEST) && instance.getConfig().getBoolean("Main.Support Enderchests"))) { + if (event.getClickedBlock().getState() instanceof InventoryHolder + || (event.getClickedBlock().getType().equals(Material.ENDER_CHEST) + && Settings.ENDERCHESTS.getBoolean())) { Hopper hopper = playerData.getLastHopper(); if (event.getClickedBlock().getLocation().equals(playerData.getLastHopper().getLocation())) { if (!hopper.getLinkedBlocks().isEmpty()) - instance.getLocale().getMessage("event.hopper.syncfinish").sendPrefixedMessage(player); + plugin.getLocale().getMessage("event.hopper.syncfinish").sendPrefixedMessage(player); else - instance.getLocale().getMessage("event.hopper.synccanceled").sendPrefixedMessage(player); + plugin.getLocale().getMessage("event.hopper.synccanceled").sendPrefixedMessage(player); hopper.cancelSync(player); } else if (playerData.getSyncType() != null) { hopper.link(event.getClickedBlock(), playerData.getSyncType() == SyncType.FILTERED, player); diff --git a/src/main/java/com/songoda/epichoppers/settings/Settings.java b/src/main/java/com/songoda/epichoppers/settings/Settings.java index f6d6fac..d301d19 100644 --- a/src/main/java/com/songoda/epichoppers/settings/Settings.java +++ b/src/main/java/com/songoda/epichoppers/settings/Settings.java @@ -36,7 +36,7 @@ public class Settings { public static final ConfigSetting ENDERCHESTS = new ConfigSetting(config, "Main.Support Enderchests", true, "Should hoppers dump items into a player enderchests?"); - public static final ConfigSetting PARTICLE_TYPE = new ConfigSetting(config, "Main.Upgrade Particle Type", "SPELL_WITCH", + public static final ConfigSetting UPGRADE_PARTICLE_TYPE = new ConfigSetting(config, "Main.Upgrade Particle Type", "SPELL_WITCH", "The type of particle shown when a hopper is upgraded."); public static final ConfigSetting HOP_TICKS = new ConfigSetting(config, "Main.Amount of Ticks Between Hops", 8L, @@ -52,7 +52,7 @@ public class Settings { "The cooldown between teleports. It prevents players", "from getting stuck in a teleport loop."); - public static final ConfigSetting SYNC_TIMEOUT = new ConfigSetting(config, "Main.Timeout When Syncing Hoppers", 300L, + public static final ConfigSetting LINK_TIMEOUT = new ConfigSetting(config, "Main.Timeout When Syncing Hoppers", 300L, "The amount of time in ticks a player has between hitting the hopper", "Link button and performing the link. When the time is up the link event is canceled."); diff --git a/src/main/java/com/songoda/epichoppers/utils/CostType.java b/src/main/java/com/songoda/epichoppers/utils/CostType.java index a615d55..0b0627c 100644 --- a/src/main/java/com/songoda/epichoppers/utils/CostType.java +++ b/src/main/java/com/songoda/epichoppers/utils/CostType.java @@ -1,18 +1,7 @@ package com.songoda.epichoppers.utils; -/** - * Represents a cost type when making a purchase from EpicSpawners - */ public enum CostType { - /** - * A purchase made with an economy balance (generally an implementation of Vault) - */ - ECONOMY, - - /** - * A purchase made with a player's experience levels - */ - EXPERIENCE + ECONOMY, EXPERIENCE } \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/HopperDirection.java b/src/main/java/com/songoda/epichoppers/utils/HopperDirection.java index 3299be6..a1f38a3 100644 --- a/src/main/java/com/songoda/epichoppers/utils/HopperDirection.java +++ b/src/main/java/com/songoda/epichoppers/utils/HopperDirection.java @@ -11,12 +11,12 @@ public enum HopperDirection { WEST(4, 12, -1, 0, 0), EAST(5, 13, 1, 0, 0); - private int unpowered; - private int powered; + private final int unpowered; + private final int powered; - private int x; - private int y; - private int z; + private final int x; + private final int y; + private final int z; HopperDirection(int unpowered, int powered, int x, int y, int z) { this.unpowered = unpowered; @@ -28,10 +28,9 @@ public enum HopperDirection { } public static HopperDirection getDirection(int value) { - for (HopperDirection hopperDirection : HopperDirection.values()) { - if (hopperDirection.getPowered() == value - || hopperDirection.getUnpowered() == value) return hopperDirection; - } + for (HopperDirection hopperDirection : HopperDirection.values()) + if (hopperDirection.getPowered() == value || hopperDirection.getUnpowered() == value) + return hopperDirection; return null; } diff --git a/src/main/java/com/songoda/epichoppers/utils/TeleportTrigger.java b/src/main/java/com/songoda/epichoppers/utils/TeleportTrigger.java deleted file mode 100644 index f70b0d7..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/TeleportTrigger.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.songoda.epichoppers.utils; - -public enum TeleportTrigger { - DISABLED, - WALK_ON, - SNEAK -} From 91cbbb4adeec52dc2860047beb51eed8f658dfbd Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 1 Jun 2021 13:38:44 -0500 Subject: [PATCH 8/8] version 4.6.21 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2e32ec4..40f780b 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ com.songoda EpicHoppers 4.0.0 - 4.6.20 + 4.6.21 clean install EpicHoppers-${project.version}