diff --git a/src/main/java/me/goodandevil/skyblock/command/commands/island/ValueCommand.java b/src/main/java/me/goodandevil/skyblock/command/commands/island/ValueCommand.java index 445f2232..3537d671 100644 --- a/src/main/java/me/goodandevil/skyblock/command/commands/island/ValueCommand.java +++ b/src/main/java/me/goodandevil/skyblock/command/commands/island/ValueCommand.java @@ -4,7 +4,7 @@ import me.goodandevil.skyblock.command.SubCommand; import me.goodandevil.skyblock.config.FileManager; import me.goodandevil.skyblock.config.FileManager.Config; import me.goodandevil.skyblock.levelling.LevellingManager; -import me.goodandevil.skyblock.levelling.Material; +import me.goodandevil.skyblock.levelling.LevellingMaterial; import me.goodandevil.skyblock.message.MessageManager; import me.goodandevil.skyblock.sound.SoundManager; import me.goodandevil.skyblock.utils.NumberUtil; @@ -46,7 +46,7 @@ public class ValueCommand extends SubCommand { } if (materials != null && levellingManager.containsMaterial(materials)) { - Material material = levellingManager.getMaterial(materials); + LevellingMaterial material = levellingManager.getMaterial(materials); double level = (double) material.getPoints() / (double) fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")) .getFileConfiguration().getInt("Island.Levelling.Division"); diff --git a/src/main/java/me/goodandevil/skyblock/island/IslandManager.java b/src/main/java/me/goodandevil/skyblock/island/IslandManager.java index 39d63585..c580254e 100644 --- a/src/main/java/me/goodandevil/skyblock/island/IslandManager.java +++ b/src/main/java/me/goodandevil/skyblock/island/IslandManager.java @@ -738,6 +738,9 @@ public class IslandManager { setNextAvailableLocation(world, islandLocation); saveNextAvailableLocation(world); + + // Recalculate island level + Bukkit.getServer().getScheduler().runTaskLater(skyblock, () -> skyblock.getLevellingManager().calculatePoints(null, island), 20L); } public Set getVisitorsAtIsland(Island island) { diff --git a/src/main/java/me/goodandevil/skyblock/levelling/LevellingManager.java b/src/main/java/me/goodandevil/skyblock/levelling/LevellingManager.java index 55af9dab..a715c27f 100644 --- a/src/main/java/me/goodandevil/skyblock/levelling/LevellingManager.java +++ b/src/main/java/me/goodandevil/skyblock/levelling/LevellingManager.java @@ -1,23 +1,5 @@ package me.goodandevil.skyblock.levelling; -import java.io.File; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; - -import org.bukkit.Bukkit; -import org.bukkit.ChunkSnapshot; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.scheduler.BukkitRunnable; - import me.goodandevil.skyblock.SkyBlock; import me.goodandevil.skyblock.api.event.island.IslandLevelChangeEvent; import me.goodandevil.skyblock.config.FileManager.Config; @@ -30,12 +12,22 @@ import me.goodandevil.skyblock.utils.version.Materials; import me.goodandevil.skyblock.utils.version.NMSUtil; import me.goodandevil.skyblock.utils.version.Sounds; import me.goodandevil.skyblock.world.WorldManager; +import org.bukkit.*; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; +import java.util.logging.Level; public class LevellingManager { private final SkyBlock skyblock; - private List materialStorage = new ArrayList<>(); + private List materialStorage = new ArrayList<>(); public LevellingManager(SkyBlock skyblock) { this.skyblock = skyblock; @@ -59,8 +51,6 @@ public class LevellingManager { if (!chunk.isComplete()) return; cancel(); - Map materials = new HashMap<>(); - Method getBlockTypeMethod = null; Method getBlockTypeIdMethod = null; Method getBlockTypeDataMethod = null; @@ -76,6 +66,8 @@ public class LevellingManager { } } + Map levellingData = new HashMap<>(); + for (ChunkSnapshot chunkSnapshotList : chunk.getChunkSnapshots()) { for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { @@ -114,51 +106,33 @@ public class LevellingManager { blockData = (int) getBlockTypeDataMethod.invoke(chunkSnapshotList, x, y, z); } - if (blockMaterial == org.bukkit.Material.AIR) continue; + if (blockMaterial == org.bukkit.Material.AIR) + continue; - for (Material materialList : materialStorage) { - if (materialList == null) continue; - ItemStack is; - try { - is = materialList.getItemStack(); - } catch (Exception ignored) { - continue; + int amount = 1; + if (blockMaterial == Materials.SPAWNER.parseMaterial() + && Bukkit.getPluginManager().isPluginEnabled("EpicSpawners")) { + World world = Bukkit.getWorld(chunkSnapshotList.getWorldName()); + com.songoda.epicspawners.api.EpicSpawners epicSpawners = + com.songoda.epicspawners.api.EpicSpawnersAPI.getImplementation(); + Location location = new Location(world, chunkSnapshotList.getX() * 16 + x, y, chunkSnapshotList.getZ() * 16 + z); + if (epicSpawners.getSpawnerManager().isSpawner(location)) { + amount = epicSpawners.getSpawnerManager() + .getSpawnerFromWorld(location).getSpawnerDataCount(); } - - if (blockMaterial != materialList.getItemStack().getType()) continue; - if (NMSVersion < 13) { - if (blockData != is.getData().getData()) { - continue; - } - } - - int amount = 1; - if (blockMaterial == Materials.SPAWNER.parseMaterial() - && Bukkit.getPluginManager().isPluginEnabled("EpicSpawners")) { - World world = Bukkit.getWorld(chunkSnapshotList.getWorldName()); - com.songoda.epicspawners.api.EpicSpawners epicSpawners = - com.songoda.epicspawners.api.EpicSpawnersAPI.getImplementation(); - Location location = new Location(world, chunkSnapshotList.getX() * 16 + x, y, chunkSnapshotList.getZ() * 16 + z); - if (epicSpawners.getSpawnerManager().isSpawner(location)) { - amount = epicSpawners.getSpawnerManager() - .getSpawnerFromWorld(location).getSpawnerDataCount(); - } - } else if (stackableManager != null && stackableManager.getStackableMaterials().contains(blockMaterial)) { - World world = Bukkit.getWorld(chunkSnapshotList.getWorldName()); - Location location = new Location(world, chunkSnapshotList.getX() * 16 + x, y, chunkSnapshotList.getZ() * 16 + z); - if (stackableManager.isStacked(location)) { - Stackable stackable = stackableManager.getStack(location, blockMaterial); - amount = stackable.getSize(); - } - } - - if (materials.containsKey(materialList.getMaterials().name())) { - materials.put(materialList.getMaterials().name(), - materials.get(materialList.getMaterials().name()) + amount); - } else { - materials.put(materialList.getMaterials().name(), amount); + } else if (stackableManager != null && stackableManager.getStackableMaterials().contains(blockMaterial)) { + World world = Bukkit.getWorld(chunkSnapshotList.getWorldName()); + Location location = new Location(world, chunkSnapshotList.getX() * 16 + x, y, chunkSnapshotList.getZ() * 16 + z); + if (stackableManager.isStacked(location)) { + Stackable stackable = stackableManager.getStack(location, blockMaterial); + amount = stackable.getSize(); } } + + LevellingData data = new LevellingData(blockMaterial, (byte)blockData); + Integer totalAmountInteger = levellingData.get(data); + int totalAmount = totalAmountInteger == null ? amount : totalAmountInteger + amount; + levellingData.put(data, totalAmount); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { e.printStackTrace(); @@ -168,6 +142,12 @@ public class LevellingManager { } } + Map materials = new HashMap<>(); + for (LevellingData data : levellingData.keySet()) { + int amount = levellingData.get(data); + materials.put(data.getMaterials().name(), amount); + } + if (materials.size() == 0) { if (player != null) { skyblock.getMessageManager().sendMessage(player, skyblock.getFileManager() @@ -218,15 +198,15 @@ public class LevellingManager { } public void addMaterial(Materials materials, int points) { - materialStorage.add(new Material(materials, points)); + materialStorage.add(new LevellingMaterial(materials, points)); } - public void removeMaterial(Material material) { + public void removeMaterial(LevellingMaterial material) { materialStorage.remove(material); } public boolean containsMaterial(Materials materials) { - for (Material materialList : materialStorage) { + for (LevellingMaterial materialList : materialStorage) { if (materialList.getMaterials().name().equals(materials.name())) { return true; } @@ -235,8 +215,8 @@ public class LevellingManager { return false; } - public Material getMaterial(Materials materials) { - for (Material materialList : materialStorage) { + public LevellingMaterial getMaterial(Materials materials) { + for (LevellingMaterial materialList : materialStorage) { if (materialList.getMaterials().name().equals(materials.name())) { return materialList; } @@ -245,7 +225,40 @@ public class LevellingManager { return null; } - public List getMaterials() { + public List getMaterials() { return materialStorage; } + + private class LevellingData { + private final Material material; + private final byte data; + + private LevellingData(Material material, byte data) { + this.material = material; + this.data = data; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof LevellingData)) return false; + LevellingData data = (LevellingData)obj; + if (this == obj) return true; + return this.material == data.material && this.data == data.data; + } + + @Override + public int hashCode() { + return Objects.hash(this.material, this.data); + } + + private Materials getMaterials() { + if (NMSUtil.getVersionNumber() > 12) { + try { + return Materials.fromString(material.name()); + } catch (Exception ignored) {} + } + + return Materials.getMaterials(this.material, this.data); + } + } } diff --git a/src/main/java/me/goodandevil/skyblock/levelling/Material.java b/src/main/java/me/goodandevil/skyblock/levelling/LevellingMaterial.java similarity index 84% rename from src/main/java/me/goodandevil/skyblock/levelling/Material.java rename to src/main/java/me/goodandevil/skyblock/levelling/LevellingMaterial.java index 40d6bebc..8b49480d 100644 --- a/src/main/java/me/goodandevil/skyblock/levelling/Material.java +++ b/src/main/java/me/goodandevil/skyblock/levelling/LevellingMaterial.java @@ -4,12 +4,12 @@ import org.bukkit.inventory.ItemStack; import me.goodandevil.skyblock.utils.version.Materials; -public class Material { +public class LevellingMaterial { private Materials materials; private int points; - public Material(Materials materials, int points) { + public LevellingMaterial(Materials materials, int points) { this.materials = materials; this.points = points; } diff --git a/src/main/java/me/goodandevil/skyblock/menus/Levelling.java b/src/main/java/me/goodandevil/skyblock/menus/Levelling.java index cef00228..65058d9b 100644 --- a/src/main/java/me/goodandevil/skyblock/menus/Levelling.java +++ b/src/main/java/me/goodandevil/skyblock/menus/Levelling.java @@ -2,6 +2,7 @@ package me.goodandevil.skyblock.menus; import java.io.File; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.lang3.text.WordUtils; @@ -193,9 +194,10 @@ public class Levelling { IslandLevel level = island.getLevel(); Map testIslandMaterials = level.getMaterials(); - Map islandMaterials = new HashMap<>(); + Map islandMaterials = level.getMaterials(); Config mainConfig = fileManager.getConfig(new File(skyblock.getDataFolder(), "levelling.yml")); + Config settingsConfig = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); // Filter out ItemStacks that can't be displayed in the inventory Inventory testInventory = Bukkit.createInventory(null, 9); @@ -204,12 +206,12 @@ public class Levelling { continue; if (mainConfig.getFileConfiguration().getInt("Materials." + materialName + ".Points") <= 0) continue; - + Materials materials = Materials.fromString(materialName); ItemStack is = materials.parseItem(); - is.setAmount(testIslandMaterials.get(materialName)); + is.setAmount(Math.min(testIslandMaterials.get(materialName), 64)); is.setType(MaterialUtil.correctMaterial(is.getType())); - + if (is == null || is.getItemMeta() == null) continue; testInventory.clear(); testInventory.setItem(0, is); @@ -270,27 +272,30 @@ public class Levelling { if (materials != null) { int materialAmount = islandMaterials.get(material); - if (mainConfig.getFileConfiguration() - .getString("Materials." + material + ".Points") != null) { - int pointsRequired = mainConfig.getFileConfiguration() - .getInt("Materials." + material + ".Points"); + if (mainConfig.getFileConfiguration().getString("Materials." + material + ".Points") != null) { + int pointsMultiplier = mainConfig.getFileConfiguration().getInt("Materials." + material + ".Points"); - if (pointsRequired != 0) { + if (settingsConfig.getFileConfiguration().getBoolean("Island.Levelling.IncludeEmptyPointsInList") || pointsMultiplier != 0) { inventorySlot++; - int pointsEarned = materialAmount * pointsRequired; + int pointsEarned = materialAmount * pointsMultiplier; ItemStack is = materials.parseItem(); is.setAmount(materialAmount); is.setType(MaterialUtil.correctMaterial(is.getType())); + System.out.println(MaterialUtil.correctMaterial(is.getType()).name() + " | " + material); + + List lore = configLoad.getStringList("Menu.Levelling.Item.Material.Lore"); + lore.replaceAll(x -> x.replace("%blocks", NumberUtil.formatNumberByDecimal(materialAmount))); + nInv.addItem(nInv.createItem(is, configLoad - .getString("Menu.Levelling.Item.Material.Displayname") - .replace("%points", NumberUtil.formatNumberByDecimal(pointsEarned)) - .replace("%material", - WordUtils.capitalize(material.toLowerCase().replace("_", " ") - .replace("item", "").replace("block", ""))), - null, null, null, null), inventorySlot); + .getString("Menu.Levelling.Item.Material.Displayname") + .replace("%points", NumberUtil.formatNumberByDecimal(pointsEarned)) + .replace("%material", + WordUtils.capitalize(material.toLowerCase().replace("_", " ") + .replace("item", "").replace("block", ""))), + lore, null, null, null), inventorySlot); } } } diff --git a/src/main/java/me/goodandevil/skyblock/menus/admin/Levelling.java b/src/main/java/me/goodandevil/skyblock/menus/admin/Levelling.java index 466acb10..66b8ea8f 100644 --- a/src/main/java/me/goodandevil/skyblock/menus/admin/Levelling.java +++ b/src/main/java/me/goodandevil/skyblock/menus/admin/Levelling.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.util.List; import java.util.stream.Collectors; +import me.goodandevil.skyblock.levelling.LevellingMaterial; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -56,7 +57,7 @@ public class Levelling implements Listener { PlayerData playerData = skyblock.getPlayerDataManager().getPlayerData(player); - List levellingMaterials = levellingManager.getMaterials(); + List levellingMaterials = levellingManager.getMaterials(); // Filter out materials that won't be displayed in the GUI properly Inventory testInventory = Bukkit.createInventory(null, 9); @@ -121,7 +122,7 @@ public class Levelling implements Listener { if (levellingMaterials.size() > index) { inventorySlot++; - me.goodandevil.skyblock.levelling.Material material = levellingMaterials.get(index); + LevellingMaterial material = levellingMaterials.get(index); nInv.addItem( nInv.createItem( new ItemStack(MaterialUtil.correctMaterial(material.getItemStack().getType()), 1, @@ -311,7 +312,7 @@ public class Levelling implements Listener { } if (is.hasItemMeta() && is.getItemMeta().hasDisplayName()) { - for (me.goodandevil.skyblock.levelling.Material materialList : levellingManager.getMaterials()) { + for (LevellingMaterial materialList : levellingManager.getMaterials()) { Materials materials = materialList.getMaterials(); if (event.getCurrentItem().getType() == MaterialUtil.correctMaterial(materials.parseMaterial()) diff --git a/src/main/java/me/goodandevil/skyblock/utils/item/MaterialUtil.java b/src/main/java/me/goodandevil/skyblock/utils/item/MaterialUtil.java index 20c28211..c99efbb8 100644 --- a/src/main/java/me/goodandevil/skyblock/utils/item/MaterialUtil.java +++ b/src/main/java/me/goodandevil/skyblock/utils/item/MaterialUtil.java @@ -23,6 +23,8 @@ public class MaterialUtil { material = Material.TRIPWIRE_HOOK; } else if (material == Material.FLOWER_POT) { material = Materials.LEGACY_FLOWER_POT_ITEM.getPostMaterial(); + } else if (material.name().startsWith("POTTED_")) { + material = Material.FLOWER_POT; } else if (material == Materials.LEGACY_IRON_DOOR_BLOCK.getPostMaterial()) { material = Material.IRON_DOOR; } else if (material == Material.CAULDRON) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f4fc4b52..4b6088fa 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -162,6 +162,8 @@ Island: # time cycle or be fixed. Cycle: false Levelling: + # Include Points: 0 in the '/is level' GUI + IncludeEmptyPointsInList: true # Island Level Points divided by value Division: 100 Cooldown: diff --git a/src/main/resources/language.yml b/src/main/resources/language.yml index 1d346f2d..13b22029 100644 --- a/src/main/resources/language.yml +++ b/src/main/resources/language.yml @@ -1842,6 +1842,8 @@ Menu: Displayname: "&c&oNothing to see here..." Material: Displayname: "&7%material &8(&e+%points Points&8)" + Lore: + - "&8(&e%blocks Blocks&8)" Settings: Categories: Title: "&8Settings - Categories"