Sponge icon indicating copy to clipboard operation
Sponge copied to clipboard

Bug inventory et ItemStack

Open SimonBHB opened this issue 6 years ago • 5 comments
trafficstars

I am currently running

  • SpongeForge version: 7.2.0-SNAPSHOT/
  • Forge version: 1.12.2-14.23.5.2825
  • Java version:1.8.0_181
  • Operating System: W10
  • Plugins/Mods: spongeForge

Issue Description

Code test:

chest.getInventory().query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(0))).set(ItemStack.of(ItemTypes.DIAMOND));
chest.getInventory().query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(1))).set(ItemStack.of(ItemTypes.REDSTONE_BLOCK));
chest.getInventory().query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(2))).set(ItemStack.of(ItemTypes.TORCH));

Bug: image


Autre problème: Quand je déserialize un ItemStack je perds mes key comme si elles étaient ignoré.

translate.google.fr: other bug: when I deserialize an ItemStack I lose my key as if they were ignored.

Use ExampleDataPlugin: https://github.com/pie-flavor/ExampleDataPlugin

My code: MyImmutableInventoryData: https://pastebin.com/UbZGGhFD MyInventoryData: https://pastebin.com/1JeLmciS MyInventoryDataBuilder: https://pastebin.com/u6SAg5vb

Register<Key<?>/GamePreInitializationEvent : https://pastebin.com/k9WUwRYJ code serialize/unserialize: https://pastebin.com/tKRNiqTX

json conf: https://pastebin.com/NJpB4Rfc toContainer serialize: https://pastebin.com/2vJ3HLPf toContainer unserialize: https://pastebin.com/K28jzUGc

SimonBHB avatar Sep 08 '19 10:09 SimonBHB

For the DataRegistration, you need to put that in a GameRegistry.Register listener as well:

    @Listener
    public void onDataRegister(GameRegistryEvent.Register<DataRegistration<?, ?>> event) {
        event.register(DataRegistration.builder()
                .name("My Inventory Data")
                .id("inventory_data")
                .dataClass(MyInventoryData.class)
                .immutableClass(MyImmutableInventoryData.class)
                .builder(new MyInventoryDataBuilder()).build());
    }

Not sure about the inventory bug though.

JBYoshi avatar Sep 29 '19 21:09 JBYoshi

@Faithcaio

    @Listener
    public void run(InteractBlockEvent e) {
        Chest chest = (Chest) e.getTargetBlock().getLocation().get().getTileEntity().filter(Chest.class::isInstance).orElse(null);
        if (chest == null) return;
        TileEntityInventory<TileEntityCarrier> inv = chest.getInventory();
        QueryOperation<InventoryProperty<?, ?>> query = QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(0));
        Inventory result = inv.query(query);
        for (Inventory slot : result.slots()) {
            slot.set(ItemStack.of(ItemTypes.DIAMOND));
        }
    }

Puts a diamond in the first slot of every row and the first slot of every column.

...So how is a plugin supposed to specify whether they want "the first slot in a 2D grid" or "the first slot in every row/column in a 2D grid"?

JBYoshi avatar Sep 29 '19 21:09 JBYoshi

SlotPos.of(0, 0) or just don't iterate over returned inv. and offer to first one.

XakepSDK avatar Sep 29 '19 21:09 XakepSDK

Problem is, I don't think using SlotIndex with arbitrary values works properly with this. You could use SlotPos if you want to treat it as a 2D form, but if you use SlotIndex it won't work that way. You'd have to manually check for stuff like this, which (I'm pretty sure) is not how the SlotIndex property was designed.

JBYoshi avatar Sep 29 '19 21:09 JBYoshi

Actually, I think I found an alternative option that should work better.

OrderedInventory inventory = (OrderedInventory) chest.getInventory();
inventory.getSlot(SlotIndex.of(0)).set(ItemStack.of(ItemTypes.DIAMOND));

If you want to use 2D coordinates, you can also replace OrderedInventory with GridInventory and SlotIndex.of(0) with SlotPos.of(0, 0).

JBYoshi avatar Sep 29 '19 23:09 JBYoshi