-
Notifications
You must be signed in to change notification settings - Fork 2
InventoryHelper
InventoryHelper provides simplified inventory management utilities for working with player inventories in Hytale.
InventoryHelper makes it easy to give/remove items, check inventory contents, and manage hotbar slots without dealing with the complex inventory container API directly.
Give items to a player's inventory.
Parameters:
-
entity- The entity (must be a LivingEntity) -
itemId- The item ID (e.g., "Ingredient_Bone_Fragment", "Rock_Stone") -
quantity- The number of items to give
Returns: boolean - true if items were successfully added
Example:
// Give 10 Ingredient_Bone_Fragments to a player
boolean success = InventoryHelper.giveItem(player, "Ingredient_Bone_Fragment", 10);
if (success) {
WorldHelper.log(world, "Gave 10 Ingredient_Bone_Fragments to player!");
}Use Cases:
- Quest rewards
- Shop purchases
- Admin commands
- Custom loot systems
Remove items from a player's inventory.
Parameters:
-
entity- The entity (must be a LivingEntity) -
itemId- The item ID to remove -
quantity- The number of items to remove
Returns: boolean - true if items were successfully removed
Example:
// Remove 5 Ingredient_Bone_Fragments from player
boolean removed = InventoryHelper.removeItem(player, "Ingredient_Bone_Fragments", 5);
if (removed) {
WorldHelper.log(world, "Removed 5 Ingredient_Bone_Fragments from player");
} else {
WorldHelper.log(world, "Player doesn't have enough Ingredient_Bone_Fragments");
}Use Cases:
- Shop purchases (taking payment)
- Crafting requirements
- Quest item consumption
- Item durability systems
Check if a player has at least the specified quantity of an item.
Parameters:
-
entity- The entity (must be a LivingEntity) -
itemId- The item ID to check -
quantity- The minimum quantity required
Returns: boolean - true if player has at least the specified quantity
Example:
// Check if player has at least 10 Ingredient_Bone_Fragments
if (InventoryHelper.hasItem(player, "Ingredient_Bone_Fragments", 10)) {
WorldHelper.log(world, "Player has enough Ingredient_Bone_Fragments!");
} else {
PlayerHelper.sendMessage(player, "You need 10 Ingredient_Bone_Fragments!");
}Use Cases:
- Quest requirements
- Shop purchase validation
- Crafting prerequisites
- Access control (key items)
Count the total quantity of a specific item in the player's inventory.
Parameters:
-
entity- The entity (must be a LivingEntity) -
itemId- The item ID to count
Returns: int - The total quantity of the item (0 if none found)
Example:
// Count how many Ingredient_Bone_Fragments the player has
int fragmentCount = InventoryHelper.countItem(player, "Ingredient_Bone_Fragments");
WorldHelper.log(world, "Player has " + fragmentCount + " Ingredient_Bone_Fragments");Use Cases:
- Displaying inventory statistics
- Leaderboards (richest player)
- Quest progress tracking
- Economy systems
Get a list of all items in the player's inventory (hotbar, storage, backpack, armor, utility).
Parameters:
-
entity- The entity (must be a LivingEntity)
Returns: List<ItemStack> - List of all items in inventory
Example:
// Get all items and log them
List<ItemStack> items = InventoryHelper.getAllItems(player);
WorldHelper.log(world, "Player has " + items.size() + " item stacks");
for (ItemStack item : items) {
WorldHelper.log(world, " - " + item.getItemId() + " x" + item.getQuantity());
}Use Cases:
- Inventory analysis
- Backup/restore systems
- Death drop mechanics
- Inventory search features
Get the item in the player's currently selected hotbar slot.
Parameters:
-
entity- The entity (must be a LivingEntity)
Returns: ItemStack - The active hotbar item, or null if slot is empty
Example:
// Check what item the player is holding
ItemStack activeItem = InventoryHelper.getActiveHotbarItem(player);
if (activeItem != null) {
WorldHelper.log(world, "Player is holding: " + activeItem.getItemId());
} else {
WorldHelper.log(world, "Player's hand is empty");
}Use Cases:
- Tool requirement checks
- Item-specific abilities
- Context-sensitive actions
- Custom item interactions
Get the index of the player's currently selected hotbar slot.
Parameters:
-
entity- The entity (must be a LivingEntity)
Returns: byte - The active hotbar slot index (0-8), or -1 if error
Example:
// Get the active slot index
byte slotIndex = InventoryHelper.getActiveHotbarSlotIndex(player);
WorldHelper.log(world, "Active hotbar slot: " + slotIndex);Set a specific hotbar slot to contain an item.
Parameters:
-
entity- The entity (must be a LivingEntity) -
slotIndex- The hotbar slot index (0-8) -
itemStack- The ItemStack to place in the slot (or null to clear)
Returns: boolean - true if slot was successfully set
Example:
// Give player a Ingredient_Bone_Fragment in hotbar slot 0
ItemStack fragment = new ItemStack("Ingredient_Bone_Fragment", 1);
boolean success = InventoryHelper.setHotbarSlot(player, 0, fragment);
if (success) {
WorldHelper.log(world, "Placed Ingredient_Bone_Fragment in hotbar slot 0");
}Use Cases:
- Custom starting equipment
- Ability hotbar systems
- Quick-access item placement
- Tutorial systems
Clear all items from the player's inventory (hotbar, storage, backpack).
Parameters:
-
entity- The entity (must be a LivingEntity)
Returns: boolean - true if inventory was successfully cleared
Example:
// Clear player's inventory
boolean cleared = InventoryHelper.clearInventory(player);
if (cleared) {
PlayerHelper.sendMessage(player, "Your inventory has been cleared!");
}Use Cases:
- Admin commands
- Minigame resets
- Death penalties
- Fresh start systems
Check if the player's inventory is full (cannot accept any more items).
Parameters:
-
entity- The entity (must be a LivingEntity)
Returns: boolean - true if inventory is full
Example:
// Check if inventory is full before giving items
if (InventoryHelper.isInventoryFull(player)) {
PlayerHelper.sendMessage(player, "Your inventory is full!");
} else {
InventoryHelper.giveItem(player, "Ingredient_Bone_Fragment", 10);
}Use Cases:
- Preventing item loss
- Inventory warnings
- Shop purchase validation
- Loot distribution
Count the number of empty slots in the player's inventory.
Parameters:
-
entity- The entity (must be a LivingEntity)
Returns: int - The number of empty slots
Example:
// Check how much space the player has
int emptySlots = InventoryHelper.getEmptySlotCount(player);
WorldHelper.log(world, "Player has " + emptySlots + " empty slots");
if (emptySlots < 5) {
PlayerHelper.sendMessage(player, "Your inventory is almost full!");
}Use Cases:
- Inventory space warnings
- Loot distribution calculations
- Storage management
- UI indicators
All InventoryHelper methods require the entity to be a LivingEntity (which includes Players). Methods will return false/null/0 if called on non-living entities.
Item IDs must match the exact item identifiers used in Hytale (e.g., "Ingredient_Bone_Fragment", "Rock_Stone", "Torch"). Invalid item IDs will cause the operation to fail.
All inventory operations are thread-safe and handle exceptions gracefully. Failed operations will log warnings but won't crash your plugin.
The helper works with the following inventory sections:
- Hotbar - 9 slots (quick access bar)
- Storage - 36 slots (main inventory)
- Backpack - Additional storage slots
- Armor - Equipment slots
- Utility - Utility item slots
Here's a complete example showing common inventory operations:
// Quest system example
EventHelper.onPlayerChat(plugin, (username, message) -> {
if (message.equals("!quest")) {
Entity player = EntityHelper.getPlayerByName(world, username);
if (player == null) return;
// Check if player has quest items
if (InventoryHelper.hasItem(player, "QuestItem", 10)) {
// Remove quest items
InventoryHelper.removeItem(player, "QuestItem", 10);
// Give reward
InventoryHelper.giveItem(player, "Ingredient_Bone_Fragment", 5);
InventoryHelper.giveItem(player, "Gold", 100);
PlayerHelper.sendMessage(player, "Quest complete! Rewards given!");
} else {
int currentCount = InventoryHelper.countItem(player, "QuestItem");
PlayerHelper.sendMessage(player, "You need 10 Quest Items. You have: " + currentCount);
}
}
});- PlayerHelper - Player-specific utilities
- EntityHelper - Entity management utilities
- EventHelper - Event handling utilities