Skip to content

Commit

Permalink
Rewrote clearTeamInventory to fix #847
Browse files Browse the repository at this point in the history
- should now correctly ignore visitors (since visitors are technically members?)
- should now remove data for offline players
  • Loading branch information
sh0inx committed May 13, 2024
1 parent 4120244 commit 97fabed
Showing 1 changed file with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<User> onlinePlayers = new ArrayList<>();
List<User> 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());
}
}
}

Expand Down

0 comments on commit 97fabed

Please sign in to comment.