diff --git a/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java b/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java index 051dbd6ac..d9f118ef0 100644 --- a/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java +++ b/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java @@ -1,5 +1,6 @@ package com.iridium.iridiumskyblock.managers; +import com.earth2me.essentials.commands.PlayerNotFoundException; import com.iridium.iridiumcore.dependencies.nbtapi.NBTCompound; import com.iridium.iridiumcore.dependencies.nbtapi.NBTFile; import com.iridium.iridiumcore.dependencies.nbtapi.NBTItem; @@ -41,6 +42,8 @@ import org.jetbrains.annotations.Nullable; import java.io.File; +import java.io.IOException; +import java.rmi.NoSuchObjectException; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -748,14 +751,52 @@ public void handleBlockPlaceOutsideTerritory(BlockPlaceEvent blockEvent) { public void clearTeamInventory(Island island) { - if (IridiumSkyblock.getInstance().getConfiguration().clearInventoryOnRegen) { - IridiumSkyblock.getInstance().getIslandManager().getMembersOnIsland(island).forEach(member -> - member.getPlayer().getInventory().clear()); + List onlinePlayers = new ArrayList<>(); + List offlinePlayers = new ArrayList<>(); + + for(User user : island.getMembers()) { + + if (user.getUserRank() == -1) continue; + + try{ + user.getPlayer(); + onlinePlayers.add(user); + } catch (Exception e) { + IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); + offlinePlayers.add(user); + } } - if (IridiumSkyblock.getInstance().getConfiguration().clearEnderChestOnRegen) { - IridiumSkyblock.getInstance().getIslandManager().getMembersOnIsland(island).forEach(member -> - member.getPlayer().getEnderChest().clear()); + for (User user : onlinePlayers) { + if (IridiumSkyblock.getInstance().getConfiguration().clearInventoryOnRegen) user.getPlayer().getInventory().clear(); + if (IridiumSkyblock.getInstance().getConfiguration().clearEnderChestOnRegen) user.getPlayer().getEnderChest().clear(); + } + + for(User user : offlinePlayers) { + + try { + File file = new File(Bukkit.getWorlds().get(0).getWorldFolder().getPath() + File.pathSeparator + "playerdata" + File.pathSeparator + user.getUuid() + ".dat"); + NBTFile playerFile = new NBTFile(file); + + if (IridiumSkyblock.getInstance().getConfiguration().clearInventoryOnRegen) { + NBTCompound compound = playerFile.getCompound("").getCompound("Inventory"); + compound.clearNBT(); + playerFile.save(); + } + + if (IridiumSkyblock.getInstance().getConfiguration().clearEnderChestOnRegen) { + NBTCompound compound = playerFile.getCompound("").getCompound("EnderItems"); + compound.clearNBT(); + playerFile.save(); + } + + } catch (IOException e) { + IridiumSkyblock.getInstance().getLogger().warning("Cannot mutate user: " + user.getName() + ". See stacktrace for details."); + IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); + } catch (NullPointerException e) { + IridiumSkyblock.getInstance().getLogger().warning("Cannot mutate user: " + user.getName() + ". Either player or compound doesn't exist (See stacktrace for details)."); + IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); + } } }