vr-oneslot/.minecraft/kubejs/server_scripts/onebar.js
2025-02-15 09:03:16 -08:00

49 lines
No EOL
2.1 KiB
JavaScript

PlayerEvents.tick(e => {
const {player, server} = e;
const {persistentData: pData} = player;
pData.slot = player.selectedSlot;
// ensure the player is only able to use the first 3 hotbar slots
server.scheduleInTicks(2, () => {
if([0, 1, 2].includes(pData.slot)) return;
if (player.selectedSlot === 8) {
player.setSelectedSlot(2)
}
if (player.selectedSlot >= 3) {
player.setSelectedSlot(0)
}
})
})
// `inventory` slots 36-44 are the hotbar slots (0-8)
PlayerEvents.inventoryChanged(e => {
const { player, slot, item, level } = e;
const lockedItem = Item.of('kubejs:slotlock');
for (let i = 0; i < player.inventory.getSize(); i++) {
if (i >= 36 && i <= 44) {
// TODO: Fix this as its just not working.
// The intended behavior is that for hotbar slots 4 through 9, they are permanently locked to contain `kubejs:slotlock`,
// and any instance of `kubejs:slotlock` in other slots is removed.
// if an item *somehow* gets into a locked slot, it is removed, then given back to the player after the slot is locked again.
// Hotbar slots: Ensure they always contain `kubejs:slotlock`
if (!player.inventory.get(i).equals(lockedItem)) {
if (!player.inventory.get(i).isEmpty()) {
level.server.runCommandSilent(`clear ${player.username} ${player.inventory.get(i).id} 1`); // Remove incorrect item
level.server.runCommandSilent(`give ${player.username} ${player.inventory.get(i).id} 1`); // Return the item to the player
}
// player.inventory.set(i, lockedItem); // Force slot to contain `kubejs:slotlock`
}
} else {
// Non-hotbar slots: Remove any extra `kubejs:slotlock`
if (player.inventory.get(i)?.equals(lockedItem)) {
level.server.runCommand(`clear ${player.username} kubejs:slotlock 1 from ${i}`); // Remove extra instance from specific slot
}
}
}
});