KubeJS-Mods / KubeJS

https://kubejs.com
GNU Lesser General Public License v3.0
307 stars 90 forks source link

Hoppers Can't Interact With Custom Block Entity Inventory #837

Open xXStrayXx opened 4 months ago

xXStrayXx commented 4 months ago

Minecraft Version

1.20.1

KubeJS Version

2001.6.5-build.7

Rhino Version

2001.2.2-build.18

Architectury Version

9.2.14

Forge/Fabric Version

Forge 47.2.0

Describe your issue

When creating an custom block entity like this: StartupEvents.registry("block", (event) => { event.create("test") .blockEntity((entityInfo) => { entityInfo.inventory(9, 3); entityInfo.rightClickOpensInventory(); }) }) It seems that the inventory of said block entity cannot be interacted by anything but player. Things like vanilla hoppers and create funnels completely ignore the inventory. This is probably because the inventory of the custom block entity isn't a regular inventory but rather a custom attactment. I'm not certain whether this is intentional.

Crash report/logs

No response

Classified3939 commented 4 days ago

For those who also encounter this problem on 1.20.1, PowerfulJS seems to be the solution. Here's some example code for a block that has no manual inventory, but gives an attached hopper 1 sugarcane per second:

StartupEvents.registry('block', event => { event.create('basic_sugarcane_generator') .blockEntity(blockInfo => { blockInfo.inventory(9, 1); blockInfo.serverTick(20, 0, entity => { entity.inventory.insertItem('sugar_cane', false); }), blockInfo.attachCapability( CapabilityBuilder.ITEM.blockEntity() .extractItem((blockEntity, slot, amount, simulate) => blockEntity.inventory.extractItem(slot, amount, simulate) ) .insertItem((blockEntity, slot, stack, simulate) => blockEntity.inventory.insertItem(slot, stack, simulate) ) .getSlotLimit((blockEntity, slot) => blockEntity.inventory.getSlotLimit(slot) ) .getSlots((blockEntity) => blockEntity.inventory.slots) .getStackInSlot((blockEntity, slot) => blockEntity.inventory.getStackInSlot(slot) ) ); }) [assorted block properties go here] });

note that while you might not think the "insert Item" function is needed, removing it will make any item hoppered into the block, accidentally or otherwise, vanish into oblivion.

EvanHsieh0415 commented 4 days ago

Use PowerfulJS

https://discord.com/channels/303440391124942858/1251063549145645066

StartupEvents.registry("block", (event) => {
  event.create("kubejs:barrel").blockEntity((info) => {
    info.inventory(9, 3);
    info.rightClickOpensInventory();
    info.attachCapability(
      CapabilityBuilder.ITEM.blockEntity()
        .extractItem((blockEntity, slot, amount, simulate) => blockEntity.inventory.extractItem(slot, amount, simulate))
        .insertItem((blockEntity, slot, stack, simulate) => blockEntity.inventory.insertItem(slot, stack, simulate))
        .getSlotLimit((blockEntity, slot) => blockEntity.inventory.getSlotLimit(slot))
        .getSlots((blockEntity) => blockEntity.inventory.slots)
        .getStackInSlot((blockEntity, slot) => blockEntity.inventory.getStackInSlot(slot))
        .isItemValid((blockEntity, slot, stack) => blockEntity.inventory.isItemValid(slot, stack))
        .availableOn((blockEntity, direction) => direction != Direction.UP)
    );
  });
});