From bf684fe4aaf97c46079d1f74c31b65998e4d905d Mon Sep 17 00:00:00 2001 From: Fabrizio La Rosa Date: Thu, 25 Jun 2020 03:08:22 +0200 Subject: [PATCH] Simplify level manager --- .../levelling/IslandLevelManager.java | 50 ++++++++++++++++- .../com/songoda/skyblock/listeners/Block.java | 53 ++++--------------- 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/songoda/skyblock/levelling/IslandLevelManager.java b/src/main/java/com/songoda/skyblock/levelling/IslandLevelManager.java index b15ac2cd..705c66ac 100644 --- a/src/main/java/com/songoda/skyblock/levelling/IslandLevelManager.java +++ b/src/main/java/com/songoda/skyblock/levelling/IslandLevelManager.java @@ -9,14 +9,18 @@ import java.util.Map; import java.util.Map.Entry; import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.compatibility.ServerVersion; +import com.songoda.skyblock.island.IslandLevel; import com.songoda.skyblock.levelling.calculator.Calculator; import com.songoda.skyblock.levelling.calculator.CalculatorRegistry; import com.songoda.skyblock.levelling.calculator.impl.EpicSpawnerCalculator; import com.songoda.skyblock.levelling.calculator.impl.UltimateStackerCalculator; +import com.songoda.skyblock.utils.version.CompatibleSpawners; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.block.CreatureSpawner; import org.bukkit.configuration.Configuration; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; @@ -210,5 +214,49 @@ public final class IslandLevelManager { return new AmountMaterialPair(compMaterial, amount + stackSize); } - + + public void updateLevel(Island island, Location location) { + // Fix a bug in Paper 1.8.8 when using ViaVersion on a 1.12.2 client. + // BUG: Player can infinitely increase their level by placing a block at their + // feet. + // It doesn't take the block away but still increments the level. + // This doesn't happen in Spigot, but does happen in PaperSpigot due to a + // BlockPlaceEvent being incorrectly fired. + // The solution is to wait a tick to make sure that the block was actually + // placed. + // This shouldn't cause any issues besides the task number being increased + // insanely fast. + if(ServerVersion.isServerVersion(ServerVersion.V1_8)){ + Bukkit.getScheduler().runTask(plugin, () -> { + updateLevelLocation(island, location); + }); + } else { + updateLevelLocation(island, location); + } + } + + private void updateLevelLocation(Island island, Location location) { + Block block = location.getBlock(); + CompatibleMaterial material = CompatibleMaterial.getMaterial(block); + + if (material == null || material == CompatibleMaterial.AIR) return; + + if (material == CompatibleMaterial.SPAWNER) { + if (Bukkit.getPluginManager().isPluginEnabled("EpicSpawners") || Bukkit.getPluginManager().isPluginEnabled("WildStacker")) + return; + + CompatibleSpawners spawner = CompatibleSpawners.getSpawner(((CreatureSpawner) block.getState()).getSpawnedType()); + + if (spawner != null) + material = CompatibleMaterial.getBlockMaterial(spawner.getMaterial()); + } + + long materialAmount = 0; + IslandLevel level = island.getLevel(); + + if (level.hasMaterial(material.name())) + materialAmount = level.getMaterialAmount(material.name()); + + level.setMaterialAmount(material.name(), materialAmount + 1); + } } diff --git a/src/main/java/com/songoda/skyblock/listeners/Block.java b/src/main/java/com/songoda/skyblock/listeners/Block.java index ee5865bf..e1af24ab 100644 --- a/src/main/java/com/songoda/skyblock/listeners/Block.java +++ b/src/main/java/com/songoda/skyblock/listeners/Block.java @@ -185,7 +185,7 @@ public class Block implements Listener { IslandManager islandManager = skyblock.getIslandManager(); WorldManager worldManager = skyblock.getWorldManager(); - IslandLevelManager levellingManager = skyblock.getLevellingManager(); + IslandLevelManager islandLevelManager = skyblock.getLevellingManager(); if (!worldManager.isIslandWorld(block.getWorld())) return; Location blockLoc = block.getLocation(); @@ -201,7 +201,7 @@ public class Block implements Listener { return; } - if (levellingManager.isScanning(island)) { + if (islandLevelManager.isScanning(island)) { skyblock.getMessageManager().sendMessage(player, skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration().getString("Command.Island.Level.Scanning.BlockPlacing.Message")); event.setCancelled(true); @@ -286,8 +286,8 @@ public class Block implements Listener { if (event.getBlock().getType() == CompatibleMaterial.END_PORTAL_FRAME.getMaterial() && event.getPlayer().getItemInHand().getType() == CompatibleMaterial.ENDER_EYE.getMaterial()) return; - - updateLevel(island, blockLoc); + + islandLevelManager.updateLevel(island, blockLoc); } @EventHandler @@ -297,6 +297,7 @@ public class Block implements Listener { GeneratorManager generatorManager = skyblock.getGeneratorManager(); IslandManager islandManager = skyblock.getIslandManager(); WorldManager worldManager = skyblock.getWorldManager(); + IslandLevelManager islandLevelManager = skyblock.getLevellingManager(); Island island = islandManager.getIslandAtLocation(event.getBlock().getLocation()); IslandWorld world = worldManager.getIslandWorld(event.getBlock().getWorld()); @@ -382,7 +383,7 @@ public class Block implements Listener { toBlockState.setData(genState.getData()); toBlockState.setType(genState.getType()); toBlockState.update(); - updateLevel(island, genState.getLocation()); + islandLevelManager.updateLevel(island, genState.getLocation()); return; } } @@ -541,6 +542,8 @@ public class Block implements Listener { public void onBlockForm(BlockFormEvent event) { org.bukkit.block.Block block = event.getBlock(); WorldManager worldManager = skyblock.getWorldManager(); + IslandLevelManager islandLevelManager = skyblock.getLevellingManager(); + if (!worldManager.isIslandWorld(block.getWorld())) return; // Check ice/snow forming @@ -612,7 +615,7 @@ public class Block implements Listener { state.setType(genState.getType()); if (NMSUtil.getVersionNumber() < 13) state.setData(genState.getData()); - updateLevel(island, genState.getLocation()); + islandLevelManager.updateLevel(island, genState.getLocation()); return; } } @@ -715,42 +718,4 @@ public class Block implements Listener { event.setCancelled(true); } - private void updateLevel(Island island, Location location) { - // Fix a bug in Paper 1.8.8 when using ViaVersion on a 1.12.2 client. - // BUG: Player can infinitely increase their level by placing a block at their - // feet. - // It doesn't take the block away but still increments the level. - // This doesn't happen in Spigot, but does happen in PaperSpigot due to a - // BlockPlaceEvent being incorrectly fired. - // The solution is to wait a tick to make sure that the block was actually - // placed. - // This shouldn't cause any issues besides the task number being increased - // insanely fast. - // TODO Do this only in 1.8.8 - Bukkit.getScheduler().runTask(skyblock, () -> { - org.bukkit.block.Block block = location.getBlock(); - CompatibleMaterial material = CompatibleMaterial.getMaterial(block); - - if (material == null || material == CompatibleMaterial.AIR) return; - - if (material == CompatibleMaterial.SPAWNER) { - if (Bukkit.getPluginManager().isPluginEnabled("EpicSpawners") || Bukkit.getPluginManager().isPluginEnabled("WildStacker")) - return; - - CompatibleSpawners spawner = CompatibleSpawners.getSpawner(((CreatureSpawner) block.getState()).getSpawnedType()); - - if (spawner != null) - material = CompatibleMaterial.getBlockMaterial(spawner.getMaterial()); - } - - long materialAmount = 0; - IslandLevel level = island.getLevel(); - - if (level.hasMaterial(material.name())) - materialAmount = level.getMaterialAmount(material.name()); - - level.setMaterialAmount(material.name(), materialAmount + 1); - }); - } - }