diff --git a/Hook_WildStacker/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/listeners/WildStackerListener.java b/Hook_WildStacker/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/listeners/WildStackerListener.java index e4d9fd9f3..639026bf0 100644 --- a/Hook_WildStacker/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/listeners/WildStackerListener.java +++ b/Hook_WildStacker/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/listeners/WildStackerListener.java @@ -1,14 +1,22 @@ package com.bgsoftware.superiorskyblock.module.upgrades.listeners; +import com.bgsoftware.superiorskyblock.module.BuiltinModules; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeSpawnerRates; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import javax.annotation.Nullable; + public final class WildStackerListener implements Listener { + @Nullable + private final UpgradeTypeSpawnerRates spawnerRates = BuiltinModules.UPGRADES + .getEnabledUpgradeType(UpgradeTypeSpawnerRates.class); + @EventHandler public void onWildStackerStackSpawn(com.bgsoftware.wildstacker.api.events.SpawnerStackedEntitySpawnEvent e) { - if (UpgradesListener.IMP != null) - UpgradesListener.IMP.handleSpawnerSpawn(e.getSpawner()); + if (spawnerRates != null) + spawnerRates.handleSpawnerSpawn(e.getSpawner()); } } diff --git a/src/main/java/com/bgsoftware/superiorskyblock/commands/admin/CmdAdminShow.java b/src/main/java/com/bgsoftware/superiorskyblock/commands/admin/CmdAdminShow.java index 87f218d08..90436e060 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/commands/admin/CmdAdminShow.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/commands/admin/CmdAdminShow.java @@ -10,6 +10,12 @@ import com.bgsoftware.superiorskyblock.key.Key; import com.bgsoftware.superiorskyblock.module.BuiltinModules; import com.bgsoftware.superiorskyblock.lang.PlayerLocales; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeBlockLimits; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeCropGrowth; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeEntityLimits; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeIslandEffects; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeMobDrops; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeSpawnerRates; import com.bgsoftware.superiorskyblock.utils.StringUtils; import com.bgsoftware.superiorskyblock.utils.islands.IslandUtils; import com.bgsoftware.superiorskyblock.wrappers.SBlockPosition; @@ -135,7 +141,9 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, Superio if (!Message.ISLAND_INFO_PAYPAL.isEmpty(locale)) infoMessage.append(Message.ISLAND_INFO_PAYPAL.getMessage(locale, island.getPaypal())).append("\n"); - if (BuiltinModules.UPGRADES.isEnabled()) { + boolean upgradesModule = BuiltinModules.UPGRADES.isEnabled(); + + if (upgradesModule) { // Island upgrades if (!Message.ISLAND_INFO_ADMIN_UPGRADES.isEmpty(locale) && !Message.ISLAND_INFO_ADMIN_UPGRADE_LINE.isEmpty(locale)) { StringBuilder upgradesString = new StringBuilder(); @@ -186,9 +194,10 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, Superio infoMessage.append("\n"); } - if (BuiltinModules.UPGRADES.isEnabled()) { + if (upgradesModule) { // Island spawners multiplier - if (!Message.ISLAND_INFO_ADMIN_SPAWNERS_MULTIPLIER.isEmpty(locale)) { + if (!Message.ISLAND_INFO_ADMIN_SPAWNERS_MULTIPLIER.isEmpty(locale) && + BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeSpawnerRates.class)) { infoMessage.append(Message.ISLAND_INFO_ADMIN_SPAWNERS_MULTIPLIER.getMessage(locale, island.getSpawnerRatesMultiplier())); if (island.getSpawnerRatesRaw() != island.getSpawnerRatesMultiplier()) infoMessage.append(" ").append(Message.ISLAND_INFO_ADMIN_VALUE_SYNCED.getMessage(locale)); @@ -196,7 +205,8 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, Superio } // Island drops multiplier - if (!Message.ISLAND_INFO_ADMIN_DROPS_MULTIPLIER.isEmpty(locale)) { + if (!Message.ISLAND_INFO_ADMIN_DROPS_MULTIPLIER.isEmpty(locale) && + BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeMobDrops.class)) { infoMessage.append(Message.ISLAND_INFO_ADMIN_DROPS_MULTIPLIER.getMessage(locale, island.getMobDropsMultiplier())); if (island.getMobDropsRaw() != island.getMobDropsMultiplier()) infoMessage.append(" ").append(Message.ISLAND_INFO_ADMIN_VALUE_SYNCED.getMessage(locale)); @@ -204,36 +214,41 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, Superio } // Island crops multiplier - if (!Message.ISLAND_INFO_ADMIN_CROPS_MULTIPLIER.isEmpty(locale)) { + if (!Message.ISLAND_INFO_ADMIN_CROPS_MULTIPLIER.isEmpty(locale) && + BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeCropGrowth.class)) { infoMessage.append(Message.ISLAND_INFO_ADMIN_CROPS_MULTIPLIER.getMessage(locale, island.getCropGrowthMultiplier())); if (island.getCropGrowthRaw() != island.getCropGrowthMultiplier()) infoMessage.append(" ").append(Message.ISLAND_INFO_ADMIN_VALUE_SYNCED.getMessage(locale)); infoMessage.append("\n"); } - } - // Island entity limits - if (!Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS.isEmpty(locale) && !Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS_LINE.isEmpty(locale)) { - StringBuilder entitiesString = new StringBuilder(); - for (Map.Entry entry : island.getEntitiesLimitsAsKeys().entrySet()) { - entitiesString.append(Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS_LINE.getMessage(locale, StringUtils.format(entry.getKey().toString()), entry.getValue())); - if (!island.getCustomEntitiesLimits().containsKey(entry.getKey())) - entitiesString.append(" ").append(Message.ISLAND_INFO_ADMIN_VALUE_SYNCED.getMessage(locale)); - entitiesString.append("\n"); + // Island entity limits + if (!Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS.isEmpty(locale) && + !Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS_LINE.isEmpty(locale) && + BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeEntityLimits.class)) { + StringBuilder entitiesString = new StringBuilder(); + for (Map.Entry entry : island.getEntitiesLimitsAsKeys().entrySet()) { + entitiesString.append(Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS_LINE.getMessage(locale, StringUtils.format(entry.getKey().toString()), entry.getValue())); + if (!island.getCustomEntitiesLimits().containsKey(entry.getKey())) + entitiesString.append(" ").append(Message.ISLAND_INFO_ADMIN_VALUE_SYNCED.getMessage(locale)); + entitiesString.append("\n"); + } + infoMessage.append(Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS.getMessage(locale, entitiesString)); } - infoMessage.append(Message.ISLAND_INFO_ADMIN_ENTITIES_LIMITS.getMessage(locale, entitiesString)); - } - // Island block limits - if (!Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS.isEmpty(locale) && !Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS_LINE.isEmpty(locale)) { - StringBuilder blocksString = new StringBuilder(); - for (Map.Entry entry : island.getBlocksLimits().entrySet()) { - blocksString.append(Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS_LINE.getMessage(locale, StringUtils.format(entry.getKey().toString()), entry.getValue())); - if (!island.getCustomBlocksLimits().containsKey(entry.getKey())) - blocksString.append(" ").append(Message.ISLAND_INFO_ADMIN_VALUE_SYNCED.getMessage(locale)); - blocksString.append("\n"); + // Island block limits + if (!Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS.isEmpty(locale) && + !Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS_LINE.isEmpty(locale) && + BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeBlockLimits.class)) { + StringBuilder blocksString = new StringBuilder(); + for (Map.Entry entry : island.getBlocksLimits().entrySet()) { + blocksString.append(Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS_LINE.getMessage(locale, StringUtils.format(entry.getKey().toString()), entry.getValue())); + if (!island.getCustomBlocksLimits().containsKey(entry.getKey())) + blocksString.append(" ").append(Message.ISLAND_INFO_ADMIN_VALUE_SYNCED.getMessage(locale)); + blocksString.append("\n"); + } + infoMessage.append(Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS.getMessage(locale, blocksString)); } - infoMessage.append(Message.ISLAND_INFO_ADMIN_BLOCKS_LIMITS.getMessage(locale, blocksString)); } if (BuiltinModules.GENERATORS.isEnabled()) { @@ -259,9 +274,11 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, Superio } } - if (BuiltinModules.UPGRADES.isEnabled()) { + if (upgradesModule) { // Island effects - if (!Message.ISLAND_INFO_ADMIN_ISLAND_EFFECTS.isEmpty(locale) && !Message.ISLAND_INFO_ADMIN_ISLAND_EFFECTS_LINE.isEmpty(locale)) { + if (!Message.ISLAND_INFO_ADMIN_ISLAND_EFFECTS.isEmpty(locale) && + !Message.ISLAND_INFO_ADMIN_ISLAND_EFFECTS_LINE.isEmpty(locale) && + BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeIslandEffects.class)) { StringBuilder blocksString = new StringBuilder(); for (Map.Entry entry : island.getPotionEffects().entrySet()) { blocksString.append(Message.ISLAND_INFO_ADMIN_ISLAND_EFFECTS_LINE.getMessage(locale, StringUtils.format(entry.getKey().getName()), entry.getValue())).append("\n"); diff --git a/src/main/java/com/bgsoftware/superiorskyblock/island/SIsland.java b/src/main/java/com/bgsoftware/superiorskyblock/island/SIsland.java index 52d84949d..8b9bf0b0e 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/island/SIsland.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/island/SIsland.java @@ -37,6 +37,8 @@ import com.bgsoftware.superiorskyblock.menu.SuperiorMenu; import com.bgsoftware.superiorskyblock.mission.MissionData; import com.bgsoftware.superiorskyblock.module.BuiltinModules; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeCropGrowth; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeIslandEffects; import com.bgsoftware.superiorskyblock.structure.CompletableFutureList; import com.bgsoftware.superiorskyblock.threads.Executor; import com.bgsoftware.superiorskyblock.threads.SyncedObject; @@ -1384,15 +1386,21 @@ public void setIslandSize(int islandSize) { PluginDebugger.debug("Action: Set Size, Island: " + owner.getName() + ", Size: " + islandSize); - // First, we want to remove all the current crop tile entities - getLoadedChunks(true, false).forEach(chunk -> - plugin.getNMSChunks().startTickingChunk(this, chunk, true)); + boolean cropGrowthEnabled = BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeCropGrowth.class); + + if (cropGrowthEnabled) { + // First, we want to remove all the current crop tile entities + getLoadedChunks(true, false).forEach(chunk -> + plugin.getNMSChunks().startTickingChunk(this, chunk, true)); + } this.islandSize = new UpgradeValue<>(islandSize, false); - // Now, we want to update the tile entities again - getLoadedChunks(true, false).forEach(chunk -> - plugin.getNMSChunks().startTickingChunk(this, chunk, false)); + if (cropGrowthEnabled) { + // Now, we want to update the tile entities again + getLoadedChunks(true, false).forEach(chunk -> + plugin.getNMSChunks().startTickingChunk(this, chunk, false)); + } IslandsDatabaseBridge.saveSize(this); } @@ -2336,22 +2344,34 @@ public Map getPotionEffects() { @Override public void applyEffects(SuperiorPlayer superiorPlayer) { Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null."); + + if (!BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeIslandEffects.class)) + return; + Player player = superiorPlayer.asPlayer(); - if (player != null) - getPotionEffects().forEach((potionEffectType, level) -> player.addPotionEffect(new PotionEffect(potionEffectType, Integer.MAX_VALUE, level - 1), true)); + if (player != null) { + getPotionEffects().forEach((potionEffectType, level) -> player.addPotionEffect( + new PotionEffect(potionEffectType, Integer.MAX_VALUE, level - 1), true)); + } } @Override public void removeEffects(SuperiorPlayer superiorPlayer) { Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null."); - Player player = superiorPlayer.asPlayer(); - if (player != null) - getPotionEffects().keySet().forEach(player::removePotionEffect); + if (BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeIslandEffects.class)) + removeEffectsNoUpgradeCheck(superiorPlayer); } @Override public void removeEffects() { - getAllPlayersInside().forEach(this::removeEffects); + if (BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeIslandEffects.class)) + getAllPlayersInside().forEach(this::removeEffectsNoUpgradeCheck); + } + + private void removeEffectsNoUpgradeCheck(SuperiorPlayer superiorPlayer) { + Player player = superiorPlayer.asPlayer(); + if (player != null) + getPotionEffects().keySet().forEach(player::removePotionEffect); } @Override diff --git a/src/main/java/com/bgsoftware/superiorskyblock/listeners/BlocksListener.java b/src/main/java/com/bgsoftware/superiorskyblock/listeners/BlocksListener.java index 7ea3f65f0..1e6db134a 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/listeners/BlocksListener.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/listeners/BlocksListener.java @@ -1,18 +1,20 @@ package com.bgsoftware.superiorskyblock.listeners; -import com.bgsoftware.superiorskyblock.lang.Message; +import com.bgsoftware.common.reflection.ReflectMethod; import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; import com.bgsoftware.superiorskyblock.api.island.Island; import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer; import com.bgsoftware.superiorskyblock.key.ConstantKeys; import com.bgsoftware.superiorskyblock.key.Key; +import com.bgsoftware.superiorskyblock.lang.Message; import com.bgsoftware.superiorskyblock.menu.StackedBlocksDepositMenu; +import com.bgsoftware.superiorskyblock.threads.Executor; import com.bgsoftware.superiorskyblock.utils.LocationUtils; -import com.bgsoftware.superiorskyblock.utils.logic.ProtectionLogic; -import com.bgsoftware.superiorskyblock.world.chunks.ChunksTracker; +import com.bgsoftware.superiorskyblock.utils.ServerVersion; import com.bgsoftware.superiorskyblock.utils.logic.BlocksLogic; +import com.bgsoftware.superiorskyblock.utils.logic.ProtectionLogic; import com.bgsoftware.superiorskyblock.utils.logic.StackedBlocksLogic; -import com.bgsoftware.superiorskyblock.threads.Executor; +import com.bgsoftware.superiorskyblock.world.chunks.ChunksTracker; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -21,6 +23,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.FallingBlock; import org.bukkit.entity.IronGolem; +import org.bukkit.entity.Minecart; import org.bukkit.entity.Snowman; import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.Wither; @@ -47,21 +50,21 @@ import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.vehicle.VehicleDestroyEvent; import org.bukkit.event.world.StructureGrowEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; -import java.util.UUID; @SuppressWarnings("unused") public final class BlocksListener implements Listener { - private final SuperiorSkyblockPlugin plugin; + private static final ReflectMethod INTERACT_GET_HAND = new ReflectMethod<>( + PlayerInteractEvent.class, "getHand"); - private final Set recentlyClicked = new HashSet<>(); + private final SuperiorSkyblockPlugin plugin; public BlocksListener(SuperiorSkyblockPlugin plugin) { this.plugin = plugin; @@ -327,8 +330,10 @@ public void onBlockUnstack(BlockBreakEvent e) { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockUnstack(PlayerInteractEvent e) { - if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getItem() != null || - recentlyClicked.contains(e.getPlayer().getUniqueId())) + if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getItem() != null) + return; + + if (INTERACT_GET_HAND.isValid() && INTERACT_GET_HAND.invoke(e) != EquipmentSlot.HAND) return; if (plugin.getStackedBlocks().getStackedBlockAmount(e.getClickedBlock()) <= 1) @@ -337,13 +342,9 @@ public void onBlockUnstack(PlayerInteractEvent e) { if (plugin.getSettings().getStackedBlocks().getDepositMenu().isEnabled() && e.getPlayer().isSneaking()) { StackedBlocksDepositMenu depositMenu = new StackedBlocksDepositMenu(e.getClickedBlock().getLocation()); e.getPlayer().openInventory(depositMenu.getInventory()); - } else { - recentlyClicked.add(e.getPlayer().getUniqueId()); - Executor.sync(() -> recentlyClicked.remove(e.getPlayer().getUniqueId()), 5L); - - if (!ProtectionLogic.handleBlockBreak(e.getClickedBlock(), e.getPlayer(), true) || - StackedBlocksLogic.tryUnstack(e.getPlayer(), e.getClickedBlock(), plugin)) - e.setCancelled(true); + } else if (!ProtectionLogic.handleBlockBreak(e.getClickedBlock(), e.getPlayer(), true) || + StackedBlocksLogic.tryUnstack(e.getPlayer(), e.getClickedBlock(), plugin)) { + e.setCancelled(true); } } @@ -451,6 +452,58 @@ public void onBlockChangeState(BlockFormEvent e) { e.setCancelled(true); } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onCartPlaceMonitor(PlayerInteractEvent e) { + if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getItem() == null || + !e.getClickedBlock().getType().name().contains("RAIL") || + !e.getItem().getType().name().contains("MINECART")) + return; + + if (INTERACT_GET_HAND.isValid() && INTERACT_GET_HAND.invoke(e) != EquipmentSlot.HAND) + return; + + Island island = plugin.getGrid().getIslandAt(e.getClickedBlock().getLocation()); + + if (island == null) + return; + + switch (e.getItem().getType().name()) { + case "HOPPER_MINECART": + island.handleBlockPlace(ConstantKeys.HOPPER, 1); + break; + case "COMMAND_MINECART": + case "COMMAND_BLOCK_MINECART": + island.handleBlockPlace(ServerVersion.isAtLeast(ServerVersion.v1_13) ? + ConstantKeys.COMMAND_BLOCK : ConstantKeys.COMMAND, 1); + break; + case "EXPLOSIVE_MINECART": + case "TNT_MINECART": + island.handleBlockPlace(ConstantKeys.TNT, 1); + break; + case "POWERED_MINECART": + case "FURNACE_MINECART": + island.handleBlockPlace(ConstantKeys.FURNACE, 1); + break; + case "STORAGE_MINECART": + case "CHEST_MINECART": + island.handleBlockPlace(ConstantKeys.CHEST, 1); + break; + } + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onCartBreakMonitor(VehicleDestroyEvent e) { + if (!(e.getVehicle() instanceof Minecart)) + return; + + Island island = plugin.getGrid().getIslandAt(e.getVehicle().getLocation()); + + if (island == null) + return; + + island.handleBlockBreak(plugin.getNMSAlgorithms().getMinecartBlock((Minecart) e.getVehicle()), 1); + } + /* * Island Warps */ diff --git a/src/main/java/com/bgsoftware/superiorskyblock/listeners/ChunksListener.java b/src/main/java/com/bgsoftware/superiorskyblock/listeners/ChunksListener.java index f58775c7c..e7ea779bb 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/listeners/ChunksListener.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/listeners/ChunksListener.java @@ -1,11 +1,9 @@ package com.bgsoftware.superiorskyblock.listeners; import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; -import com.bgsoftware.superiorskyblock.api.island.Island; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockGrowEvent; import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkUnloadEvent; @@ -28,12 +26,4 @@ public void onChunkLoad(ChunkLoadEvent e) { plugin.getWorldEventsManager().loadChunk(e.getChunk()); } - // Should potentially fix crop growth tile entities "disappearing" - @EventHandler(priority = EventPriority.LOWEST) - public void onBlockGrow(BlockGrowEvent e) { - Island island = plugin.getGrid().getIslandAt(e.getBlock().getLocation()); - if (island != null && island.isInsideRange(e.getBlock().getLocation())) - plugin.getNMSChunks().startTickingChunk(island, e.getBlock().getChunk(), false); - } - } diff --git a/src/main/java/com/bgsoftware/superiorskyblock/listeners/PlayersListener.java b/src/main/java/com/bgsoftware/superiorskyblock/listeners/PlayersListener.java index da4964f2c..e08ad29e9 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/listeners/PlayersListener.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/listeners/PlayersListener.java @@ -48,7 +48,6 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityPortalEnterEvent; -import org.bukkit.event.entity.EntityPotionEffectEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerChangedWorldEvent; @@ -87,12 +86,6 @@ public PlayersListener(SuperiorSkyblockPlugin plugin) { String fileName = plugin.getFileName().split("\\.")[0]; String buildName = fileName.contains("-") ? fileName.substring(fileName.indexOf('-') + 1) : ""; this.buildName = buildName.isEmpty() ? "" : " (Build: " + buildName + ")"; - - try { - Class.forName("org.bukkit.event.entity.EntityPotionEffectEvent"); - Bukkit.getPluginManager().registerEvents(new EffectsListener(), plugin); - } catch (Throwable ignored) { - } } @EventHandler @@ -633,26 +626,4 @@ public void onIslandChestInteract(InventoryClickEvent e) { } } - private final class EffectsListener implements Listener { - - @EventHandler(ignoreCancelled = true) - public void onPlayerEffect(EntityPotionEffectEvent e) { - if (e.getAction() == EntityPotionEffectEvent.Action.ADDED || !(e.getEntity() instanceof Player) || - e.getCause() == EntityPotionEffectEvent.Cause.PLUGIN) - return; - - Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation()); - - if (island == null) - return; - - int islandEffectLevel = island.getPotionEffectLevel(e.getModifiedType()); - - if (islandEffectLevel > 0 && (e.getOldEffect() == null || e.getOldEffect().getAmplifier() == islandEffectLevel)) { - e.setCancelled(true); - } - } - - } - } diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/UpgradesModule.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/UpgradesModule.java index 3b57ea4c9..050245727 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/UpgradesModule.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/UpgradesModule.java @@ -10,20 +10,15 @@ import com.bgsoftware.superiorskyblock.api.upgrades.cost.UpgradeCostLoader; import com.bgsoftware.superiorskyblock.key.dataset.KeyMap; import com.bgsoftware.superiorskyblock.module.BuiltinModule; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddCropGrowth; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddEffect; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddMobDrops; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddSpawnerRates; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminRankup; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetCropGrowth; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetEffect; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetMobDrops; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetSpawnerRates; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetUpgrade; -import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSyncUpgrades; import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdRankup; import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdUpgrade; -import com.bgsoftware.superiorskyblock.module.upgrades.listeners.UpgradesListener; +import com.bgsoftware.superiorskyblock.module.upgrades.type.IUpgradeType; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeBlockLimits; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeCropGrowth; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeEntityLimits; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeIslandEffects; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeMobDrops; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeSpawnerRates; import com.bgsoftware.superiorskyblock.upgrade.SUpgrade; import com.bgsoftware.superiorskyblock.upgrade.SUpgradeLevel; import com.bgsoftware.superiorskyblock.upgrade.UpgradeValue; @@ -34,8 +29,10 @@ import org.bukkit.event.Listener; import org.bukkit.potion.PotionEffectType; +import javax.annotation.Nullable; import java.io.File; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -46,35 +43,16 @@ public final class UpgradesModule extends BuiltinModule { private static final int MAX_UPGRADES_NAME_LENGTH = 255; - private boolean enabled = false; + private final List enabledUpgrades = new ArrayList<>(); public UpgradesModule() { super("upgrades"); } - @SuppressWarnings("ResultOfMethodCallIgnored") @Override protected void onPluginInit(SuperiorSkyblockPlugin plugin) { super.onPluginInit(plugin); - - File upgradesFile = new File(plugin.getDataFolder(), "upgrades.yml"); - - if (upgradesFile.exists()) { - CommentedConfiguration config = CommentedConfiguration.loadConfiguration(upgradesFile); - super.config.set("upgrades", config.get("upgrades")); - - File moduleConfigFile = new File(getDataFolder(), "config.yml"); - - try { - super.config.save(moduleConfigFile); - config.save(upgradesFile); - } catch (Exception ex) { - ex.printStackTrace(); - PluginDebugger.debug(ex); - } - - upgradesFile.delete(); - } + convertOldUpgradesFile(plugin); } @Override @@ -87,138 +65,194 @@ public void onDisable(SuperiorSkyblockPlugin plugin) { @Override public Listener[] getModuleListeners(SuperiorSkyblockPlugin plugin) { - return !enabled ? null : new Listener[]{new UpgradesListener(plugin)}; + return !isEnabled() ? null : enabledUpgrades.stream() + .map(IUpgradeType::getListener) + .toArray(Listener[]::new); } @Override public SuperiorCommand[] getSuperiorCommands(SuperiorSkyblockPlugin plugin) { - return !enabled ? null : new SuperiorCommand[]{new CmdRankup(), new CmdUpgrade()}; + return !isEnabled() ? null : new SuperiorCommand[]{new CmdRankup(), new CmdUpgrade()}; } @Override public SuperiorCommand[] getSuperiorAdminCommands(SuperiorSkyblockPlugin plugin) { - return !enabled ? null : new SuperiorCommand[]{ - new CmdAdminAddCropGrowth(), new CmdAdminAddEffect(), new CmdAdminAddMobDrops(), - new CmdAdminAddSpawnerRates(), new CmdAdminRankup(), new CmdAdminSetCropGrowth(), - new CmdAdminSetEffect(), new CmdAdminSetMobDrops(), new CmdAdminSetSpawnerRates(), - new CmdAdminSetUpgrade(), new CmdAdminSyncUpgrades() - }; + return !isEnabled() ? null : enabledUpgrades.stream() + .map(IUpgradeType::getCommands) + .flatMap(List::stream) + .toArray(SuperiorCommand[]::new); } @Override public boolean isEnabled() { - return enabled && isInitialized(); + return !enabledUpgrades.isEmpty() && isInitialized(); + } + + @Override + protected String[] getIgnoredSections() { + return new String[]{"upgrades"}; } @Override protected void updateConfig(SuperiorSkyblockPlugin plugin) { - enabled = config.getBoolean("enabled"); + enabledUpgrades.clear(); - if (enabled) { - ConfigurationSection upgrades = config.getConfigurationSection("upgrades"); - for (String upgradeName : upgrades.getKeys(false)) { - if (upgradeName.length() > MAX_UPGRADES_NAME_LENGTH) - upgradeName = upgradeName.substring(0, MAX_UPGRADES_NAME_LENGTH); + if (config.getBoolean("crop-growth", true)) + enabledUpgrades.add(new UpgradeTypeCropGrowth(plugin)); + if (config.getBoolean("mob-drops", true)) + enabledUpgrades.add(new UpgradeTypeMobDrops(plugin)); + if (config.getBoolean("island-effects", true)) + enabledUpgrades.add(new UpgradeTypeIslandEffects(plugin)); + if (config.getBoolean("spawner-rates", true)) + enabledUpgrades.add(new UpgradeTypeSpawnerRates(plugin)); + if (config.getBoolean("block-limits", true)) + enabledUpgrades.add(new UpgradeTypeBlockLimits(plugin)); + if (config.getBoolean("entity-limits", true)) + enabledUpgrades.add(new UpgradeTypeEntityLimits(plugin)); - SUpgrade upgrade = new SUpgrade(upgradeName); - for (String _level : upgrades.getConfigurationSection(upgradeName).getKeys(false)) { - ConfigurationSection levelSection = upgrades.getConfigurationSection(upgradeName + "." + _level); - int level = Integer.parseInt(_level); + if (enabledUpgrades.isEmpty()) + return; - String priceType = levelSection.getString("price-type", "money"); - UpgradeCostLoader costLoader = plugin.getUpgrades().getUpgradeCostLoader(priceType); + ConfigurationSection upgrades = config.getConfigurationSection("upgrades"); - if (costLoader == null) { - SuperiorSkyblockPlugin.log("&cUpgrade by name " + upgrade.getName() + " (level " + level + ") has invalid price-type. Skipping..."); - continue; - } + if (upgrades == null) + return; - UpgradeCost upgradeCost; + for (String upgradeName : upgrades.getKeys(false)) { + if (upgradeName.length() > MAX_UPGRADES_NAME_LENGTH) + upgradeName = upgradeName.substring(0, MAX_UPGRADES_NAME_LENGTH); - try { - upgradeCost = costLoader.loadCost(levelSection); - } catch (UpgradeCostLoadException ex) { - SuperiorSkyblockPlugin.log("&cUpgrade by name " + upgrade.getName() + " (level " + level + ") failed to initialize because: " + ex.getMessage() + ". Skipping..."); - PluginDebugger.debug(ex); - continue; - } + SUpgrade upgrade = new SUpgrade(upgradeName); + for (String _level : upgrades.getConfigurationSection(upgradeName).getKeys(false)) { + loadUpgradeLevelFromSection(plugin, upgrade, _level, upgrades.getConfigurationSection(upgradeName + "." + _level)); + } - List commands = levelSection.getStringList("commands"); - String permission = levelSection.getString("permission", ""); - Set> requirements = new HashSet<>(); - for (String line : levelSection.getStringList("required-checks")) { - String[] sections = line.split(";"); - requirements.add(new Pair<>(sections[0], StringUtils.translateColors(sections[1]))); - } - UpgradeValue cropGrowth = new UpgradeValue<>(levelSection.getDouble("crop-growth", -1D), true); - UpgradeValue spawnerRates = new UpgradeValue<>(levelSection.getDouble("spawner-rates", -1D), true); - UpgradeValue mobDrops = new UpgradeValue<>(levelSection.getDouble("mob-drops", -1D), true); - UpgradeValue teamLimit = new UpgradeValue<>(levelSection.getInt("team-limit", -1), true); - UpgradeValue warpsLimit = new UpgradeValue<>(levelSection.getInt("warps-limit", -1), true); - UpgradeValue coopLimit = new UpgradeValue<>(levelSection.getInt("coop-limit", -1), true); - UpgradeValue borderSize = new UpgradeValue<>(levelSection.getInt("border-size", -1), true); - UpgradeValue bankLimit = new UpgradeValue<>(new BigDecimal(levelSection.getString("bank-limit", "-1")), true); - KeyMap blockLimits = new KeyMap<>(); - if (levelSection.contains("block-limits")) { - for (String block : levelSection.getConfigurationSection("block-limits").getKeys(false)) { - blockLimits.put(block, levelSection.getInt("block-limits." + block)); - plugin.getBlockValues().addCustomBlockKey(Key.of(block)); - } - } - KeyMap entityLimits = new KeyMap<>(); - if (levelSection.contains("entity-limits")) { - for (String entity : levelSection.getConfigurationSection("entity-limits").getKeys(false)) - entityLimits.put(entity.toUpperCase(), levelSection.getInt("entity-limits." + entity)); - } - KeyMap[] generatorRates = new KeyMap[World.Environment.values().length]; - if (levelSection.contains("generator-rates")) { - for (String blockOrEnv : levelSection.getConfigurationSection("generator-rates").getKeys(false)) { - try { - int index = World.Environment.valueOf(blockOrEnv.toUpperCase()).ordinal(); - for (String block : levelSection.getConfigurationSection("generator-rates." + blockOrEnv).getKeys(false)) { - if (generatorRates[index] == null) - generatorRates[index] = new KeyMap<>(); - generatorRates[index].put(block, levelSection.getInt("generator-rates." + blockOrEnv + "." + block)); - } - } catch (Exception ex) { - if (generatorRates[0] == null) - generatorRates[0] = new KeyMap<>(); - generatorRates[0].put(blockOrEnv, levelSection.getInt("generator-rates." + blockOrEnv)); - } - } - } - Map islandEffects = new HashMap<>(); - if (levelSection.contains("island-effects")) { - for (String effect : levelSection.getConfigurationSection("island-effects").getKeys(false)) { - PotionEffectType potionEffectType = PotionEffectType.getByName(effect); - if (potionEffectType != null) - islandEffects.put(potionEffectType, levelSection.getInt("island-effects." + effect) - 1); - } - } - Map rolesLimits = new HashMap<>(); - if (levelSection.contains("role-limits")) { - for (String roleId : levelSection.getConfigurationSection("role-limits").getKeys(false)) { - try { - rolesLimits.put(Integer.parseInt(roleId), levelSection.getInt("role-limits." + roleId)); - } catch (NumberFormatException error) { - PluginDebugger.debug(error); - } - } + plugin.getUpgrades().addUpgrade(upgrade); + } + } + + @Nullable + public T getEnabledUpgradeType(Class clazz) { + return enabledUpgrades.stream() + .filter(upgradeType -> upgradeType.getClass().equals(clazz)) + .findFirst().map(clazz::cast).orElse(null); + } + + public boolean isUpgradeTypeEnabled(Class clazz) { + return getEnabledUpgradeType(clazz) != null; + } + + private void loadUpgradeLevelFromSection(SuperiorSkyblockPlugin plugin, SUpgrade upgrade, + String sectionName, ConfigurationSection levelSection) { + int level = Integer.parseInt(sectionName); + + String priceType = levelSection.getString("price-type", "money"); + UpgradeCostLoader costLoader = plugin.getUpgrades().getUpgradeCostLoader(priceType); + + if (costLoader == null) { + SuperiorSkyblockPlugin.log("&cUpgrade by name " + upgrade.getName() + " (level " + level + ") has invalid price-type. Skipping..."); + return; + } + + UpgradeCost upgradeCost; + + try { + upgradeCost = costLoader.loadCost(levelSection); + } catch (UpgradeCostLoadException ex) { + SuperiorSkyblockPlugin.log("&cUpgrade by name " + upgrade.getName() + " (level " + level + ") failed to initialize because: " + + ex.getMessage() + ". Skipping..."); + PluginDebugger.debug(ex); + return; + } + + List commands = levelSection.getStringList("commands"); + String permission = levelSection.getString("permission", ""); + Set> requirements = new HashSet<>(); + for (String line : levelSection.getStringList("required-checks")) { + String[] sections = line.split(";"); + requirements.add(new Pair<>(sections[0], StringUtils.translateColors(sections[1]))); + } + UpgradeValue cropGrowth = new UpgradeValue<>(levelSection.getDouble("crop-growth", -1D), true); + UpgradeValue spawnerRates = new UpgradeValue<>(levelSection.getDouble("spawner-rates", -1D), true); + UpgradeValue mobDrops = new UpgradeValue<>(levelSection.getDouble("mob-drops", -1D), true); + UpgradeValue teamLimit = new UpgradeValue<>(levelSection.getInt("team-limit", -1), true); + UpgradeValue warpsLimit = new UpgradeValue<>(levelSection.getInt("warps-limit", -1), true); + UpgradeValue coopLimit = new UpgradeValue<>(levelSection.getInt("coop-limit", -1), true); + UpgradeValue borderSize = new UpgradeValue<>(levelSection.getInt("border-size", -1), true); + UpgradeValue bankLimit = new UpgradeValue<>(new BigDecimal(levelSection.getString("bank-limit", "-1")), true); + KeyMap blockLimits = new KeyMap<>(); + if (levelSection.contains("block-limits")) { + for (String block : levelSection.getConfigurationSection("block-limits").getKeys(false)) { + blockLimits.put(block, levelSection.getInt("block-limits." + block)); + plugin.getBlockValues().addCustomBlockKey(Key.of(block)); + } + } + KeyMap entityLimits = new KeyMap<>(); + if (levelSection.contains("entity-limits")) { + for (String entity : levelSection.getConfigurationSection("entity-limits").getKeys(false)) + entityLimits.put(entity.toUpperCase(), levelSection.getInt("entity-limits." + entity)); + } + KeyMap[] generatorRates = new KeyMap[World.Environment.values().length]; + if (levelSection.contains("generator-rates")) { + for (String blockOrEnv : levelSection.getConfigurationSection("generator-rates").getKeys(false)) { + try { + int index = World.Environment.valueOf(blockOrEnv.toUpperCase()).ordinal(); + for (String block : levelSection.getConfigurationSection("generator-rates." + blockOrEnv).getKeys(false)) { + if (generatorRates[index] == null) + generatorRates[index] = new KeyMap<>(); + generatorRates[index].put(block, levelSection.getInt("generator-rates." + blockOrEnv + "." + block)); } - upgrade.addUpgradeLevel(level, new SUpgradeLevel(level, upgradeCost, commands, permission, requirements, - cropGrowth, spawnerRates, mobDrops, teamLimit, warpsLimit, coopLimit, borderSize, blockLimits, - entityLimits, generatorRates, islandEffects, bankLimit, rolesLimits)); + } catch (Exception ex) { + if (generatorRates[0] == null) + generatorRates[0] = new KeyMap<>(); + generatorRates[0].put(blockOrEnv, levelSection.getInt("generator-rates." + blockOrEnv)); } - - plugin.getUpgrades().addUpgrade(upgrade); } } - + Map islandEffects = new HashMap<>(); + if (levelSection.contains("island-effects")) { + for (String effect : levelSection.getConfigurationSection("island-effects").getKeys(false)) { + PotionEffectType potionEffectType = PotionEffectType.getByName(effect); + if (potionEffectType != null) + islandEffects.put(potionEffectType, levelSection.getInt("island-effects." + effect) - 1); + } + } + Map rolesLimits = new HashMap<>(); + if (levelSection.contains("role-limits")) { + for (String roleId : levelSection.getConfigurationSection("role-limits").getKeys(false)) { + try { + rolesLimits.put(Integer.parseInt(roleId), levelSection.getInt("role-limits." + roleId)); + } catch (NumberFormatException error) { + PluginDebugger.debug(error); + } + } + } + upgrade.addUpgradeLevel(level, new SUpgradeLevel(level, upgradeCost, commands, permission, requirements, + cropGrowth, spawnerRates, mobDrops, teamLimit, warpsLimit, coopLimit, borderSize, blockLimits, + entityLimits, generatorRates, islandEffects, bankLimit, rolesLimits)); } - @Override - protected String[] getIgnoredSections() { - return new String[]{"upgrades"}; + @SuppressWarnings("ResultOfMethodCallIgnored") + private void convertOldUpgradesFile(SuperiorSkyblockPlugin plugin) { + File upgradesFile = new File(plugin.getDataFolder(), "upgrades.yml"); + + if (upgradesFile.exists()) { + CommentedConfiguration config = CommentedConfiguration.loadConfiguration(upgradesFile); + + super.config.set("upgrades", config.get("upgrades")); + + File moduleConfigFile = new File(getDataFolder(), "config.yml"); + + try { + super.config.save(moduleConfigFile); + config.save(upgradesFile); + } catch (Exception ex) { + ex.printStackTrace(); + PluginDebugger.debug(ex); + } + + upgradesFile.delete(); + } } } diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/listeners/UpgradesListener.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/listeners/UpgradesListener.java deleted file mode 100644 index f1cc0d9f3..000000000 --- a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/listeners/UpgradesListener.java +++ /dev/null @@ -1,433 +0,0 @@ -package com.bgsoftware.superiorskyblock.module.upgrades.listeners; - -import com.bgsoftware.superiorskyblock.lang.Message; -import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; -import com.bgsoftware.superiorskyblock.api.island.Island; -import com.bgsoftware.superiorskyblock.key.ConstantKeys; -import com.bgsoftware.superiorskyblock.key.Key; -import com.bgsoftware.superiorskyblock.utils.LocationUtils; -import com.bgsoftware.superiorskyblock.utils.ServerVersion; -import com.bgsoftware.superiorskyblock.utils.StringUtils; -import com.bgsoftware.superiorskyblock.utils.entities.EntityUtils; -import com.bgsoftware.superiorskyblock.threads.Executor; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.CreatureSpawner; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Hanging; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Minecart; -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.BlockPlaceEvent; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.entity.EntitySpawnEvent; -import org.bukkit.event.entity.SpawnerSpawnEvent; -import org.bukkit.event.hanging.HangingPlaceEvent; -import org.bukkit.event.player.PlayerBucketEmptyEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.vehicle.VehicleCreateEvent; -import org.bukkit.event.vehicle.VehicleDestroyEvent; -import org.bukkit.inventory.ItemStack; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -@SuppressWarnings("unused") -public final class UpgradesListener implements Listener { - - public static UpgradesListener IMP; - - private final Set alreadySet = new HashSet<>(); - private final Set noRightClickTwice = new HashSet<>(); - private final SuperiorSkyblockPlugin plugin; - @SuppressWarnings("UnstableApiUsage") - private final Cache vehiclesOwners = CacheBuilder.newBuilder() - .expireAfterWrite(2, TimeUnit.SECONDS).build(); - - /* - * SPAWNER RATES - */ - - public UpgradesListener(SuperiorSkyblockPlugin plugin) { - this.plugin = plugin; - IMP = this; - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onSpawn(SpawnerSpawnEvent e) { - handleSpawnerSpawn(e.getSpawner()); - } - - /* - * MOB DROPS - */ - - public void handleSpawnerSpawn(CreatureSpawner spawner) { - if (spawner == null || spawner.getLocation() == null) - return; - - Island island = plugin.getGrid().getIslandAt(spawner.getLocation()); - - if (island == null) - return; - - double spawnerRatesMultiplier = island.getSpawnerRatesMultiplier(); - - if (spawnerRatesMultiplier > 1 && !alreadySet.contains(island.getOwner().getUniqueId())) { - alreadySet.add(island.getOwner().getUniqueId()); - Executor.sync(() -> { - int spawnDelay = plugin.getNMSWorld().getSpawnerDelay(spawner); - if (spawnDelay > 0) { - plugin.getNMSWorld().setSpawnerDelay(spawner, (int) Math.round(spawnDelay / spawnerRatesMultiplier)); - Executor.sync(() -> alreadySet.remove(island.getOwner().getUniqueId()), 10L); - } - }, 5L); - } - } - - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void onEntityDeath(EntityDeathEvent e) { - Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation()); - - if (island == null) - return; - - if (e.getEntity() instanceof Player) - return; - - if (plugin.getSettings().isDropsUpgradePlayersMultiply()) { - EntityDamageEvent lastDamage = e.getEntity().getLastDamageCause(); - if (!(lastDamage instanceof EntityDamageByEntityEvent) || - EntityUtils.getPlayerDamager((EntityDamageByEntityEvent) lastDamage) == null) - return; - } - - double mobDropsMultiplier = island.getMobDropsMultiplier(); - - if (mobDropsMultiplier > 1) { - List dropItems = new ArrayList<>(e.getDrops()); - for (ItemStack itemStack : dropItems) { - if (itemStack != null && !EntityUtils.isEquipment(e.getEntity(), itemStack) && - !plugin.getNMSTags().getNBTTag(itemStack).getValue().containsKey("WildChests")) { - int newAmount = (int) (itemStack.getAmount() * mobDropsMultiplier); - - if (Bukkit.getPluginManager().isPluginEnabled("WildStacker")) { - itemStack.setAmount(newAmount); - } else { - int stackAmounts = newAmount / itemStack.getMaxStackSize(); - int leftOvers = newAmount % itemStack.getMaxStackSize(); - boolean usedOriginal = false; - - if (stackAmounts > 0) { - itemStack.setAmount(itemStack.getMaxStackSize()); - usedOriginal = true; - - ItemStack stackItem = itemStack.clone(); - stackItem.setAmount(itemStack.getMaxStackSize()); - - for (int i = 0; i < stackAmounts - 1; i++) - e.getDrops().add(itemStack.clone()); - } - - if (leftOvers > 0) { - if (usedOriginal) { - ItemStack leftOversItem = itemStack.clone(); - leftOversItem.setAmount(leftOvers); - e.getDrops().add(leftOversItem); - } else { - itemStack.setAmount(leftOvers); - } - } - } - } - } - } - - EntityUtils.clearEntityEquipment(e.getEntity()); - } - - /* - * BLOCK LIMIT - */ - - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void onLastDamageEntity(EntityDamageEvent e) { - if (!(e.getEntity() instanceof LivingEntity)) - return; - - LivingEntity livingEntity = (LivingEntity) e.getEntity(); - - if (!(livingEntity instanceof ArmorStand) && livingEntity.getHealth() - e.getFinalDamage() > 0) - return; - - Island island = plugin.getGrid().getIslandAt(livingEntity.getLocation()); - - if (island == null) - return; - - EntityUtils.cacheEntityEquipment(livingEntity); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onCartPlaceMonitor(PlayerInteractEvent e) { - if (e.getAction() != Action.RIGHT_CLICK_BLOCK || noRightClickTwice.contains(e.getPlayer().getUniqueId()) || - !e.getClickedBlock().getType().name().contains("RAIL") || e.getItem() == null || - !e.getItem().getType().name().contains("MINECART")) - return; - - Island island = plugin.getGrid().getIslandAt(e.getClickedBlock().getLocation()); - - if (island == null) - return; - - noRightClickTwice.add(e.getPlayer().getUniqueId()); - Executor.sync(() -> noRightClickTwice.remove(e.getPlayer().getUniqueId()), 2L); - - switch (e.getItem().getType().name()) { - case "HOPPER_MINECART": - island.handleBlockPlace(ConstantKeys.HOPPER, 1); - break; - case "COMMAND_MINECART": - case "COMMAND_BLOCK_MINECART": - island.handleBlockPlace(ServerVersion.isAtLeast(ServerVersion.v1_13) ? - ConstantKeys.COMMAND_BLOCK : ConstantKeys.COMMAND, 1); - break; - case "EXPLOSIVE_MINECART": - case "TNT_MINECART": - island.handleBlockPlace(ConstantKeys.TNT, 1); - break; - case "POWERED_MINECART": - case "FURNACE_MINECART": - island.handleBlockPlace(ConstantKeys.FURNACE, 1); - break; - case "STORAGE_MINECART": - case "CHEST_MINECART": - island.handleBlockPlace(ConstantKeys.CHEST, 1); - break; - } - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onCartBreakMonitor(VehicleDestroyEvent e) { - if (!(e.getVehicle() instanceof Minecart)) - return; - - Island island = plugin.getGrid().getIslandAt(e.getVehicle().getLocation()); - - if (island == null) - return; - - island.handleBlockBreak(plugin.getNMSAlgorithms().getMinecartBlock((Minecart) e.getVehicle()), 1); - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onBlockPlace(BlockPlaceEvent e) { - Island island = plugin.getGrid().getIslandAt(e.getBlockPlaced().getLocation()); - - if (island == null) - return; - - Key blockKey = Key.of(e.getBlock()); - - if (island.hasReachedBlockLimit(blockKey)) { - e.setCancelled(true); - Message.REACHED_BLOCK_LIMIT.send(e.getPlayer(), StringUtils.format(blockKey.toString())); - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onCartPlace(PlayerInteractEvent e) { - if (e.getAction() != Action.RIGHT_CLICK_BLOCK || noRightClickTwice.contains(e.getPlayer().getUniqueId()) || - !e.getClickedBlock().getType().name().contains("RAIL") || e.getItem() == null || - !e.getItem().getType().name().contains("MINECART")) - return; - - Island island = plugin.getGrid().getIslandAt(e.getClickedBlock().getLocation()); - - if (island == null) - return; - - Key key = null; - - switch (e.getItem().getType().name()) { - case "HOPPER_MINECART": - key = ConstantKeys.HOPPER; - break; - case "COMMAND_MINECART": - case "COMMAND_BLOCK_MINECART": - key = ServerVersion.isAtLeast(ServerVersion.v1_13) ? ConstantKeys.COMMAND_BLOCK : ConstantKeys.COMMAND; - break; - case "EXPLOSIVE_MINECART": - case "TNT_MINECART": - key = ConstantKeys.TNT; - break; - case "POWERED_MINECART": - case "FURNACE_MINECART": - key = ConstantKeys.FURNACE; - break; - case "STORAGE_MINECART": - case "CHEST_MINECART": - key = ConstantKeys.CHEST; - break; - } - - if (key != null && island.hasReachedBlockLimit(key)) { - e.setCancelled(true); - Message.REACHED_BLOCK_LIMIT.send(e.getPlayer(), StringUtils.format(key.getGlobalKey())); - } - } - - /* - * ENTITY LIMIT - */ - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onBucketEmpty(PlayerBucketEmptyEvent e) { - Island island = plugin.getGrid().getIslandAt(e.getBlockClicked().getLocation()); - - if (island == null) - return; - - Key blockKey = Key.of(e.getBucket().name().replace("_BUCKET", "")); - - if (island.hasReachedBlockLimit(blockKey)) { - e.setCancelled(true); - Message.REACHED_BLOCK_LIMIT.send(e.getPlayer(), StringUtils.format(blockKey.toString())); - } - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onEntitySpawn(EntitySpawnEvent e) { - Island island = plugin.getGrid().getIslandAt(e.getLocation()); - - if (island == null) - return; - - if (!EntityUtils.canHaveLimit(e.getEntityType())) - return; - - island.hasReachedEntityLimit(Key.of(e.getEntity())).whenComplete((result, ex) -> { - if (result) { - if (ServerVersion.isAtLeast(ServerVersion.v1_17)) { - Executor.ensureMain(() -> e.getEntity().remove()); - } else { - e.getEntity().remove(); - } - } - }); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onHangingPlace(HangingPlaceEvent e) { - Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation()); - - if (island == null) - return; - - if (!EntityUtils.canHaveLimit(e.getEntity().getType())) - return; - - island.hasReachedEntityLimit(Key.of(e.getEntity())).whenComplete((result, ex) -> { - if (result && e.getEntity().isValid() && !e.getEntity().isDead()) { - e.getEntity().remove(); - if (e.getPlayer().getGameMode() != GameMode.CREATIVE) - e.getPlayer().getInventory().addItem(asItemStack(e.getEntity())); - } - }); - } - - @EventHandler - public void onVehicleSpawn(PlayerInteractEvent e) { - if (e.getAction() != Action.RIGHT_CLICK_BLOCK || noRightClickTwice.contains(e.getPlayer().getUniqueId()) || - e.getPlayer().getGameMode() == GameMode.CREATIVE || e.getItem() == null || - !e.getClickedBlock().getType().name().contains("RAIL") || - !e.getItem().getType().name().contains("MINECART")) - return; - - Island island = plugin.getGrid().getIslandAt(e.getClickedBlock().getLocation()); - - if (island == null) - return; - - //noinspection UnstableApiUsage - vehiclesOwners.put(e.getClickedBlock().getLocation(), e.getPlayer().getUniqueId()); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onVehicleSpawn(VehicleCreateEvent e) { - if (!(e.getVehicle() instanceof Minecart)) - return; - - Island island = plugin.getGrid().getIslandAt(e.getVehicle().getLocation()); - - if (island == null) - return; - - //noinspection UnstableApiUsage - UUID placedVehicle = vehiclesOwners.asMap().get(LocationUtils.getBlockLocation(e.getVehicle().getLocation())); - - if (!EntityUtils.canHaveLimit(e.getVehicle().getType())) - return; - - island.hasReachedEntityLimit(Key.of(e.getVehicle())).whenComplete((result, ex) -> { - if (result && e.getVehicle().isValid() && !e.getVehicle().isDead()) { - Executor.sync(() -> { - e.getVehicle().remove(); - if (placedVehicle != null) - Bukkit.getPlayer(placedVehicle).getInventory().addItem(asItemStack(e.getVehicle())); - }); - } - }); - } - - /* - * ISLAND CHEST - */ - - private ItemStack asItemStack(Entity entity) { - if (entity instanceof Hanging) { - switch (entity.getType()) { - case ITEM_FRAME: - return new ItemStack(Material.ITEM_FRAME); - case PAINTING: - return new ItemStack(Material.PAINTING); - } - } else if (entity instanceof Minecart) { - Material material = Material.valueOf(plugin.getNMSAlgorithms().getMinecartBlock((Minecart) entity).getGlobalKey()); - switch (material.name()) { - case "HOPPER": - return new ItemStack(Material.HOPPER_MINECART); - case "COMMAND_BLOCK": - return new ItemStack(Material.valueOf("COMMAND_BLOCK_MINECART")); - case "COMMAND": - return new ItemStack(Material.COMMAND_MINECART); - case "TNT": - return new ItemStack(ServerVersion.isLegacy() ? Material.EXPLOSIVE_MINECART : Material.valueOf("TNT_MINECART")); - case "FURNACE": - return new ItemStack(ServerVersion.isLegacy() ? Material.POWERED_MINECART : Material.valueOf("FURNACE_MINECART")); - case "CHEST": - return new ItemStack(ServerVersion.isLegacy() ? Material.STORAGE_MINECART : Material.valueOf("CHEST_MINECART")); - default: - return new ItemStack(Material.MINECART); - } - } - - throw new IllegalArgumentException("Cannot find an item for " + entity.getType()); - } - -} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/IUpgradeType.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/IUpgradeType.java new file mode 100644 index 000000000..5d59baff8 --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/IUpgradeType.java @@ -0,0 +1,14 @@ +package com.bgsoftware.superiorskyblock.module.upgrades.type; + +import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; +import org.bukkit.event.Listener; + +import java.util.List; + +public interface IUpgradeType { + + Listener getListener(); + + List getCommands(); + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeBlockLimits.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeBlockLimits.java new file mode 100644 index 000000000..0f998afe7 --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeBlockLimits.java @@ -0,0 +1,125 @@ +package com.bgsoftware.superiorskyblock.module.upgrades.type; + +import com.bgsoftware.common.reflection.ReflectMethod; +import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; +import com.bgsoftware.superiorskyblock.api.island.Island; +import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; +import com.bgsoftware.superiorskyblock.key.ConstantKeys; +import com.bgsoftware.superiorskyblock.key.Key; +import com.bgsoftware.superiorskyblock.lang.Message; +import com.bgsoftware.superiorskyblock.threads.Executor; +import com.bgsoftware.superiorskyblock.utils.ServerVersion; +import com.bgsoftware.superiorskyblock.utils.StringUtils; +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.BlockPlaceEvent; +import org.bukkit.event.player.PlayerBucketEmptyEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; + +import java.util.Collections; +import java.util.List; + +public final class UpgradeTypeBlockLimits implements IUpgradeType { + + private static final ReflectMethod INTERACT_GET_HAND = new ReflectMethod<>( + PlayerInteractEvent.class, "getHand"); + + private final SuperiorSkyblockPlugin plugin; + + public UpgradeTypeBlockLimits(SuperiorSkyblockPlugin plugin) { + this.plugin = plugin; + } + + @Override + public Listener getListener() { + return new BlockLimitsListener(); + } + + @Override + public List getCommands() { + return Collections.emptyList(); + } + + private final class BlockLimitsListener implements Listener { + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onBlockPlace(BlockPlaceEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getBlockPlaced().getLocation()); + + if (island == null) + return; + + Key blockKey = Key.of(e.getBlock()); + + if (island.hasReachedBlockLimit(blockKey)) { + e.setCancelled(true); + Message.REACHED_BLOCK_LIMIT.send(e.getPlayer(), StringUtils.format(blockKey.toString())); + } + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onCartPlace(PlayerInteractEvent e) { + if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getItem() == null || + !e.getClickedBlock().getType().name().contains("RAIL") || + !e.getItem().getType().name().contains("MINECART")) + return; + + if (INTERACT_GET_HAND.isValid() && INTERACT_GET_HAND.invoke(e) != EquipmentSlot.HAND) + return; + + Island island = plugin.getGrid().getIslandAt(e.getClickedBlock().getLocation()); + + if (island == null) + return; + + Key key = null; + + switch (e.getItem().getType().name()) { + case "HOPPER_MINECART": + key = ConstantKeys.HOPPER; + break; + case "COMMAND_MINECART": + case "COMMAND_BLOCK_MINECART": + key = ServerVersion.isAtLeast(ServerVersion.v1_13) ? ConstantKeys.COMMAND_BLOCK : ConstantKeys.COMMAND; + break; + case "EXPLOSIVE_MINECART": + case "TNT_MINECART": + key = ConstantKeys.TNT; + break; + case "POWERED_MINECART": + case "FURNACE_MINECART": + key = ConstantKeys.FURNACE; + break; + case "STORAGE_MINECART": + case "CHEST_MINECART": + key = ConstantKeys.CHEST; + break; + } + + if (key != null && island.hasReachedBlockLimit(key)) { + e.setCancelled(true); + Message.REACHED_BLOCK_LIMIT.send(e.getPlayer(), StringUtils.format(key.getGlobalKey())); + } + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onBucketEmpty(PlayerBucketEmptyEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getBlockClicked().getLocation()); + + if (island == null) + return; + + Key blockKey = Key.of(e.getBucket().name().replace("_BUCKET", "")); + + if (island.hasReachedBlockLimit(blockKey)) { + e.setCancelled(true); + Message.REACHED_BLOCK_LIMIT.send(e.getPlayer(), StringUtils.format(blockKey.toString())); + } + } + + } + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeCropGrowth.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeCropGrowth.java new file mode 100644 index 000000000..de1db8993 --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeCropGrowth.java @@ -0,0 +1,49 @@ +package com.bgsoftware.superiorskyblock.module.upgrades.type; + +import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; +import com.bgsoftware.superiorskyblock.api.island.Island; +import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddCropGrowth; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetCropGrowth; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockGrowEvent; + +import java.util.Arrays; +import java.util.List; + +public final class UpgradeTypeCropGrowth implements IUpgradeType { + + private static final List commands = Arrays.asList(new CmdAdminAddCropGrowth(), + new CmdAdminSetCropGrowth()); + + private final SuperiorSkyblockPlugin plugin; + + public UpgradeTypeCropGrowth(SuperiorSkyblockPlugin plugin) { + this.plugin = plugin; + } + + @Override + public Listener getListener() { + return new CropGrowthListener(); + } + + @Override + public List getCommands() { + return commands; + } + + private final class CropGrowthListener implements Listener { + + // Should potentially fix crop growth tile entities "disappearing" + @EventHandler(priority = EventPriority.LOWEST) + public void onBlockGrow(BlockGrowEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getBlock().getLocation()); + if (island != null && island.isInsideRange(e.getBlock().getLocation())) + plugin.getNMSChunks().startTickingChunk(island, e.getBlock().getChunk(), false); + } + + } + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeEntityLimits.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeEntityLimits.java new file mode 100644 index 000000000..772c85dde --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeEntityLimits.java @@ -0,0 +1,182 @@ +package com.bgsoftware.superiorskyblock.module.upgrades.type; + +import com.bgsoftware.common.reflection.ReflectMethod; +import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; +import com.bgsoftware.superiorskyblock.api.island.Island; +import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; +import com.bgsoftware.superiorskyblock.key.Key; +import com.bgsoftware.superiorskyblock.threads.Executor; +import com.bgsoftware.superiorskyblock.utils.LocationUtils; +import com.bgsoftware.superiorskyblock.utils.ServerVersion; +import com.bgsoftware.superiorskyblock.utils.entities.EntityUtils; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Hanging; +import org.bukkit.entity.Minecart; +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.entity.EntitySpawnEvent; +import org.bukkit.event.hanging.HangingPlaceEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.vehicle.VehicleCreateEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public final class UpgradeTypeEntityLimits implements IUpgradeType { + + private static final ReflectMethod INTERACT_GET_HAND = new ReflectMethod<>( + PlayerInteractEvent.class, "getHand"); + + private final SuperiorSkyblockPlugin plugin; + + public UpgradeTypeEntityLimits(SuperiorSkyblockPlugin plugin) { + this.plugin = plugin; + } + + @Override + public Listener getListener() { + return new BlockLimitsListener(); + } + + @Override + public List getCommands() { + return Collections.emptyList(); + } + + private final class BlockLimitsListener implements Listener { + + private final Map vehiclesOwners = new HashMap<>(); + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onEntitySpawn(EntitySpawnEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getLocation()); + + if (island == null) + return; + + if (!EntityUtils.canHaveLimit(e.getEntityType())) + return; + + island.hasReachedEntityLimit(Key.of(e.getEntity())).whenComplete((result, ex) -> { + if (result) { + if (ServerVersion.isAtLeast(ServerVersion.v1_17)) { + Executor.ensureMain(() -> e.getEntity().remove()); + } else { + e.getEntity().remove(); + } + } + }); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onHangingPlace(HangingPlaceEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation()); + + if (island == null) + return; + + if (!EntityUtils.canHaveLimit(e.getEntity().getType())) + return; + + island.hasReachedEntityLimit(Key.of(e.getEntity())).whenComplete((result, ex) -> { + if (result && e.getEntity().isValid() && !e.getEntity().isDead()) { + e.getEntity().remove(); + if (e.getPlayer().getGameMode() != GameMode.CREATIVE) + e.getPlayer().getInventory().addItem(asItemStack(e.getEntity())); + } + }); + } + + @EventHandler + public void onVehicleSpawn(PlayerInteractEvent e) { + if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getItem() == null || + e.getPlayer().getGameMode() == GameMode.CREATIVE || + !e.getClickedBlock().getType().name().contains("RAIL") || + !e.getItem().getType().name().contains("MINECART")) + return; + + if (INTERACT_GET_HAND.isValid() && INTERACT_GET_HAND.invoke(e) != EquipmentSlot.HAND) + return; + + Island island = plugin.getGrid().getIslandAt(e.getClickedBlock().getLocation()); + + if (island == null) + return; + + Location blockLocation = e.getClickedBlock().getLocation(); + + vehiclesOwners.put(blockLocation, e.getPlayer().getUniqueId()); + Executor.sync(() -> vehiclesOwners.remove(blockLocation), 40L); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onVehicleSpawn(VehicleCreateEvent e) { + if (!(e.getVehicle() instanceof Minecart)) + return; + + Island island = plugin.getGrid().getIslandAt(e.getVehicle().getLocation()); + + if (island == null) + return; + + UUID placedVehicle = vehiclesOwners.remove(LocationUtils.getBlockLocation(e.getVehicle().getLocation())); + + if (!EntityUtils.canHaveLimit(e.getVehicle().getType())) + return; + + island.hasReachedEntityLimit(Key.of(e.getVehicle())).whenComplete((result, ex) -> { + if (result && e.getVehicle().isValid() && !e.getVehicle().isDead()) { + Executor.sync(() -> { + e.getVehicle().remove(); + if (placedVehicle != null) + Bukkit.getPlayer(placedVehicle).getInventory().addItem(asItemStack(e.getVehicle())); + }); + } + }); + } + + private ItemStack asItemStack(Entity entity) { + if (entity instanceof Hanging) { + switch (entity.getType()) { + case ITEM_FRAME: + return new ItemStack(Material.ITEM_FRAME); + case PAINTING: + return new ItemStack(Material.PAINTING); + } + } else if (entity instanceof Minecart) { + Material material = Material.valueOf(plugin.getNMSAlgorithms().getMinecartBlock((Minecart) entity).getGlobalKey()); + switch (material.name()) { + case "HOPPER": + return new ItemStack(Material.HOPPER_MINECART); + case "COMMAND_BLOCK": + return new ItemStack(Material.valueOf("COMMAND_BLOCK_MINECART")); + case "COMMAND": + return new ItemStack(Material.COMMAND_MINECART); + case "TNT": + return new ItemStack(ServerVersion.isLegacy() ? Material.EXPLOSIVE_MINECART : Material.valueOf("TNT_MINECART")); + case "FURNACE": + return new ItemStack(ServerVersion.isLegacy() ? Material.POWERED_MINECART : Material.valueOf("FURNACE_MINECART")); + case "CHEST": + return new ItemStack(ServerVersion.isLegacy() ? Material.STORAGE_MINECART : Material.valueOf("CHEST_MINECART")); + default: + return new ItemStack(Material.MINECART); + } + } + + throw new IllegalArgumentException("Cannot find an item for " + entity.getType()); + } + + } + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeIslandEffects.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeIslandEffects.java new file mode 100644 index 000000000..ec1f4ab95 --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeIslandEffects.java @@ -0,0 +1,59 @@ +package com.bgsoftware.superiorskyblock.module.upgrades.type; + +import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; +import com.bgsoftware.superiorskyblock.api.island.Island; +import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddEffect; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetEffect; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityPotionEffectEvent; + +import java.util.Arrays; +import java.util.List; + +public final class UpgradeTypeIslandEffects implements IUpgradeType { + + private static final List commands = Arrays.asList(new CmdAdminAddEffect(), + new CmdAdminSetEffect()); + + private final SuperiorSkyblockPlugin plugin; + + public UpgradeTypeIslandEffects(SuperiorSkyblockPlugin plugin) { + this.plugin = plugin; + } + + @Override + public Listener getListener() { + return new IslandEffectsListener(); + } + + @Override + public List getCommands() { + return commands; + } + + private final class IslandEffectsListener implements Listener { + + @EventHandler(ignoreCancelled = true) + public void onPlayerEffect(EntityPotionEffectEvent e) { + if (e.getAction() == EntityPotionEffectEvent.Action.ADDED || !(e.getEntity() instanceof Player) || + e.getCause() == EntityPotionEffectEvent.Cause.PLUGIN) + return; + + Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation()); + + if (island == null) + return; + + int islandEffectLevel = island.getPotionEffectLevel(e.getModifiedType()); + + if (islandEffectLevel > 0 && (e.getOldEffect() == null || e.getOldEffect().getAmplifier() == islandEffectLevel)) { + e.setCancelled(true); + } + } + + } + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeMobDrops.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeMobDrops.java new file mode 100644 index 000000000..8300e9501 --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeMobDrops.java @@ -0,0 +1,129 @@ +package com.bgsoftware.superiorskyblock.module.upgrades.type; + +import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; +import com.bgsoftware.superiorskyblock.api.island.Island; +import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddMobDrops; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetMobDrops; +import com.bgsoftware.superiorskyblock.utils.entities.EntityUtils; +import org.bukkit.Bukkit; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDeathEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public final class UpgradeTypeMobDrops implements IUpgradeType { + + private static final List commands = Arrays.asList(new CmdAdminAddMobDrops(), + new CmdAdminSetMobDrops()); + + private final SuperiorSkyblockPlugin plugin; + + public UpgradeTypeMobDrops(SuperiorSkyblockPlugin plugin) { + this.plugin = plugin; + } + + @Override + public Listener getListener() { + return new MobDropsListener(); + } + + @Override + public List getCommands() { + return commands; + } + + private final class MobDropsListener implements Listener { + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onLastDamageEntity(EntityDamageEvent e) { + if (!(e.getEntity() instanceof LivingEntity)) + return; + + LivingEntity livingEntity = (LivingEntity) e.getEntity(); + + if (!(livingEntity instanceof ArmorStand) && livingEntity.getHealth() - e.getFinalDamage() > 0) + return; + + Island island = plugin.getGrid().getIslandAt(livingEntity.getLocation()); + + if (island == null) + return; + + EntityUtils.cacheEntityEquipment(livingEntity); + } + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onEntityDeath(EntityDeathEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation()); + + if (island == null) + return; + + if (e.getEntity() instanceof Player) + return; + + if (plugin.getSettings().isDropsUpgradePlayersMultiply()) { + EntityDamageEvent lastDamage = e.getEntity().getLastDamageCause(); + if (!(lastDamage instanceof EntityDamageByEntityEvent) || + EntityUtils.getPlayerDamager((EntityDamageByEntityEvent) lastDamage) == null) + return; + } + + double mobDropsMultiplier = island.getMobDropsMultiplier(); + + if (mobDropsMultiplier > 1) { + List dropItems = new ArrayList<>(e.getDrops()); + for (ItemStack itemStack : dropItems) { + if (itemStack != null && !EntityUtils.isEquipment(e.getEntity(), itemStack) && + !plugin.getNMSTags().getNBTTag(itemStack).getValue().containsKey("WildChests")) { + int newAmount = (int) (itemStack.getAmount() * mobDropsMultiplier); + + if (Bukkit.getPluginManager().isPluginEnabled("WildStacker")) { + itemStack.setAmount(newAmount); + } else { + int stackAmounts = newAmount / itemStack.getMaxStackSize(); + int leftOvers = newAmount % itemStack.getMaxStackSize(); + boolean usedOriginal = false; + + if (stackAmounts > 0) { + itemStack.setAmount(itemStack.getMaxStackSize()); + usedOriginal = true; + + ItemStack stackItem = itemStack.clone(); + stackItem.setAmount(itemStack.getMaxStackSize()); + + for (int i = 0; i < stackAmounts - 1; i++) + e.getDrops().add(itemStack.clone()); + } + + if (leftOvers > 0) { + if (usedOriginal) { + ItemStack leftOversItem = itemStack.clone(); + leftOversItem.setAmount(leftOvers); + e.getDrops().add(leftOversItem); + } else { + itemStack.setAmount(leftOvers); + } + } + } + } + } + } + + EntityUtils.clearEntityEquipment(e.getEntity()); + } + + } + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeSpawnerRates.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeSpawnerRates.java new file mode 100644 index 000000000..af817267e --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeSpawnerRates.java @@ -0,0 +1,78 @@ +package com.bgsoftware.superiorskyblock.module.upgrades.type; + +import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; +import com.bgsoftware.superiorskyblock.api.island.Island; +import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminAddSpawnerRates; +import com.bgsoftware.superiorskyblock.module.upgrades.commands.CmdAdminSetSpawnerRates; +import com.bgsoftware.superiorskyblock.threads.Executor; +import org.bukkit.block.CreatureSpawner; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.SpawnerSpawnEvent; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +public final class UpgradeTypeSpawnerRates implements IUpgradeType { + + private static final List commands = Arrays.asList(new CmdAdminAddSpawnerRates(), + new CmdAdminSetSpawnerRates()); + + private final Set alreadyTrackedSpawning = new HashSet<>(); + + private final SuperiorSkyblockPlugin plugin; + + public UpgradeTypeSpawnerRates(SuperiorSkyblockPlugin plugin) { + this.plugin = plugin; + } + + @Override + public Listener getListener() { + return new SpawnerRatesListener(); + } + + @Override + public List getCommands() { + return commands; + } + + public void handleSpawnerSpawn(@Nullable CreatureSpawner creatureSpawner) { + if (creatureSpawner == null || creatureSpawner.getLocation() == null) + return; + + Island island = plugin.getGrid().getIslandAt(creatureSpawner.getLocation()); + + if (island == null) + return; + + double spawnerRatesMultiplier = island.getSpawnerRatesMultiplier(); + + if (spawnerRatesMultiplier > 1 && alreadyTrackedSpawning.add(island.getOwner().getUniqueId())) { + Executor.sync(() -> { + int spawnDelay = plugin.getNMSWorld().getSpawnerDelay(creatureSpawner); + if (spawnDelay > 0) { + plugin.getNMSWorld().setSpawnerDelay(creatureSpawner, + (int) Math.round(spawnDelay / spawnerRatesMultiplier)); + Executor.sync(() -> alreadyTrackedSpawning.remove(island.getOwner().getUniqueId()), 10L); + } + }, 5L); + } + } + + private final class SpawnerRatesListener implements Listener { + + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onSpawn(SpawnerSpawnEvent e) { + handleSpawnerSpawn(e.getSpawner()); + } + + } + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/utils/logic/PlayersLogic.java b/src/main/java/com/bgsoftware/superiorskyblock/utils/logic/PlayersLogic.java index c3965d1a9..8064faac1 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/utils/logic/PlayersLogic.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/utils/logic/PlayersLogic.java @@ -7,6 +7,8 @@ import com.bgsoftware.superiorskyblock.api.events.IslandRestrictMoveEvent; import com.bgsoftware.superiorskyblock.api.island.Island; import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer; +import com.bgsoftware.superiorskyblock.module.BuiltinModules; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeIslandEffects; import com.bgsoftware.superiorskyblock.utils.events.EventsCaller; import com.bgsoftware.superiorskyblock.island.flags.IslandFlags; import com.bgsoftware.superiorskyblock.island.permissions.IslandPrivileges; diff --git a/src/main/java/com/bgsoftware/superiorskyblock/world/blocks/BlockChangeTask.java b/src/main/java/com/bgsoftware/superiorskyblock/world/blocks/BlockChangeTask.java index 8d3c66c5a..f3dc9e509 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/world/blocks/BlockChangeTask.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/world/blocks/BlockChangeTask.java @@ -2,6 +2,8 @@ import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; import com.bgsoftware.superiorskyblock.api.island.Island; +import com.bgsoftware.superiorskyblock.module.BuiltinModules; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeCropGrowth; import com.bgsoftware.superiorskyblock.tag.CompoundTag; import com.bgsoftware.superiorskyblock.world.chunks.ChunkPosition; import com.bgsoftware.superiorskyblock.world.chunks.ChunksProvider; @@ -55,7 +57,8 @@ public void submitUpdate(Runnable onFinish) { IslandUtils.deleteChunks(island, Collections.singletonList(entry.getKey()), null); - if (island.isInsideRange(chunk)) + boolean cropGrowthEnabled = BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeCropGrowth.class); + if (cropGrowthEnabled && island.isInsideRange(chunk)) plugin.getNMSChunks().startTickingChunk(island, chunk, false); ChunksTracker.markDirty(island, chunk, false); diff --git a/src/main/java/com/bgsoftware/superiorskyblock/world/event/WorldEventsManagerImpl.java b/src/main/java/com/bgsoftware/superiorskyblock/world/event/WorldEventsManagerImpl.java index 726990534..91bd04012 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/world/event/WorldEventsManagerImpl.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/world/event/WorldEventsManagerImpl.java @@ -3,6 +3,8 @@ import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; import com.bgsoftware.superiorskyblock.api.island.Island; import com.bgsoftware.superiorskyblock.api.world.event.WorldEventsManager; +import com.bgsoftware.superiorskyblock.module.BuiltinModules; +import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeCropGrowth; import com.bgsoftware.superiorskyblock.world.chunks.ChunksTracker; import com.google.common.base.Preconditions; import org.bukkit.Chunk; @@ -39,7 +41,8 @@ public void loadChunk(Chunk chunk) { plugin.getNMSChunks().injectChunkSections(chunk); - if (island.isInsideRange(chunk)) + boolean cropGrowthEnabled = BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeCropGrowth.class); + if (cropGrowthEnabled && island.isInsideRange(chunk)) plugin.getNMSChunks().startTickingChunk(island, chunk, false); if (!plugin.getNMSChunks().isChunkEmpty(chunk)) @@ -69,7 +72,8 @@ public void unloadChunk(Chunk chunk) { if (island == null) return; - plugin.getNMSChunks().startTickingChunk(island, chunk, true); + if (BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeCropGrowth.class)) + plugin.getNMSChunks().startTickingChunk(island, chunk, true); if (!island.isSpawn() && !plugin.getNMSChunks().isChunkEmpty(chunk)) ChunksTracker.markDirty(island, chunk, true); diff --git a/src/main/resources/modules/upgrades/config.yml b/src/main/resources/modules/upgrades/config.yml index 28ab904d3..d828c6d91 100644 --- a/src/main/resources/modules/upgrades/config.yml +++ b/src/main/resources/modules/upgrades/config.yml @@ -5,7 +5,29 @@ ## ## ###################################################### -enabled: true +# Whether crop-growth should be enabled. +# When disabled, the plugin will not alter crop growth. +crop-growth: true + +# Whether mob drops should be enabled. +# When disabled, the plugin will not alter mob drops. +mob-drops: true + +# Whether island-effects should be enabled. +# When disabled, the plugin will not give any effects to any players. +island-effects: true + +# Whether spawner-rates should be enabled. +# When disabled, the plugin will not alter spawner rates. +spawner-rates: true + +# Whether block-limits should be enabled. +# When disabled, the plugin will not limit placement of blocks. +block-limits: true + +# Whether entity-limits should be enabled. +# When disabled, the plugin will not limit spawning of entities. +entity-limits: true # All the upgrades of the plugin upgrades: