From eecb2bb04cffa704a7d0594e3758ecdd99f789a8 Mon Sep 17 00:00:00 2001 From: Dakroach Date: Thu, 11 Jun 2026 17:58:40 -0700 Subject: [PATCH] Enhance sword management: implement comprehensive removal from all inventory types and ensure player state is saved. --- .../net/kewwbec/hub/koth/KothService.java | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/kewwbec/hub/koth/KothService.java b/src/main/java/net/kewwbec/hub/koth/KothService.java index f0a5f68..4e206f6 100644 --- a/src/main/java/net/kewwbec/hub/koth/KothService.java +++ b/src/main/java/net/kewwbec/hub/koth/KothService.java @@ -6,7 +6,9 @@ import com.hypixel.hytale.logger.HytaleLogger; import com.hypixel.hytale.server.core.HytaleServer; import com.hypixel.hytale.server.core.Message; import com.hypixel.hytale.server.core.entity.entities.Player; +import com.hypixel.hytale.server.core.inventory.Inventory; import com.hypixel.hytale.server.core.inventory.ItemStack; +import com.hypixel.hytale.server.core.inventory.container.ItemContainer; import com.hypixel.hytale.server.core.universe.PlayerRef; import com.hypixel.hytale.server.core.universe.Universe; import com.hypixel.hytale.server.core.universe.world.World; @@ -76,6 +78,7 @@ public final class KothService { tickTask = null; } for (ArenaPlayer ap : arenaPlayers.values()) { + removeSword(ap.playerRef); safeOnWorld(ap.playerRef, () -> ap.hud.clear()); } arenaPlayers.clear(); @@ -115,6 +118,8 @@ public final class KothService { accumulatedMs.computeIfAbsent(uuid, k -> 0L); giveSword(playerRef); if (hudVisibility != null) hudVisibility.showAll(playerRef); + } else { + removeSword(playerRef); } ap.hud.render(buildSnapshot(uuid, computeRanking())); } @@ -123,7 +128,7 @@ public final class KothService { UUID uuid = playerRef.getUuid(); ArenaPlayer removed = arenaPlayers.remove(uuid); if (removed != null) removed.hud.clear(); - if (phase == Phase.ACTIVE) removeSword(playerRef); + removeSword(playerRef); if (hudVisibility != null) hudVisibility.applyHidden(playerRef); currentCapturers.remove(uuid); } @@ -337,13 +342,36 @@ public final class KothService { private void giveSword(@Nonnull PlayerRef ref) { short slot = (short) Math.max(0, cfg.sword_slot); - runOnPlayerEntity(ref, player -> - player.getInventory().getHotbar().addItemStackToSlot(slot, new ItemStack(cfg.sword_item_id, 1))); + runOnPlayerEntity(ref, player -> { + player.getInventory().getHotbar().addItemStackToSlot(slot, new ItemStack(cfg.sword_item_id, 1)); + player.markNeedsSave(); + }); } private void removeSword(@Nonnull PlayerRef ref) { - runOnPlayerEntity(ref, player -> - player.getInventory().getHotbar().removeItemStack(new ItemStack(cfg.sword_item_id, 1))); + runOnPlayerEntity(ref, player -> { + Inventory inventory = player.getInventory(); + boolean removed = false; + removed |= removeSwordFrom(inventory.getHotbar()); + removed |= removeSwordFrom(inventory.getStorage()); + removed |= removeSwordFrom(inventory.getBackpack()); + removed |= removeSwordFrom(inventory.getUtility()); + removed |= removeSwordFrom(inventory.getTools()); + if (removed) player.markNeedsSave(); + }); + } + + private boolean removeSwordFrom(@Nullable ItemContainer container) { + if (container == null) return false; + final boolean[] removed = {false}; + container.replaceAll((slot, stack) -> { + if (!ItemStack.isEmpty(stack) && cfg.sword_item_id.equals(stack.getItemId())) { + removed[0] = true; + return ItemStack.EMPTY; + } + return stack; + }); + return removed[0]; } private void runOnPlayerEntity(@Nonnull PlayerRef ref, -- 2.47.3