Skip to content

Player inventory stash

PlayerInventoryStash snapshots a player’s inventory, equipment, selected hotbar slot, and experience to memory, then restores it later. Every minigame with a phase swap — meeting rooms, voting screens, mid-round shops, spectator transitions — wants the same primitive: hand the player phase-specific items, then put their real loadout back.

  • A round phase needs the player to hold engine-supplied items (voting tokens, settings book).
  • A player is going into spectator mode and you want their normal inventory back when they’re un-spec’d.
  • Any phase swap where “remember what they had” is simpler than re-deriving it.
package me.zlex.conduit.player;
public final class PlayerInventoryStash {
public record Snapshot(
List<ItemStack> items,
Map<EquipmentSlot, ItemStack> equipment,
int selectedSlot,
int totalExperience,
float xpProgress,
int xpLevel
) {}
public static void stash(ServerPlayer player);
public static void restore(ServerPlayer player);
public static boolean isStashed(UUID id);
public static void discard(UUID id);
}

stash does not modify the inventory — callers typically follow it with inventory.clearContent() and then hand out the phase items. A second stash for the same player overwrites the first.

import me.zlex.conduit.player.PlayerInventoryStash;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
public final class MeetingPhase {
public static void enter(Collection<ServerPlayer> players) {
for (ServerPlayer p : players) {
PlayerInventoryStash.stash(p);
p.getInventory().clearContent();
p.getInventory().setItem(0, new ItemStack(Items.PAPER)); // vote token
p.getInventory().setItem(8, new ItemStack(Items.BARRIER)); // skip vote
}
}
public static void exit(Collection<ServerPlayer> players) {
for (ServerPlayer p : players) {
p.getInventory().clearContent();
PlayerInventoryStash.restore(p);
}
}
}
  • Stashes live in memory only. A server crash mid-phase loses them — acceptable for ephemeral rounds, not for durable swaps.
  • Always pair stash and restore. If you stash twice without restore, the first snapshot is gone.
  • Server-thread only. The internal map is unsynchronised.