Jump to content

Item despawn chest


Stroam

Recommended Posts

So right now I am trying to get all items that are about to despawn to instead go into a chest spawned at the location. So far I have found the forge ItemExpireEvent and trying to use

to hook into the event. I don't have very much so far and I feel like I'm already messing up somewhere since I don't know how to get the ItemEntity, or the location of the ItemEntity to be able to spawn a chest there. I am using the latest forge and I know the video is for 1.8 but figured it would at least get me close. I looking for a bit of guidance, updated info, and explanation.

 

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.item.ItemExpireEvent;

/**
* Created by Stroam on 11/3/2016.
*/
public class ItemDespawn {

    public ItemDespawn()
    {
        MinecraftForge.EVENT_BUS.register(this);
    }

    @SubscribeEvent
    public void itemDespawnEvent(ItemExpireEvent event){

    }

}

 

You don't need to point out what's wrong with the programmer. I already know. Now what's wrong with the code?

Link to comment
Share on other sites

You get the EntityItem from

ItemExpireEvent#getEntityItem()

.

I also advice you to create a BlockPos, from the EntityItem's x, y & z coords, and scan in all cardinal directions (for-loop through EnumFacing.VALUES, and offset the BlockPos) for another chest. If none exists, create one at the BlockPos, and insert the ItemExpireEvent#getEntityItem()#getEntityItem() (The stack stored in the entity) into the chest.

 

To insert the item, use the ItemHandler Capability, don't cast the chest to IInventory.

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

Thank you for the reply. The chest is a place holder right now. I'm going to be making a custom container so I don't need to worry about other chests. I'm afraid I am going to need some hand holding as ItemExpiresEvent#getEntityItem() confuses me. I don't know what the # means and when typed as is into the function it's just red. Here's my attempt with the information you have given me. I now realize I did cast to IInventory, sorry about that. Is there an example of using itemHandler capability and what are the advantages of doing it that way?

 

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.item.ItemExpireEvent;



/**
* Created by Stroam on 11/3/2016.
*/
public class ItemDespawn {

    public ItemDespawn()
    {
        MinecraftForge.EVENT_BUS.register(this);
    }

    @SubscribeEvent
    public void itemDespawnEvent(ItemExpireEvent event){
        int x,y,z;
        x = (int) event.entity.posX;
        y = (int) event.entity.posY;
        z = (int) event.entity.posZ;
        event.entity.worldObj.setBlock(x, y, z, Blocks.chest);
        TileEntity tile = event.entity.worldObj.getTileEntity(x, y, z);
        IInventory inv = (IInventory)tile;
        inv.setInventorySlotContents(1, event.entityItem.getEntityItem();

    }

}

 

You don't need to point out what's wrong with the programmer. I already know. Now what's wrong with the code?

Link to comment
Share on other sites

why are you still on 1.7.10 ??

 

edit this is the code you need but you need to be on 1.10.2

    @SubscribeEvent
    public void itemDespawnEvent(ItemExpireEvent event){
        BlockPos pos = new BlockPos(event.getEntity().posX ,event.getEntity().posY, event.getEntity().posZ);
        event.getEntity().worldObj.setBlockState(pos,Blocks.CHEST.getDefaultState());
        TileEntity tile = event.getEntity().worldObj.getTileEntity(pos);
        IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
        handler.insertItem(0, event.getEntityItem().getEntityItem(), false);

    }

Link to comment
Share on other sites

The "#" is the notation used in JavaDoc to reference another class' methods. "Class.method" is the syntax for static methods, which can be mis-leading.

This means, that when you see "class#method", you need an instance of the class. Here, the direct code would be "

event.getEntityItem()

", where event is an instance of

ItemExpireEvent

.

 

I use the IItemHandler capability here, to pickup items, and insert them into adjacent inventories that have said IItemHandler capability.

 

If you do create your own custom container, you will need to attach the IItemHandler Capability to it. There are quite the few tutorials on that out there.

 

The way you set the inventory now, will destroy items when any of these 2 conditions are met: There is already an item in the inventory at slot #1, OR more than 1 stack expires in the same block-space, which does fire the first condition. You simply set the first stack to something new, without checking if it is already something else.

 

IItemHandler is superior to IInventory, because it does a lot of the logic for you. With IInventory, you need to scan the whole inventory for a null slot, or a slot containing the same item/block. Calculate how much you can insert into that slot, split the stacks, and actually insert the amount that you can.

In the class I linked above, I first tested with IInventory.... Code was bulky as hell, more than twice as long, and not easily understandable from a glance. IItemHandler compacted the whole thing. I am seriously happy I took the time to learn about IItemHandler!

 

Also: What do you think will happen when, let's say a BuildCraft quarry, or pipe, or heck, even a player with a magnet, gathers a lot of items in the world, and they despawn? Will each stack have it's own inventory spawned? That's some tower 'o chests right there...

 

@loordgek

Still the issue with overwriting whatever is already in the slot you specify.

 

Always check IF you can, THEN do it.

And yes. Seriously update to 1.7.10. Not supported here, and only reason that version isn't dead yet is because server's haven't been able to get a stable plugin+mod platform until recently ('stable' or rather, somewhat working)

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

As said before, 1.7.10 is no longer supported by Forge. Update to 1.10.2.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.