diff --git a/src/main/java/me/unurled/sacredrealms/sr/components/treasure/TreasureManager.java b/src/main/java/me/unurled/sacredrealms/sr/components/treasure/TreasureManager.java index 8b58cbb..c13ccb6 100644 --- a/src/main/java/me/unurled/sacredrealms/sr/components/treasure/TreasureManager.java +++ b/src/main/java/me/unurled/sacredrealms/sr/components/treasure/TreasureManager.java @@ -5,9 +5,11 @@ import static me.unurled.sacredrealms.sr.utils.Component.textComp; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.papermc.paper.event.packet.PlayerChunkLoadEvent; -import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import javax.annotation.Nullable; import me.unurled.sacredrealms.sr.components.player.PlayerManager; import me.unurled.sacredrealms.sr.components.player.SRPlayer; @@ -27,7 +29,8 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; public class TreasureManager extends Manager { - private final List treasures = new ArrayList<>(); + private final Map treasures = new HashMap<>(); + private final Map locations = new HashMap<>(); private int nextId = 0; @@ -35,12 +38,12 @@ public class TreasureManager extends Manager { @Override public void saveData() { // save all treasures - for (Treasure treasure : treasures) { + for (Treasure treasure : treasures.values()) { // serialize treasure DataHandler dh = Manager.getInstance(DataManager.class).getDataHandler(); Gson gson = new GsonBuilder().registerTypeAdapter(Treasure.class, new TreasureSerializer()).create(); - dh.set("treasures." + treasures.indexOf(treasure), gson.toJson(treasure)); + dh.set("treasures." + treasure.getId(), gson.toJson(treasure)); } } @@ -58,28 +61,34 @@ public class TreasureManager extends Manager { } public void addTreasure(Treasure treasure) { - treasures.add(treasure); + treasures.put(treasure.getId(), treasure); nextId = Math.max(nextId, treasure.getId() + 1); + locations.put(treasure.getId(), treasure.getLocation()); + } + + @Nullable + private Treasure getTreasureById(Integer id) { + return treasures.get(id); } @Nullable public Treasure findByLocation(@NotNull Location loc) { - for (Treasure treasure : treasures) { - if (treasure.getLocation().equals(loc)) { - return treasure; + for (Entry location : locations.entrySet()) { + if (location.getValue().equals(loc)) { + return getTreasureById(location.getKey()); } } return null; } @Nullable - public Treasure findByNearbyPlayer(@NotNull Location loc) { - for (Treasure treasure : treasures) { - if (treasure.getLocation().getWorld() != loc.getWorld()) { + private Treasure findByNearbyPlayer(@NotNull Location loc) { + for (Entry location : locations.entrySet()) { + if (location.getValue().getWorld() != loc.getWorld()) { continue; } - if (treasure.getLocation().distance(loc) < 5) { - return treasure; + if (location.getValue().distance(loc) < 5) { + return getTreasureById(location.getKey()); } } return null; @@ -87,7 +96,7 @@ public class TreasureManager extends Manager { @NotNull public List getTreasures() { - return treasures; + return treasures.values().stream().toList(); } public void removeTreasure(@NotNull Treasure treasure) { @@ -106,7 +115,7 @@ public class TreasureManager extends Manager { player.sendBlockChange(treasure.getLocation(), Material.AIR.createBlockData()); } }); - treasures.remove(treasure); + treasures.remove(treasure.getId()); } public int getNextId() { @@ -129,10 +138,15 @@ public class TreasureManager extends Manager { private void onTreasureOpen(PlayerInteractEvent e, Block clickedBlock) { // it is a chest, lets loop on all treasure and find if the clicked block is a treasure chest - for (Treasure treasure : treasures) { - if (treasure.getLocation().equals(clickedBlock.getLocation())) { + for (Entry location : locations.entrySet()) { + if (location.getValue().equals(clickedBlock.getLocation())) { // it is a treasure chest, lets open it Player p = e.getPlayer(); + Treasure treasure = getTreasureById(location.getKey()); + if (treasure == null) { + p.sendMessage(textComp("An Error occurred contact an Admin.")); + return; + } if (!p.hasPermission(treasure.getPermission())) { p.sendMessage("You don't have permission to open this chest"); p.playSound(p.getLocation(), Sound.BLOCK_ANVIL_BREAK, 1f, 0.5f);