Skip to content

Inventory Object

The inventory object represents the inventory of a container block (chest, barrel, hopper, furnace, etc.) or a player. It is obtained through world.getInventory() or player.getInventory().

js
// Inventory of a container block (chest, barrel, etc.)
var inv = world.getInventory(x, y, z);
if (inv !== null) {
    // The block has an inventory
}

// Player inventory
var inv = player.getInventory();

World transactions

world.getInventory() only works from inside an event or a command, not in onEnable(). See World Object for details.

Methods

MethodReturnsDescription
getType()stringInventory type: "player", "chest", "barrel", "hopper", "furnace", "blast_furnace", "smoker", "brewing_stand", "container"
getSize()numberTotal number of slots in the inventory
getItem(slot)item|nullItem in the given slot (Item object). null if empty
setItem(slot, name, count)booleanPlace an item in the given slot. Returns true on success
addItem(name, count)numberAdd to the first available slot. Returns the amount that could not be added (0 = full success)
removeItem(name, count)booleanRemove the given amount of the item. Returns true on success
clear()Empty all slots in the inventory
contains(name)booleantrue if the inventory contains at least 1 of the given item
count(name)numberTotal count of the given item across all slots
getItems(){slot, item}[]Array of all non-empty slots (item is Item object)
setContents(items)booleanReplace all contents. items: array of {slot, item} or {slot, name, count}. Unspecified slots are cleared

Virtual inventories (menus)

The API includes Bukkit-style virtual menus using the inv library.

js
var menu = inventory.createMenu({
    title: "§aVirtual Menu",
    type: "chest",   // chest | hopper | barrel | dropper | ender_chest
    size: 27          // 9, 18, 27, 36, 45, 54 (chest only)
});

menu.setItems([
    { slot: 0, name: "minecraft:diamond", count: 1 },
    { slot: 1, name: "minecraft:gold_ingot", count: 8 }
]);

// You can also use patterns (Bukkit-like)
menu.pattern([
    "_________",
    "__xxx____",
    "__x_x____",
    "__xxx____"
], {
    x: { name: "minecraft:black_stained_glass_pane", count: 1 }
});

menu.onClick(function(player, item, click) {
    player.sendMessage("Click: " + click + " on " + item.getName());
});

// click can be: left_click, right_click, drop

menu.onClose(function(player) {
    player.sendMessage("Menu closed");
});

menu.open(player);      // open
menu.update(player);    // update
menu.close(player);     // close

Notes:

  • menu.open/update/close accept a player wrapper or a player name (string).
  • Menus are client-side (not real blocks in the world).

Examples

Inspecting a chest

js
commands.register("inspectchest", "View chest contents in front of you", function(player, args) {
    var x = Math.floor(player.getX());
    var y = Math.floor(player.getY());
    var z = Math.floor(player.getZ() + 1); // block in front of player

    var inv = world.getInventory(x, y, z);
    if (inv === null) {
        player.sendMessage("§cNo container there.");
        return;
    }

    var items = inv.getItems();
    player.sendMessage("§eChest contents (§f" + items.length + "§e items):");
    for (var i = 0; i < items.length; i++) {
        player.sendMessage("§7Slot §f" + items[i].slot + "§7: §f" + items[i].item.getName() + " x" + items[i].item.getCount());
    }
});

Filling a chest

js
commands.register("fillchest", "Fill the chest in front of you with diamonds", function(player, args) {
    var x = Math.floor(player.getX());
    var y = Math.floor(player.getY());
    var z = Math.floor(player.getZ() + 1);

    var inv = world.getInventory(x, y, z);
    if (inv === null) {
        player.sendMessage("§cNo container there.");
        return;
    }

    inv.clear();
    var leftover = inv.addItem("minecraft:diamond", 1000);
    player.sendMessage("§bDiamonds added! Leftover: §f" + leftover);
});

Player inventory

js
events.on("PlayerJoin", function(event) {
    var p = event.getPlayer();
    var inv = p.getInventory();

    // Check what the player has
    var items = inv.getItems();
    console.log(p.getName() + " has " + items.length + " item types");

    // Give welcome item if player has no diamonds
    if (!inv.contains("minecraft:diamond")) {
        inv.addItem("minecraft:diamond", 1);
        p.sendMessage("§b¡Welcome diamond!");
    }
});

Check and remove items

js
commands.register("pay", "Pay 5 diamonds to gain access", function(player, args) {
    var inv = player.getInventory();

    if (inv.count("minecraft:diamond") < 5) {
        player.sendMessage("§cYou need 5 diamonds to access.");
        return;
    }

    inv.removeItem("minecraft:diamond", 5);
    player.sendMessage("§aAccess granted! 5 diamonds charged.");
});

Blocks with inventory

BlockName in JS
Chestminecraft:chest
Trapped chestminecraft:trapped_chest
Barrelminecraft:barrel
Hopperminecraft:hopper
Furnaceminecraft:furnace
Blast furnaceminecraft:blast_furnace
Smokerminecraft:smoker
Dispenserminecraft:dispenser
Dropperminecraft:dropper

Dragonfly Script API