Exécuter des fonctions au déplacement d'un ItemStack dans un Container



  • Bonjour à tous.

    J'ai créé une petite fonction qui doit être exécutée à chaque fois qu'on interagit avec les objets dans un container lui aussi fait maison.
    J'ai donc une TE, un Container, une Gui et un BlockContainer.

    Je n'ai pourtant pas trouvé une seule fonction appelée dans cette situation, même sur internet.

    Pouvez-vous m'aider ?

    Merci d'avance.


  • Administrateurs

    public ItemStack transferStackInSlot(EntityPlayer player, int slotId)
    Dans le container, cette fonction est déclenché dès qu'un item stack est placé dans un slot.



  • Merci.

    En revanche, cela ne fonctionne pas, mais ça doit définitivement être ma faute.

    J'ai ceci dans mon Container :

    private TileEntityEUsInjector tileEntity;
    
    public ItemStack transferStackInSlot(EntityPlayer player, int slotId)
    {
    ItemStack itemstack = null;
    Slot slot = (Slot)this.inventorySlots.get(slotId);
    
    tileEntity.countBattery();
    
    if(slot != null && slot.getHasStack())
    {
    ItemStack itemstack1 = slot.getStack();
    itemstack = itemstack1.copy();
    
    if(slotId < 9)
    {
    if(!this.mergeItemStack(itemstack1, 9, this.inventorySlots.size(), true))
    {
    return null;
    }
    }
    else if(!this.mergeItemStack(itemstack1, 0, 9, false))
    {
    return null;
    }
    
    if(itemstack1.stackSize == 0)
    {
    slot.putStack((ItemStack)null);
    }
    else
    {
    slot.onSlotChanged();
    }
    }
    return itemstack;
    
    }
    

    On y aperçoit l'appel d'une fonction countBattery présente dans la TE du Container. Voici cette fonction :

    public void countBattery() {
    int upgrades = 0;
    // TODO Debug
    if (this.inventory[0].getUnlocalizedName() == "quantumUpgrade") upgrades++;
    if (this.inventory[1].getUnlocalizedName() == "quantumUpgrade") upgrades++;
    if (this.inventory[2].getUnlocalizedName() == "quantumUpgrade") upgrades++;
    if (this.inventory[3].getUnlocalizedName() == "quantumUpgrade") upgrades++;
    System.out.println("Upgrades: " + upgrades);
    if (upgrades == 0) this.setMaxVoltage(32); else if (upgrades == 1) this.setMaxVoltage(128); else if (upgrades == 2) this.setMaxVoltage(512); else if (upgrades == 3) this.setMaxVoltage(2048); else if (upgrades == 4) this.setMaxVoltage(-1);
    }
    

    (this.inventory étant l'inventaire relatif à la TE)

    Ainsi, cette fonction est sensée, une fois exécutée, effectuer setMaxVoltage en conséquence. Elle print aussi dans les logs une manifestation.

    Hors, lorsque je déplace un objet dans mon container, aucune réaction.
    Pouvez-vous m'aider ?

    Encore une fois merci d'avance.


  • Administrateurs

    Alors en effet la fonction n'est que déclenché quand on utilise shift + clic.
    J'ai rapidement regardé, dans le tile entity, la fonction setInventorySlotContents est déclenché quand on met un item, mais aussi quand on ouvre le container. decrStackSize elle est déclenché quand on prend un item du container.

    Donc au final j'ai pas trouvé ce que tu cherches 😕



  • A la limite c'est pas très grave si elle est exécutée un peu plus. En fait, j'aurai bien voulu qu'elle soit exécuté en permanence, mais ça pomperait de la ressource serveur. Mais là vu que tu me dis ces fonctions, je vais tester.

    Merci.

    EDIT : Alors j'ai placé la fonction comme ceci sans setInventorySlotContents :

    @Override
    public void setInventorySlotContents(int slotId, ItemStack stack)
    {
    this.inventory[slotId] = stack;
    
    if (stack != null && stack.stackSize > this.getInventoryStackLimit())
    {
    stack.stackSize = this.getInventoryStackLimit();
    }
    
    countBattery();
    
    this.markDirty();
    }
    

    Mais à l'ouverture du GUI, j'ai le temps de l'aperçevoir et puis vlan :

    [10:31:10] [Client thread/FATAL]: Unreported exception thrown!
    java.lang.NullPointerException
    at pingo.virtualcraft.common.TileEntityEUsInjector.countBattery(TileEntityEUsInjector.java:295) ~[TileEntityEUsInjector.class:?]
    at pingo.virtualcraft.common.TileEntityEUsInjector.setInventorySlotContents(TileEntityEUsInjector.java:255) ~[TileEntityEUsInjector.class:?]
    at net.minecraft.inventory.Slot.putStack(Slot.java:104) ~[Slot.class:?]
    at net.minecraft.inventory.Container.putStacksInSlots(Container.java:560) ~[Container.class:?]
    at net.minecraft.client.network.NetHandlerPlayClient.handleWindowItems(NetHandlerPlayClient.java:1205) ~[NetHandlerPlayClient.class:?]
    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:70) ~[S30PacketWindowItems.class:?]
    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:78) ~[S30PacketWindowItems.class:?]
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247) ~[NetworkManager.class:?]
    at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:321) ~[PlayerControllerMP.class:?]
    at net.minecraft.client.Minecraft.runTick(Minecraft.java:1693) ~[Minecraft.class:?]
    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1039) ~[Minecraft.class:?]
    at net.minecraft.client.Minecraft.run(Minecraft.java:961) [Minecraft.class:?]
    at net.minecraft.client.main.Main.main(Main.java:164) [Main.class:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_60]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_60]
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:134) [launchwrapper-1.9.jar:?]
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.9.jar:?]
    –-- Minecraft Crash Report ----
    // Oh - I know what I did wrong!
    
    Time: 19/07/14 10:31
    Description: Unexpected error
    
    java.lang.NullPointerException: Unexpected error
    at pingo.virtualcraft.common.TileEntityEUsInjector.countBattery(TileEntityEUsInjector.java:295)
    at pingo.virtualcraft.common.TileEntityEUsInjector.setInventorySlotContents(TileEntityEUsInjector.java:255)
    at net.minecraft.inventory.Slot.putStack(Slot.java:104)
    at net.minecraft.inventory.Container.putStacksInSlots(Container.java:560)
    at net.minecraft.client.network.NetHandlerPlayClient.handleWindowItems(NetHandlerPlayClient.java:1205)
    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:70)
    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:78)
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)
    at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:321)
    at net.minecraft.client.Minecraft.runTick(Minecraft.java:1693)
    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1039)
    at net.minecraft.client.Minecraft.run(Minecraft.java:961)
    at net.minecraft.client.main.Main.main(Main.java:164)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    
    A detailed walkthrough of the error, its code path and all known details is as follows:
    ---------------------------------------------------------------------------------------
    
    -- Head --
    Stacktrace:
    at pingo.virtualcraft.common.TileEntityEUsInjector.countBattery(TileEntityEUsInjector.java:295)
    at pingo.virtualcraft.common.TileEntityEUsInjector.setInventorySlotContents(TileEntityEUsInjector.java:255)
    at net.minecraft.inventory.Slot.putStack(Slot.java:104)
    at net.minecraft.inventory.Container.putStacksInSlots(Container.java:560)
    at net.minecraft.client.network.NetHandlerPlayClient.handleWindowItems(NetHandlerPlayClient.java:1205)
    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:70)
    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:78)
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)
    at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:321)
    
    -- Affected level --
    Details:
    Level name: MpServer
    All players: 1 total; [EntityClientPlayerMP['pingoleon60'/4, l='MpServer', x=-510,24, y=58,62, z=398,18]]
    Chunk stats: MultiplayerChunkCache: 190, 190
    Level seed: 0
    Level generator: ID 01 - flat, ver 0\. Features enabled: false
    Level generator options:
    Level spawn location: World: (-499,4,423), Chunk: (at 13,0,7 in -32,26; contains blocks -512,0,416 to -497,255,431), Region: (-1,0; contains chunks -32,0 to -1,31, blocks -512,0,0 to -1,255,511)
    Level time: 261138 game time, 34202 day time
    Level dimension: 0
    Level storage version: 0x00000 - Unknown?
    Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
    Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
    Forced entities: 1 total; [EntityClientPlayerMP['pingoleon60'/4, l='MpServer', x=-510,24, y=58,62, z=398,18]]
    Retry entities: 0 total; []
    Server brand: fml,forge
    Server type: Integrated singleplayer server
    Stacktrace:
    at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:417)
    at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2568)
    at net.minecraft.client.Minecraft.run(Minecraft.java:990)
    at net.minecraft.client.main.Main.main(Main.java:164)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    
    – System Details --
    Details:
    Minecraft Version: 1.7.10
    Operating System: Windows 7 (amd64) version 6.1
    Java Version: 1.7.0_60, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 1045615912 bytes (997 MB) / 1524105216 bytes (1453 MB) up to 7622623232 bytes (7269 MB)
    JVM Flags: 0 total;
    AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
    IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
    FML: MCP v9.05 FML v7.10.18.1180 Minecraft Forge 10.13.0.1180 11 mods loaded, 11 mods active
    mcp{9.05} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    FML{7.10.18.1180} [Forge Mod Loader] (forgeSrc-1.7.10-10.13.0.1180.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    Forge{10.13.0.1180} [Minecraft Forge] (forgeSrc-1.7.10-10.13.0.1180.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    IC2{2.2.521-experimental} [IndustrialCraft 2] (industrialcraft-2-2.2.521-experimental-dev.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    BuildCraft|Core{@VERSION@} [BuildCraft] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    BuildCraft|Builders{@VERSION@} [BuildCraft Builders] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    BuildCraft|Energy{@VERSION@} [BuildCraft Energy] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    BuildCraft|Factory{@VERSION@} [BuildCraft Factory] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    BuildCraft|Transport{@VERSION@} [Buildcraft Transport] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    BuildCraft|Silicon{@VERSION@} [BuildCraft Silicon] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    virtualcraft{0.1.0-ALPHADEV} [VirtualCraft] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
    Launched Version: 1.6
    LWJGL: 2.9.1
    OpenGL: GeForce GTX 780/PCIe/SSE2 GL version 4.4.0, NVIDIA Corporation
    GL Caps: Using GL 1.3 multitexturing.
    Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.
    Anisotropic filtering is supported and maximum anisotropy is 16.
    Shaders are available because OpenGL 2.1 is supported.
    
    Is Modded: Definitely; Client brand changed to 'fml,forge'
    Type: Client (map_client.txt)
    Resource Packs: []
    Current Language: ~~ERROR~~ NullPointerException: null
    Profiler Position: N/A (disabled)
    Vec3 Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
    Anisotropic Filtering: Off (1)
    #@!@# Game crashed! Crash report saved to: #@!@# G:\Modding\1.7.2\forge\.\crash-reports\crash-2014-07-19_10.31.10-client.txt
    AL lib: (EE) alc_cleanup: 1 device not closed
    

    EDIT 2 : Après test, si je mets ma fonction juste avant les this.markDirty() dans decrStackSize, je peux l'ouvrir, je peux placer des trucs dedans, mais quand je retire, boum crash. Je pense que c'est ma fonction qui déconne. Je vais faire des tests.

    EDIT 3 : Ok résolu ! En fait c'était ma faute, je vérifiais pas si le ItemStack était pas nul (ce qui est le cas quand le slot est vide). Merci pour votre aide !