Entity Spawn Menu

SpawnMenu.js
log.info("SpawnEntity menu loaded");

const inventoryApi = Services.get("InventoryApi");
const EntityType = org.bukkit.entity.EntityType;

// MENU TEMPLATE 
const menuTemplate = inventoryApi.constructInventory("single", "§8Spawn Entity");

const creeperItem = inventoryApi.createItem({
    id: "minecraft:creeper_head",
    name: "§aSpawn Creeper"
});

const skeletonItem = inventoryApi.createItem({
    id: "minecraft:skeleton_skull",
    name: "§fSpawn Skeleton"
});

const zombieItem = inventoryApi.createItem({
    id: "minecraft:zombie_head",
    name: "§2Spawn Zombie"
});

const villagerItem = inventoryApi.createItem({
    id: "minecraft:player_head",
    name: "§eSpawn Villager"
});

menuTemplate.setSlot(1, creeperItem);
menuTemplate.setSlot(3, skeletonItem);
menuTemplate.setSlot(5, zombieItem);
menuTemplate.setSlot(7, villagerItem);

// MENU HANDLER
function openSpawnMenu(player) {
    const menu = menuTemplate.copy();

    menu.onLeftClick(function(player, slot, event) {
        event.setCancelled(true);

        const loc = player.getLocation();
        const world = loc.getWorld();

        task.main(function() {
            switch (slot) {
                case 1:
                    world.spawnEntity(loc, EntityType.CREEPER);
                    break;
                case 3:
                    world.spawnEntity(loc, EntityType.SKELETON);
                    break;
                case 5:
                    world.spawnEntity(loc, EntityType.ZOMBIE);
                    break;
                case 7:
                    world.spawnEntity(loc, EntityType.VILLAGER);
                    break;
                default:
                    return;
            }
        });

        menu.hide(player);
    });

    menu.onRightClick(function(_, _, event) {
        event.setCancelled(true);
    });

    menu.onItemPlaced(function(_, _, event) {
        event.setCancelled(true);
    });

    menu.onClosed(function() {
        menu.destroy();
    });

    menu.show(player);
}

// COMMAND
addCommand("spawnEntity", {
    onCommand: function(sender) {
        if (!(sender instanceof org.bukkit.entity.Player)) {
            sender.sendMessage("§cThis command can only be used by players.");
            return true;
        }

        openSpawnMenu(sender);
        return true;
    }
});

// CLEANUP
task.bindToUnload(function() {
    log.info("SpawnEntity menu unloaded");
});

Last updated

Was this helpful?