From 0f0bf88dea1355d106d4d700c84a5523ed3154f8 Mon Sep 17 00:00:00 2001 From: Farmeurimmo Date: Sun, 12 Jun 2022 16:51:34 +0200 Subject: [PATCH] =?UTF-8?q?Stockage=20minions=20avec=20=C3=AEles=20+=20ajo?= =?UTF-8?q?ut=20is=20perms=20+=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/verymc/core/storage/ConfigManager.java | 23 --- .../core/storage/StorageYAMLManager.java | 156 ++++++++---------- src/main/java/fr/verymc/island/Island.java | 32 +++- .../java/fr/verymc/island/IslandManager.java | 10 +- .../verymc/island/blocks/ChestListener.java | 9 + .../island/events/IslandInteractManager.java | 6 +- .../java/fr/verymc/island/minions/Minion.java | 15 +- .../verymc/island/minions/MinionHarvest.java | 61 ++++--- .../verymc/island/minions/MinionManager.java | 17 +- .../fr/verymc/island/minions/MinionsCmd.java | 26 ++- .../island/minions/MinionsListener.java | 104 ++++++++---- .../fr/verymc/island/perms/IslandPerms.java | 14 +- .../java/fr/verymc/utils/PreGenItems.java | 9 + todo.txt | 1 - 14 files changed, 263 insertions(+), 220 deletions(-) diff --git a/src/main/java/fr/verymc/core/storage/ConfigManager.java b/src/main/java/fr/verymc/core/storage/ConfigManager.java index 46e10e27..ea634b0e 100644 --- a/src/main/java/fr/verymc/core/storage/ConfigManager.java +++ b/src/main/java/fr/verymc/core/storage/ConfigManager.java @@ -14,11 +14,9 @@ public class ConfigManager { public FileConfiguration dataAh; public FileConfiguration dataIslands; public FileConfiguration dataSkyblockUser; - public FileConfiguration dataMinions; public File ahFile; public File islandsFile; public File skyblockUserFile; - public File minionsFile; public File subdir; @@ -67,18 +65,6 @@ public void setup() { dataSkyblockUser = YamlConfiguration.loadConfiguration(skyblockUserFile); - minionsFile = new File(Main.instance.getDataFolder(), "minions.yml"); - - if (!minionsFile.exists()) { - try { - minionsFile.createNewFile(); - } catch (IOException e) { - Main.instance.getLogger().info("§c§lErreur lors de la création de minions.yml"); - } - } - - dataMinions = YamlConfiguration.loadConfiguration(minionsFile); - /*subdir = new File(Main.instance.getDataFolder().getPath() + System.getProperty("file.separator") + "backups"); subdir.mkdir();*/ @@ -98,10 +84,6 @@ public FileConfiguration getDataSkyblockUser() { return dataSkyblockUser; } - public FileConfiguration getDataMinions() { - return dataMinions; - } - public void reloadData() throws IOException { try { @@ -120,10 +102,5 @@ public void reloadData() throws IOException { } catch (InvalidConfigurationException e) { Main.instance.getLogger().info("§c§lErreur lors de la sauvegarde!"); } - try { - dataMinions.load(minionsFile); - } catch (InvalidConfigurationException e) { - Main.instance.getLogger().info("§c§lErreur lors de la sauvegarde!"); - } } } diff --git a/src/main/java/fr/verymc/core/storage/StorageYAMLManager.java b/src/main/java/fr/verymc/core/storage/StorageYAMLManager.java index d9374069..c98fe759 100644 --- a/src/main/java/fr/verymc/core/storage/StorageYAMLManager.java +++ b/src/main/java/fr/verymc/core/storage/StorageYAMLManager.java @@ -8,7 +8,6 @@ import main.java.fr.verymc.island.blocks.Chest; import main.java.fr.verymc.island.challenges.IslandChallenge; import main.java.fr.verymc.island.minions.Minion; -import main.java.fr.verymc.island.minions.MinionManager; import main.java.fr.verymc.island.minions.MinionType; import main.java.fr.verymc.island.perms.IslandPerms; import main.java.fr.verymc.island.perms.IslandRanks; @@ -53,40 +52,9 @@ public boolean getDataFromAPI() { //API FETCH DATA - ArrayList minions = new ArrayList<>(); ArrayList islands = new ArrayList<>(); ArrayList skyblockUsers = new ArrayList<>(); - for (String str : ConfigManager.instance.getDataMinions().getKeys(false)) { - if (str == null) continue; - try { - String ownerUUIDstr = ConfigManager.instance.getDataMinions().getString(str + ".uuid"); - if (ownerUUIDstr == null || ownerUUIDstr.length() != 36) { - continue; - } - UUID owner = UUID.fromString(ownerUUIDstr); - MinionType minionType = MinionType.valueOf(ConfigManager.instance.getDataMinions().getString(str + ".type")); - BlockFace blockFace = BlockFace.valueOf(ConfigManager.instance.getDataMinions().getString(str + ".blFace")); - int lvl = ConfigManager.instance.getDataMinions().getInt(str + ".lvl"); - Location loc = ConfigManager.instance.getDataMinions().getLocation(str + ".loc"); - Location locChest = null; - if (ConfigManager.instance.getDataMinions().getLocation(str + ".locChest") != null) { - locChest = ConfigManager.instance.getDataMinions().getLocation(str + ".locChest"); - } - boolean linked = ConfigManager.instance.getDataMinions().getBoolean(str + ".linked"); - boolean smelft = ConfigManager.instance.getDataMinions().getBoolean(str + ".smelt"); - long id = Long.parseLong(str.replace("'", "")); - - Block block = (locChest == null) ? null : locChest.getBlock(); - minions.add(new Minion(id, owner, lvl, loc, minionType, blockFace, linked, block, smelft)); - } catch (Exception e) { - e.printStackTrace(); - Bukkit.broadcastMessage("§6§lData §8» §4§lErreur lors de la récupération des données de la base de donnée sur le minion #" + str); - continue; - } - } - //DATA MINIONS - for (String str : ConfigManager.instance.getDataIslands().getKeys(false)) { if (str == null) continue; try { @@ -202,35 +170,66 @@ public boolean getDataFromAPI() { if (ConfigManager.instance.getDataIslands().getConfigurationSection(str + ".chests") != null) { for (String str1 : ConfigManager.instance.getDataIslands().getConfigurationSection(str + ".chests").getKeys(false)) { if (str1 == null) continue; - String uuidString = ConfigManager.instance.getDataIslands().getString(str + ".chests." + str1 + ".uuid"); - if (uuidString == null || uuidString.length() != 36) { + try { + String uuidString = ConfigManager.instance.getDataIslands().getString(str + ".chests." + str1 + ".uuid"); + if (uuidString == null || uuidString.length() != 36) { + continue; + } + UUID uuid = UUID.fromString(uuidString); + long idChest = Long.parseLong(str1.replace("'", "")); + ItemStack itemStack = ConfigManager.instance.getDataIslands().getItemStack(str + ".chests." + str1 + ".item"); + int type = ConfigManager.instance.getDataIslands().getInt(str + ".chests." + str1 + ".type"); + Location loc = ConfigManager.instance.getDataIslands().getLocation(str + ".chests." + str1 + ".loc"); + boolean isSell = ConfigManager.instance.getDataIslands().getBoolean(str + ".chests." + str1 + ".isSell"); + long chunk = ConfigManager.instance.getDataIslands().getLong(str + ".chests." + str1 + ".chunk"); + double price = ConfigManager.instance.getDataIslands().getDouble(str + ".chests." + str1 + ".price"); + boolean active = ConfigManager.instance.getDataIslands().getBoolean(str + ".chests." + str1 + ".active"); + double amount = 0; + if (ConfigManager.instance.getDataIslands().get(str + ".chests." + str1 + ".amount") != null) { + amount = ConfigManager.instance.getDataIslands().getDouble(str + ".chests." + str1 + ".amount"); + } + Material stacked = null; + if (ConfigManager.instance.getDataIslands().get(str + ".chests." + str1 + ".stacked") != null) { + stacked = Material.getMaterial(ConfigManager.instance.getDataIslands().getString(str + ".chests." + str1 + ".stacked")); + } + chests.add(new Chest(type, loc, uuid, chunk, itemStack, price, isSell, active, idChest, amount, stacked)); + } catch (Exception e) { + e.printStackTrace(); + Bukkit.broadcastMessage("§6§lData §8» §4§lErreur lors de la récupération des données de la base de donnée sur le chest #" + str); continue; } - UUID uuid = UUID.fromString(uuidString); - long idChest = Long.parseLong(str1.replace("'", "")); - ItemStack itemStack = ConfigManager.instance.getDataIslands().getItemStack(str + ".chests." + str1 + ".item"); - int type = ConfigManager.instance.getDataIslands().getInt(str + ".chests." + str1 + ".type"); - Location loc = ConfigManager.instance.getDataIslands().getLocation(str + ".chests." + str1 + ".loc"); - boolean isSell = ConfigManager.instance.getDataIslands().getBoolean(str + ".chests." + str1 + ".isSell"); - long chunk = ConfigManager.instance.getDataIslands().getLong(str + ".chests." + str1 + ".chunk"); - double price = ConfigManager.instance.getDataIslands().getDouble(str + ".chests." + str1 + ".price"); - boolean active = ConfigManager.instance.getDataIslands().getBoolean(str + ".chests." + str1 + ".active"); - double amount = 0; - if (ConfigManager.instance.getDataIslands().get(str + ".chests." + str1 + ".amount") != null) { - amount = ConfigManager.instance.getDataIslands().getDouble(str + ".chests." + str1 + ".amount"); - } - Material stacked = null; - if (ConfigManager.instance.getDataIslands().get(str + ".chests." + str1 + ".stacked") != null) { - stacked = Material.getMaterial(ConfigManager.instance.getDataIslands().getString(str + ".chests." + str1 + ".stacked")); + } + } + ArrayList minions = new ArrayList<>(); + if (ConfigManager.instance.getDataIslands().getConfigurationSection(str + ".minions") != null) { + for (String str1 : ConfigManager.instance.getDataIslands().getConfigurationSection(str + ".minions").getKeys(false)) { + if (str1 == null) continue; + try { + MinionType minionType = MinionType.valueOf(ConfigManager.instance.getDataIslands().getString(str + ".minions." + str1 + ".type")); + BlockFace blockFace = BlockFace.valueOf(ConfigManager.instance.getDataIslands().getString(str + ".minions." + str1 + ".blFace")); + int lvl = ConfigManager.instance.getDataIslands().getInt(str + ".minions." + str1 + ".lvl"); + Location loc = ConfigManager.instance.getDataIslands().getLocation(str + ".minions." + str1 + ".loc"); + Location locChest = null; + if (ConfigManager.instance.getDataIslands().getLocation(str + ".minions." + str1 + ".locChest") != null) { + locChest = ConfigManager.instance.getDataIslands().getLocation(str + ".minions." + str1 + ".locChest"); + } + boolean linked = ConfigManager.instance.getDataIslands().getBoolean(str + ".minions." + str1 + ".linked"); + boolean smelft = ConfigManager.instance.getDataIslands().getBoolean(str + ".minions." + str1 + ".smelt"); + long idMinion = Long.parseLong(str1.replace("'", "")); + + Block block = (locChest == null) ? null : locChest.getBlock(); + minions.add(new Minion(idMinion, lvl, loc, minionType, blockFace, linked, block, smelft)); + } catch (Exception e) { + e.printStackTrace(); + Bukkit.broadcastMessage("§6§lData §8» §4§lErreur lors de la récupération des données de la base de donnée sur le minion #" + str); + continue; } - - chests.add(new Chest(type, loc, uuid, chunk, itemStack, price, isSell, active, idChest, amount, stacked)); } } islands.add(new Island(name, home, center, id, members, islandUpgradeSize, islandUpgradeMember, color, islandBank, islandUpgradeGenerator, banneds, list, false, - permsPerRanks, isPublic, value, settings, chests)); + permsPerRanks, isPublic, value, settings, chests, minions)); } catch (Exception e) { e.printStackTrace(); continue; @@ -283,9 +282,6 @@ public boolean getDataFromAPI() { Bukkit.getScheduler().scheduleSyncDelayedTask(Main.instance, new Runnable() { @Override public void run() { - //SEND Minions to -> MinionManager.instance.minions - MinionManager.instance.minions = minions; - //SEND Islands to -> IslandManager.instance.islands IslandManager.instance.islands = islands; @@ -302,41 +298,6 @@ public void sendDataToAPIAuto(boolean force) { CompletableFuture.supplyAsync(() -> { long start = System.currentTimeMillis(); - HashMap toSendMinions = new HashMap<>(); - HashMap toRemoveMinions = new HashMap<>(); // NEED TO NULL Minion id because yaml don't support override data - ArrayList minions = MinionManager.instance.minions; - for (Minion minion : minions) { - HashMap toSendMinion = new HashMap<>(); - try { - toSendMinion.put(minion.getID() + ".uuid", minion.getOwnerUUID().toString()); - toSendMinion.put(minion.getID() + ".type", minion.getMinionType().toString()); - toSendMinion.put(minion.getID() + ".lvl", minion.getLevelInt()); - toSendMinion.put(minion.getID() + ".blFace", minion.getBlockFace().toString()); - toSendMinion.put(minion.getID() + ".loc", minion.getBlocLocation()); - toSendMinion.put(minion.getID() + ".linked", minion.isChestLinked()); - toSendMinion.put(minion.getID() + ".smelt", minion.isAutoSmelt()); - if (minion.getChestBloc() != null) { - toSendMinion.put(minion.getID() + ".locChest", minion.getChestBloc().getLocation()); - } else { - toSendMinion.put(minion.getID() + ".locChest", null); - } - } catch (Exception e) { - e.printStackTrace(); - Bukkit.broadcastMessage("§6§lData §8§l» §c§lErreur lors de la lecture du minion #" + minion.getID()); - toSendMinion.clear(); - } finally { - if (toSendMinion.size() > 0) { - toRemoveMinions.put(minion.getID() + "", null); - toSendMinions.putAll(toSendMinion); - } - } - } - AsyncConfig.instance.setAndSaveAsyncBlockCurrentThread(toRemoveMinions, ConfigManager.instance.getDataMinions(), - ConfigManager.instance.minionsFile); - AsyncConfig.instance.setAndSaveAsync(toSendMinions, ConfigManager.instance.getDataMinions(), - ConfigManager.instance.minionsFile); - - HashMap toSendIslands = new HashMap<>(); HashMap toRemoveIslands = new HashMap<>(); // NEED TO NULL Island id because yaml don't support override data ArrayList islands = IslandManager.instance.islands; @@ -393,6 +354,19 @@ public void sendDataToAPIAuto(boolean force) { toSendIsland.put(island.getId() + ".chests." + chest.getId() + ".stacked", chest.getStacked().toString()); } } + for (Minion minion : island.getMinions()) { + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".type", minion.getMinionType().toString()); + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".lvl", minion.getLevelInt()); + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".blFace", minion.getBlockFace().toString()); + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".loc", minion.getBlocLocation()); + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".linked", minion.isChestLinked()); + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".smelt", minion.isAutoSmelt()); + if (minion.getChestBloc() != null) { + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".locChest", minion.getChestBloc().getLocation()); + } else { + toSendIsland.put(island.getId() + ".minions." + minion.getID() + ".locChest", null); + } + } } catch (Exception e) { e.printStackTrace(); Bukkit.broadcastMessage("§cErreur lors de la sauvegarde de l'île #" + island.getId()); diff --git a/src/main/java/fr/verymc/island/Island.java b/src/main/java/fr/verymc/island/Island.java index 9366a288..e4031b42 100644 --- a/src/main/java/fr/verymc/island/Island.java +++ b/src/main/java/fr/verymc/island/Island.java @@ -5,6 +5,7 @@ import main.java.fr.verymc.island.blocks.Chest; import main.java.fr.verymc.island.challenges.IslandChallenge; import main.java.fr.verymc.island.challenges.IslandChallengesListener; +import main.java.fr.verymc.island.minions.Minion; import main.java.fr.verymc.island.perms.IslandPerms; import main.java.fr.verymc.island.perms.IslandRank; import main.java.fr.verymc.island.perms.IslandRanks; @@ -23,7 +24,6 @@ public class Island { - public ArrayList chests; private String name; private Location home; private Location center; @@ -42,12 +42,14 @@ public class Island { private boolean isPublic; private ArrayList challenges; private ArrayList activatedSettings; + private ArrayList chests; + private ArrayList minions; public Island(String name, Location home, Location center, int id, HashMap members, IslandUpgradeSize upgradeSize, IslandUpgradeMember upgradeMember, WorldBorderUtil.Color borderColor, IslandBank bank, IslandUpgradeGenerator generatorUpgrade, ArrayList banneds, ArrayList challenges, boolean isDefaultChallenges, HashMap> permsPerRanks, - boolean isPublic, double value, ArrayList activatedSettings, ArrayList chests) { + boolean isPublic, double value, ArrayList activatedSettings, ArrayList chests, ArrayList minions) { this.name = name; this.home = home; this.center = center; @@ -81,6 +83,11 @@ public Island(String name, Location home, Location center, int id, HashMap(); + } else { + this.minions = minions; + } Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.instance, new Runnable() { @Override public void run() { @@ -94,23 +101,28 @@ public void setDefaultPerms() { ArrayList permsVisit = new ArrayList<>(); permsVisit.add(IslandPerms.INTERACT); permsPerRanks.put(IslandRanks.VISITEUR, permsVisit); + ArrayList perms = new ArrayList<>(); perms.addAll(permsVisit); perms.addAll(Arrays.asList(IslandPerms.BUILD, IslandPerms.BREAK)); permsPerRanks.put(IslandRanks.COOP, perms); + ArrayList permsMembre = new ArrayList<>(); permsMembre.addAll(perms); - permsMembre.add(IslandPerms.CHANGE_BORDER_COLOR); + permsMembre.addAll(Arrays.asList(IslandPerms.CHANGE_BORDER_COLOR, IslandPerms.MINIONS_ADD, IslandPerms.MINIONS_INTERACT)); permsPerRanks.put(IslandRanks.MEMBRE, permsMembre); + ArrayList permsMod = new ArrayList<>(); permsMod.addAll(permsMembre); - permsMod.addAll(Arrays.asList(IslandPerms.CHANGE_BORDER_COLOR, IslandPerms.KICK, IslandPerms.PROMOTE, IslandPerms.DEMOTE)); + permsMod.addAll(Arrays.asList(IslandPerms.CHANGE_BORDER_COLOR, IslandPerms.KICK, IslandPerms.PROMOTE, IslandPerms.DEMOTE, IslandPerms.MINIONS_REMOVE)); permsPerRanks.put(IslandRanks.MODERATEUR, permsMod); + ArrayList permsCoChef = new ArrayList<>(); permsCoChef.addAll(permsMod); permsCoChef.addAll(Arrays.asList(IslandPerms.CHANGE_BORDER_COLOR, IslandPerms.KICK, IslandPerms.PROMOTE, IslandPerms.DEMOTE, IslandPerms.INVITE, IslandPerms.BAN)); permsPerRanks.put(IslandRanks.COCHEF, permsCoChef); + ArrayList permsChef = new ArrayList<>(); permsChef.add(IslandPerms.ALL_PERMS); permsPerRanks.put(IslandRanks.CHEF, permsChef); @@ -685,4 +697,16 @@ public void removeChest(Chest chest) { chests.remove(chest); } + public void addMinion(Minion minion) { + minions.add(minion); + } + + public void removeMinion(Minion minion) { + minions.remove(minion); + } + + public ArrayList getMinions() { + return minions; + } + } diff --git a/src/main/java/fr/verymc/island/IslandManager.java b/src/main/java/fr/verymc/island/IslandManager.java index e03b24f7..18bb29c8 100644 --- a/src/main/java/fr/verymc/island/IslandManager.java +++ b/src/main/java/fr/verymc/island/IslandManager.java @@ -220,13 +220,9 @@ public void run() { Bukkit.getScheduler().scheduleSyncDelayedTask(Main.instance, new Runnable() { @Override public void run() { - ArrayList minions = new ArrayList<>(); - for (Minion m : MinionManager.instance.minions) { - minions.add(m); - } - for (Minion m : minions) { + for (Minion m : playerIsland.getMinions()) { if (getIslandByLoc(m.getBlocLocation()) == playerIsland) { - MinionManager.instance.removeMinion(m); + MinionManager.instance.removeMinion(m, playerIsland); } } ArrayList chests = new ArrayList<>(); @@ -573,7 +569,7 @@ public void run() { home.setYaw(130); islands.add(new Island("Ile de " + p.getName(), home, finalToReturn1, finalId + 1, members, islandUpgradeSize, islandUpgradeMember, WorldBorderUtil.Color.BLUE, islandBank, islandUpgradeGenerator, banneds, challenges, - true, null, true, 0.0, null, null)); + true, null, true, 0.0, null, null, null)); p.sendMessage("§6§lIles §8» §aVous avez généré une nouvelle île avec succès (en " + (System.currentTimeMillis() - start) + "ms)."); teleportPlayerToIslandSafe(p); return; diff --git a/src/main/java/fr/verymc/island/blocks/ChestListener.java b/src/main/java/fr/verymc/island/blocks/ChestListener.java index 7f53a96d..d727b45a 100644 --- a/src/main/java/fr/verymc/island/blocks/ChestListener.java +++ b/src/main/java/fr/verymc/island/blocks/ChestListener.java @@ -35,6 +35,9 @@ public void playerInteract(PlayerInteractEvent e) { if (e.getPlayer().isSneaking()) { return; } + if (e.isCancelled()) { + return; + } if (e.getClickedBlock().getType() == Material.CHEST) { BlockState bs = e.getClickedBlock().getState(); org.bukkit.block.Chest hopper = (org.bukkit.block.Chest) bs; @@ -472,6 +475,9 @@ public void blocExplode(EntityExplodeEvent e) { public void breakEvent(BlockBreakEvent e) { if (e.isCancelled()) return; int type = -1; + if (e.isCancelled()) { + return; + } if (e.getBlock().getType() == Material.HOPPER) { Hopper blhopper = (Hopper) e.getBlock().getState(); if (blhopper.getCustomName() == null) { @@ -543,6 +549,9 @@ public void placeEvent(BlockPlaceEvent e) { } else { return; } + if (e.isCancelled()) { + return; + } ChestManager.instance.placeChest(e.getPlayer(), e.getBlock().getLocation(), type, null, 0.0, e.getBlock().getType()); } diff --git a/src/main/java/fr/verymc/island/events/IslandInteractManager.java b/src/main/java/fr/verymc/island/events/IslandInteractManager.java index fae903ea..3a4c1cf8 100644 --- a/src/main/java/fr/verymc/island/events/IslandInteractManager.java +++ b/src/main/java/fr/verymc/island/events/IslandInteractManager.java @@ -43,7 +43,7 @@ public void playerInteractEvent(PlayerInteractEvent e) { } } - @EventHandler + @EventHandler(priority = org.bukkit.event.EventPriority.HIGHEST) public void blockBreakEvent(BlockBreakEvent e) { if (e.isCancelled()) return; Player player = e.getPlayer(); @@ -59,7 +59,7 @@ public void blockBreakEvent(BlockBreakEvent e) { } } - @EventHandler + @EventHandler(priority = org.bukkit.event.EventPriority.HIGHEST) public void blockPlaceEvent(BlockPlaceEvent e) { if (e.isCancelled()) return; Player player = e.getPlayer(); @@ -75,7 +75,7 @@ public void blockPlaceEvent(BlockPlaceEvent e) { } } - @EventHandler + @EventHandler(priority = org.bukkit.event.EventPriority.HIGHEST) public void interactEvent(PlayerInteractEvent e) { if (e.isCancelled()) return; Player player = e.getPlayer(); diff --git a/src/main/java/fr/verymc/island/minions/Minion.java b/src/main/java/fr/verymc/island/minions/Minion.java index 68f78bdd..c8a51651 100644 --- a/src/main/java/fr/verymc/island/minions/Minion.java +++ b/src/main/java/fr/verymc/island/minions/Minion.java @@ -1,15 +1,11 @@ package main.java.fr.verymc.island.minions; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import java.util.UUID; - public class Minion { - private UUID ownerUUID; private Integer levelInt; private Long id; private Location blocLocation; @@ -19,10 +15,9 @@ public class Minion { private Block chestBloc; private Boolean isAutoSmelt; - public Minion(Long id, UUID ownerUUID, Integer levelInt, Location blocLocation, MinionType minionType, + public Minion(Long id, Integer levelInt, Location blocLocation, MinionType minionType, BlockFace blockFace, Boolean chestLinked, Block chestBloc, Boolean isAutoSmelt) { this.id = id; - this.ownerUUID = ownerUUID; this.levelInt = levelInt; this.blocLocation = blocLocation; this.minionType = minionType; @@ -44,14 +39,6 @@ public void setLevelInt(Integer newlevel) { levelInt = newlevel; } - public String getOwnerName() { - return Bukkit.getOfflinePlayer(ownerUUID).getName(); - } - - public UUID getOwnerUUID() { - return ownerUUID; - } - public Location getBlocLocation() { return blocLocation; } diff --git a/src/main/java/fr/verymc/island/minions/MinionHarvest.java b/src/main/java/fr/verymc/island/minions/MinionHarvest.java index d849e12e..d2c07782 100644 --- a/src/main/java/fr/verymc/island/minions/MinionHarvest.java +++ b/src/main/java/fr/verymc/island/minions/MinionHarvest.java @@ -1,6 +1,9 @@ package main.java.fr.verymc.island.minions; import main.java.fr.verymc.Main; +import main.java.fr.verymc.island.Island; +import main.java.fr.verymc.island.IslandManager; +import main.java.fr.verymc.island.perms.IslandRanks; import main.java.fr.verymc.utils.InventoryUtils; import org.bukkit.Bukkit; import org.bukkit.Effect; @@ -12,10 +15,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; +import java.util.*; public class MinionHarvest { @@ -36,33 +36,41 @@ public void autoHarvest() { Bukkit.getScheduler().scheduleAsyncRepeatingTask(Main.instance, new Runnable() { public void run() { - ArrayList minions = new ArrayList<>(); - minions.addAll(MinionManager.instance.minions); + HashMap minions = new HashMap<>(); + for (Island island : IslandManager.instance.islands) { + for (Minion minion : island.getMinions()) { + minions.put(minion, island); + } + } Long millis = System.currentTimeMillis(); - for (Minion minion : minions) { - if (minion == null) continue; - Location blocLoc = minion.getBlocLocation(); + for (Map.Entry minion : minions.entrySet()) { + if (minion.getKey() == null) continue; + Location blocLoc = minion.getKey().getBlocLocation(); if (!Bukkit.getWorld(blocLoc.getWorld().getName()). getChunkAt(blocLoc.getBlock()).isLoaded()) continue; - if (!minion.isChestLinked()) { - Player player = Bukkit.getPlayer(minion.getOwnerUUID()); - if (player != null && player.isOnline()) invalidChestProcess(player, minion); + if (!minion.getKey().isChestLinked()) { + for (Map.Entry entry : minion.getValue().getMembers().entrySet()) { + Player player = Bukkit.getPlayer(entry.getKey()); + if (player != null && player.isOnline()) invalidChestProcess(player, minion.getKey()); + } continue; } - if (minion.getChestBloc() == null) { - Player player = Bukkit.getPlayer(minion.getOwnerUUID()); - if (player != null && player.isOnline()) invalidChestProcess(player, minion); + if (minion.getKey().getChestBloc() == null) { + for (Map.Entry entry : minion.getValue().getMembers().entrySet()) { + Player player = Bukkit.getPlayer(entry.getKey()); + if (player != null && player.isOnline()) invalidChestProcess(player, minion.getKey()); + } continue; } - Integer delay = MinionManager.instance.getMinerDelay(minion.getLevelInt()); - if (!lastAction.containsKey(minion)) { - makeAction(minion); - lastAction.put(minion, millis + delay * 1000); + Integer delay = MinionManager.instance.getMinerDelay(minion.getKey().getLevelInt()); + if (!lastAction.containsKey(minion.getKey())) { + makeAction(minion.getKey()); + lastAction.put(minion.getKey(), millis + delay * 1000); continue; } - if (lastAction.get(minion) <= millis) { - lastAction.put(minion, millis + delay * 1000); - makeAction(minion); + if (lastAction.get(minion.getKey()) <= millis) { + lastAction.put(minion.getKey(), millis + delay * 1000); + makeAction(minion.getKey()); continue; } continue; @@ -122,8 +130,13 @@ public void makeAction(Minion minion) { public void run() { if (finalBlockToBreak.getType() != null && finalBlockToBreak.getType() != Material.AIR) { if (minion.getChestBloc().getType() != Material.CHEST) { - Player player = Bukkit.getPlayer(minion.getOwnerUUID()); - if (player != null && player.isOnline()) invalidChestProcess(player, minion); + Island island = IslandManager.instance.getIslandByLoc(minion.getBlocLocation()); + if (island != null) { + for (Map.Entry entry : island.getMembers().entrySet()) { + Player player = Bukkit.getPlayer(entry.getKey()); + if (player != null && player.isOnline()) invalidChestProcess(player, minion); + } + } return; } Chest blhopper = (Chest) minion.getChestBloc().getState(); diff --git a/src/main/java/fr/verymc/island/minions/MinionManager.java b/src/main/java/fr/verymc/island/minions/MinionManager.java index c6525774..205e5fa1 100644 --- a/src/main/java/fr/verymc/island/minions/MinionManager.java +++ b/src/main/java/fr/verymc/island/minions/MinionManager.java @@ -3,6 +3,8 @@ import main.java.fr.verymc.Main; import main.java.fr.verymc.core.storage.AsyncConfig; import main.java.fr.verymc.core.storage.ConfigManager; +import main.java.fr.verymc.island.Island; +import main.java.fr.verymc.island.IslandManager; import main.java.fr.verymc.utils.PreGenItems; import org.bukkit.Bukkit; import org.bukkit.Color; @@ -33,7 +35,6 @@ public class MinionManager { public static Integer level4 = 150000; public static Integer level5 = 250000; public static Integer level6 = 500000; - public ArrayList minions = new ArrayList<>(); public ArrayList faceBloc = new ArrayList<>(); @@ -119,7 +120,7 @@ public void addMinion(Player player, Location blocLoc, MinionType minionType, Bl blocLoc.add(0.5, 1, 0.5); blocLoc.setDirection(player.getLocation().getDirection()); - Minion minion = new Minion(id, player.getUniqueId(), levelInt, blocLoc, minionType, + Minion minion = new Minion(id, levelInt, blocLoc, minionType, blockFace, false, null, false); try { @@ -127,7 +128,7 @@ public void addMinion(Player player, Location blocLoc, MinionType minionType, Bl final EntityEquipment equipment = stand.getEquipment(); stand.setMetadata("minion", new FixedMetadataValue(Main.instance, true)); stand.setVisible(true); - stand.setCustomName("§eMinion " + minionType.getName(minionType)); + stand.setCustomName("§eMinion Piocheur"); stand.setCustomNameVisible(true); stand.setGravity(false); stand.setArms(true); @@ -160,13 +161,13 @@ public void addMinion(Player player, Location blocLoc, MinionType minionType, Bl e.printStackTrace(); } - minions.add(minion); + IslandManager.instance.getPlayerIsland(player).addMinion(minion); player.sendMessage("§6§lMinions §8» §fMinion §aplacé§f, pour qu'il fonctionne, il faut lier un coffre au minion. " + "(Aller dans l'inventaire du minion puis cliquer le coffre)"); } - public void removeMinion(Minion minion) { + public void removeMinion(Minion minion, Island island) { for (Entity entity : Bukkit.getWorld(minion.getBlocLocation().getWorld().getName()).getEntities()) { if (!(entity instanceof ArmorStand)) continue; @@ -176,10 +177,10 @@ public void removeMinion(Minion minion) { } } - minions.remove(minion); + island.removeMinion(minion); HashMap toEdit = new HashMap<>(); - toEdit.put(minion.getID() + "", null); - AsyncConfig.instance.setAndSaveAsync(toEdit, ConfigManager.instance.getDataMinions(), ConfigManager.instance.minionsFile); + toEdit.put(island.getId() + ".minions." + minion.getID() + "", null); + AsyncConfig.instance.setAndSaveAsync(toEdit, ConfigManager.instance.getDataIslands(), ConfigManager.instance.islandsFile); } } diff --git a/src/main/java/fr/verymc/island/minions/MinionsCmd.java b/src/main/java/fr/verymc/island/minions/MinionsCmd.java index 53e124f3..1894aaf5 100644 --- a/src/main/java/fr/verymc/island/minions/MinionsCmd.java +++ b/src/main/java/fr/verymc/island/minions/MinionsCmd.java @@ -1,6 +1,8 @@ package main.java.fr.verymc.island.minions; import main.java.fr.verymc.Main; +import main.java.fr.verymc.island.Island; +import main.java.fr.verymc.island.IslandManager; import main.java.fr.verymc.utils.PreGenItems; import org.bukkit.Bukkit; import org.bukkit.Color; @@ -18,6 +20,8 @@ import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.util.EulerAngle; +import java.util.ArrayList; + public class MinionsCmd implements CommandExecutor { @Override @@ -45,7 +49,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } if (args[1].equalsIgnoreCase("repop")) { - for (Minion minion : MinionManager.instance.minions) { + ArrayList minions = new ArrayList<>(); + for (Island island : IslandManager.instance.islands) { + minions.addAll(island.getMinions()); + } + for (Minion minion : minions) { if (!minion.getBlocLocation().isChunkLoaded()) { minion.getBlocLocation().getChunk().load(); } @@ -68,20 +76,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St final ItemStack pants = new ItemStack(Material.LEATHER_LEGGINGS, 1); final LeatherArmorMeta lam4 = (LeatherArmorMeta) pants.getItemMeta(); lam4.setColor(Color.fromRGB(249, 128, 29)); - pants.setItemMeta((ItemMeta) lam4); + pants.setItemMeta(lam4); equipment.setLeggings(pants); final ItemStack boots = new ItemStack(Material.LEATHER_BOOTS, 1); final LeatherArmorMeta lam5 = (LeatherArmorMeta) boots.getItemMeta(); lam5.setColor(Color.fromRGB(249, 128, 29)); - boots.setItemMeta((ItemMeta) lam5); + boots.setItemMeta(lam5); equipment.setBoots(boots); equipment.setItemInMainHand(new ItemStack(Material.DIAMOND_PICKAXE)); - if (Bukkit.getPlayer(minion.getOwnerUUID()) == null) { - equipment.setHelmet(PreGenItems.instance.getHead(Bukkit.getPlayer(sender.getName()))); - } else { - equipment.setHelmet(PreGenItems.instance.getHead(Bukkit.getPlayer(minion.getOwnerUUID()))); - } + equipment.setHelmet(PreGenItems.instance.getHeadMinion()); stand.setRightLegPose(new EulerAngle(0.0, 0.0, -50.0)); stand.setLeftLegPose(new EulerAngle(0.0, 0.0, 50.0)); @@ -90,7 +94,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } if (args[1].equalsIgnoreCase("repopnearest")) { - for (Minion minion : MinionManager.instance.minions) { + ArrayList minions = new ArrayList<>(); + for (Island island : IslandManager.instance.islands) { + minions.addAll(island.getMinions()); + } + for (Minion minion : minions) { if (minion.getBlocLocation().getBlock().equals(ptarget.getLocation().getBlock())) { final ArmorStand stand = (ArmorStand) minion.getBlocLocation().getWorld().spawnEntity(minion.getBlocLocation(), EntityType.ARMOR_STAND); final EntityEquipment equipment = stand.getEquipment(); diff --git a/src/main/java/fr/verymc/island/minions/MinionsListener.java b/src/main/java/fr/verymc/island/minions/MinionsListener.java index 70599780..aefa1b61 100644 --- a/src/main/java/fr/verymc/island/minions/MinionsListener.java +++ b/src/main/java/fr/verymc/island/minions/MinionsListener.java @@ -2,6 +2,9 @@ import main.java.fr.verymc.Main; import main.java.fr.verymc.core.eco.EcoAccountsManager; +import main.java.fr.verymc.island.Island; +import main.java.fr.verymc.island.IslandManager; +import main.java.fr.verymc.island.perms.IslandPerms; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -29,8 +32,10 @@ public void onInventoryClicEvent(InventoryClickEvent e) { if (currentType == null) return; if (e.getView().getTitle() == null) return; Player player = (Player) e.getWhoClicked(); + Island island = IslandManager.instance.getPlayerIsland(player); if (e.getView().getTitle().contains("§6Menu du minion " + MinionType.PIOCHEUR.getName(MinionType.PIOCHEUR))) { e.setCancelled(true); + if (island == null) return; if (!MinionsGui.instance.minionOpened.containsKey(player.getName())) { return; } @@ -47,8 +52,12 @@ public void onInventoryClicEvent(InventoryClickEvent e) { return; } if (currentType == Material.DRAGON_BREATH) { + if (!island.hasPerms(island.getIslandRankFromUUID(player.getUniqueId()), IslandPerms.MINIONS_REMOVE, player)) { + player.sendMessage("§6§lMinions §8» §fVous n'avez pas la permission de récupérer un minion."); + return; + } MinionManager.instance.giveMinionItemForExistingMinion(player, MinionsGui.instance.minionOpened.get(player.getName())); - MinionManager.instance.removeMinion(MinionsGui.instance.minionOpened.get(player.getName())); + MinionManager.instance.removeMinion(MinionsGui.instance.minionOpened.get(player.getName()), island); Bukkit.getScheduler().scheduleSyncDelayedTask(Main.instance, new Runnable() { @Override public void run() { @@ -73,6 +82,7 @@ public void run() { } if (e.getView().getTitle().contains("§6Améliorations du minion")) { e.setCancelled(true); + if (island == null) return; if (!MinionsGui.instance.minionOpened.containsKey(player.getName())) { return; } @@ -97,6 +107,7 @@ public void run() { } if (e.getView().getTitle().contains("§6Paramètres du minion ")) { e.setCancelled(true); + if (island == null) return; if (!MinionsGui.instance.minionOpened.containsKey(player.getName())) { return; } @@ -128,6 +139,22 @@ public void interactMinion(PlayerInteractEvent e) { return; } if (locBloc.getBlock().getType() == Material.CHEST) { + if (e.isCancelled()) { + return; + } + Island island = IslandManager.instance.getPlayerIsland(player); + if (island == null) { + player.sendMessage("§6§lMinions §8» §fVous devez avoir une île pour lier un coffre."); + return; + } + if (!island.hasPerms(island.getIslandRankFromUUID(player.getUniqueId()), IslandPerms.MINIONS_INTERACT, player)) { + player.sendMessage("§6§lMinions §8» §fVous n'avez pas la permission d'intéragir avec un minion."); + return; + } + if (island == IslandManager.instance.getIslandByLoc(locBloc)) { + player.sendMessage("§6§lMinions §8» §fVous ne pouvez pas lier un coffre en dehors de votre île."); + return; + } MinionsGui.instance.minionOpened.get(player.getName()).setChestLinked(locBloc.getBlock()); MinionsGui.instance.minionOpened.remove(player.getName()); MinionsGui.instance.linking.remove(player); @@ -147,46 +174,51 @@ public void placeMinion(PlayerInteractEvent e) { if (!e.getItem().isUnbreakable()) { return; } - if (e.getItem().getType() == Material.DRAGON_BREATH && e.getPlayer().isSneaking()) { - if (e.getPlayer().getLocation().getWorld().getName().equalsIgnoreCase("world")) { + Player player = e.getPlayer(); + if (e.isCancelled()) { + return; + } + if (e.getItem().getType() == Material.DRAGON_BREATH && player.isSneaking()) { + if (!e.getItem().getDisplayName().contains(MinionType.PIOCHEUR.getName(MinionType.PIOCHEUR)) + && !e.getItem().getDisplayName().contains("§6Minion")) { return; } - int plcount = 0; - for (Minion minion : MinionManager.instance.minions) { - if (minion.getOwnerUUID().equals(e.getPlayer().getUniqueId())) { - plcount += 1; - } + Island island = IslandManager.instance.getPlayerIsland(player); + e.setCancelled(true); + if (island == null) { + e.getPlayer().sendMessage("§6§lMinions §8» §fVous devez avoir une île pour poser un minion."); + return; + } + if (player.getLocation().getWorld().getName().equalsIgnoreCase("world")) { + return; } - if (plcount >= 3) { - e.getPlayer().sendMessage("§6§lMinions §8» §fVous ne pouvez pas poser plus de 3 minions."); + if (!island.hasPerms(island.getIslandRankFromUUID(player.getUniqueId()), IslandPerms.MINIONS_ADD, player)) { + player.sendMessage("§6§lMinions §8» §fVous n'avez pas la permission de poser un minion."); return; } - for (Minion minions : MinionManager.instance.minions) { + for (Minion minions : island.getMinions()) { if (e.getClickedBlock().getLocation().getBlock().getLocation().add(0, 1, 0).equals(minions.getBlocLocation().getBlock().getLocation())) { - e.setCancelled(true); return; } } - if (e.getItem().getDisplayName().contains(MinionType.PIOCHEUR.getName(MinionType.PIOCHEUR)) - && e.getItem().getDisplayName().contains("§6Minion")) { - Player player = e.getPlayer(); - Integer levelInt = 0; - if (e.getItem().getLore().size() >= 1) { - String lore1 = e.getItem().getLore().get(0); - lore1 = lore1.replace("§6Niveau §e", ""); - levelInt = Integer.parseInt(lore1); - } - e.setCancelled(true); - if (!MinionManager.instance.faceBloc.contains(player.getFacing())) { - player.sendMessage("§6§lMinions §8» §fVous devez placer un minion dans une direction précise; " + - "sud, nord, est, ouest"); - return; - } - MinionManager.instance.addMinion(player, e.getClickedBlock().getLocation(), - MinionType.PIOCHEUR, player.getFacing(), levelInt); - player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1); + if (island.getMinions().size() >= 3) { + player.sendMessage("§6§lMinions §8» §fVous ne pouvez pas poser plus de 3 minions."); + return; + } + if (!MinionManager.instance.faceBloc.contains(player.getFacing())) { + player.sendMessage("§6§lMinions §8» §fVous devez placer un minion dans une direction précise; " + + "sud, nord, est, ouest"); return; } + Integer levelInt = 0; + if (e.getItem().getLore().size() >= 1) { + String lore1 = e.getItem().getLore().get(0); + lore1 = lore1.replace("§6Niveau §e", ""); + levelInt = Integer.parseInt(lore1); + } + MinionManager.instance.addMinion(player, e.getClickedBlock().getLocation(), + MinionType.PIOCHEUR, player.getFacing(), levelInt); + player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1); } } @@ -205,7 +237,11 @@ public void onPlayerClick(PlayerInteractAtEntityEvent e) { Entity clicked = e.getRightClicked(); if (clicked.isInvulnerable()) { Minion minion = null; - for (Minion minions : MinionManager.instance.minions) { + Island island = IslandManager.instance.getIslandByLoc(clicked.getLocation()); + if (island == null) { + return; + } + for (Minion minions : island.getMinions()) { if (!e.getRightClicked().getLocation().equals(minions.getBlocLocation())) { continue; } @@ -214,17 +250,15 @@ public void onPlayerClick(PlayerInteractAtEntityEvent e) { } if (minion == null) return; e.setCancelled(true); - if (minion.getOwnerUUID().equals(e.getPlayer().getUniqueId())) { + if (island.hasPerms(island.getIslandRankFromUUID(player.getUniqueId()), IslandPerms.MINIONS_INTERACT, player)) { MinionsGui.instance.minionMainGui(player, minion); return; } if (player.hasPermission("*")) { MinionsGui.instance.minionMainGui(player, minion); - player.sendMessage("§6§lMinions §8» §fVous venez d'entrer de force dans le minion de " + minion.getOwnerName() + "."); + player.sendMessage("§6§lMinions §8» §fVous venez d'entrer de force dans le minion de: " + island.getName() + "."); return; } - player.sendMessage("§6§lMinions §8» §fCe minion ne vous appartient pas."); - return; } } } diff --git a/src/main/java/fr/verymc/island/perms/IslandPerms.java b/src/main/java/fr/verymc/island/perms/IslandPerms.java index 6813c47c..c40f1ab6 100644 --- a/src/main/java/fr/verymc/island/perms/IslandPerms.java +++ b/src/main/java/fr/verymc/island/perms/IslandPerms.java @@ -30,7 +30,10 @@ public enum IslandPerms { BUILD("§fConstruire"), BREAK("§fCasser"), INTERACT("§fIntéragir avec les blocs"), - CONTAINER("§fOuvrir les conteneurs"); + CONTAINER("§fOuvrir les conteneurs"), + MINIONS_ADD("§6Ajouter un minion"), + MINIONS_REMOVE("§6Retirer un minion"), + MINIONS_INTERACT("§6Intéragir avec un minion"); private String description; @@ -120,6 +123,15 @@ public static ItemStack getItemStackForPerm(IslandPerms perm) { case EXPEL -> { return new ItemStack(Material.DIAMOND_HOE); } + case MINIONS_ADD -> { + return new ItemStack(Material.DRAGON_BREATH); + } + case MINIONS_REMOVE -> { + return new ItemStack(Material.DRAGON_EGG); + } + case MINIONS_INTERACT -> { + return new ItemStack(Material.DRAGON_HEAD); + } default -> { return new ItemStack(Material.AIR); } diff --git a/src/main/java/fr/verymc/utils/PreGenItems.java b/src/main/java/fr/verymc/utils/PreGenItems.java index 446e96b7..d0bcbf5b 100644 --- a/src/main/java/fr/verymc/utils/PreGenItems.java +++ b/src/main/java/fr/verymc/utils/PreGenItems.java @@ -46,6 +46,15 @@ public ItemStack getHead(Player player) { return custom10; } + @NotNull + public ItemStack getHeadMinion() { + ItemStack custom10 = new ItemStack(Material.PLAYER_HEAD, 1); + SkullMeta customi = (SkullMeta) custom10.getItemMeta(); + customi.setOwner("Farmeurimmo"); + custom10.setItemMeta(customi); + return custom10; + } + @NotNull public ItemStack getEvoPickaxe() { ItemStack evoPickaxe = (new ItemStackBuilder(Material.NETHERITE_PICKAXE, 1).setName("§6Gros Cailloux") diff --git a/todo.txt b/todo.txt index fc181a1a..c72fb041 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,3 @@ -- stockage des "minions" avec les îles - intégrer l'api du stockage - On pourrait en effet mettre en place plusieurs serveurs qui gère chacun plusieurs îles. On aurait de cette manière : ▫️ 1 (ou plusieurs) lobby (avec la map spawn)