Simplify level manager

This commit is contained in:
Fabrizio La Rosa 2020-06-25 03:08:22 +02:00
parent 08e5046f52
commit bf684fe4aa
2 changed files with 58 additions and 45 deletions

View File

@ -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);
}
}

View File

@ -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);
});
}
}