Jump to content

[1.10.2] Re:Item despawn chest [solved]


Stroam

Recommended Posts

previous thread

 

I did see that I indeed see I was in my 1.7 environment and have since switched to my 1.10.2. I tested with the code.

 

 @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);

    }

 

So looking at this code I made some modifications to my own which now look like.

 

import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.item.ItemExpireEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import java.util.List;


/**
* I know the comments on every line is not needed and excessive but currently figuring this out
* and it communicates what I do and may not understand.
*/
public class ItemDespawn {

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

    @SubscribeEvent
    public void itemDespawnEvent(ItemExpireEvent event){

        //gets position of expired item
        BlockPos pos = new BlockPos(event.getEntity().posX ,event.getEntity().posY, event.getEntity().posZ);

        //puts a chest in position. I don't know if this will push items.
        event.getEntity().worldObj.setBlockState(pos,Blocks.CHEST.getDefaultState());

        //gets the chest and assigns it to tile
        TileEntity tile = event.getEntity().worldObj.getTileEntity(pos);

        //Gets the chests inventory
        IItemHandler inventory = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);

        insert(inventory);

        event.setExtraLife(10);// Don't know if this is needed.
    }

    /**
     * https://github.com/Matryoshika/Saligia/blob/master/src/main/java/se/Matryoshika/Saligia/Content/Tiles/Utility/TileItemPickUpper.java
     */
    private void insert(IItemHandler inv){
        //gets all items within 1 block radius. Can not resolve worldObj or getPos.
        List<EntityItem> items = worldObj.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(getPos().add(-1, -1, -1), getPos().add(2, 2, 2)));

        for(EntityItem entity : items){ //for each
            ItemStack stack = entity.getEntityItem(); //grab an itemStack

            for(int i = 0; i < inv.getSlots(); i++){
                if(inv.getStackInSlot(i) == null //slot is empty
                        || (inv.getStackInSlot(i) != null  // or slot contains a non-full stack of that item
                        && inv.getStackInSlot(i).getItem() == stack.getItem()
                        && inv.getStackInSlot(i).stackSize < 64)){
                    ItemStack over = inv.insertItem(i, stack, false); //insert items into itemstack and store surplus
                    if(over != null) //if surplus
                        stack.stackSize -= over.stackSize; //decrease stacksize by surplus?
                        //shouldn't it be stack.stackSize -= stack.stackSize - over.stackSize?
                    else{
                        entity.setDead();
                    }
                    if(stack.stackSize <= 0) //if there is no items in item stack, delete itemStack
                        entity.setDead();
                    break;
                }
            }
        }
    }
}

 

 

Doesn't compile because it can not resolve worldObj or getPos. If someone could point out a solution that'd be helpful or if Matryoshika could explain how his code works. Also looking over my excessive commenting and telling me where I am wrong would also be helpful.

 

 

 

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

Also I am not sure why you are even searching the world for item entities... I thought you wanted the despawning item to go into a chest, not all items nearby.

I think his "goal" is that when a player dies, all the dropped items get collected up and put into a storage container after the 5 minute despawn timer.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I didn't say it did, I was just clarifying what his goal was.  And one way I could see of doing that would be:

 

1) an item dies

2) it creates a chest

3) it searches the nearby area for other items and also puts them in the chest

 

The alternative would be for each item to search the world for a nearby already existent chest (or you'd get multiple chests).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I must say I am impressed with the speed of replies. Doesn't give me much time to fiddle around between replies. So here is my latest attempt.

 

import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.item.ItemExpireEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import java.util.List;


/**
* I know the comments on every line is not needed and excessive but currently figuring this out
* and it communicates what I do and may not understand.
*/
public class ItemDespawn {

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

    @SubscribeEvent
    public void itemDespawnEvent(ItemExpireEvent event){

        //gets position of expired item
        BlockPos pos = new BlockPos(event.getEntity().posX ,event.getEntity().posY, event.getEntity().posZ);

        //puts a chest in position. I don't know if this will push items.
        event.getEntity().worldObj.setBlockState(pos,Blocks.CHEST.getDefaultState());

        //gets the chest and assigns it to tile
        TileEntity tile = event.getEntity().worldObj.getTileEntity(pos);

        //Gets the chests inventory
        IItemHandler inventory = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);

        //gets all items within 1 block radius. 
        List<EntityItem> items = event.getEntity().worldObj.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(pos.add(-1, -1, -1), pos.add(2, 2, 2)));
        insert(inventory, items);

        event.setExtraLife(10);// Don't know if this is needed.
    }

    /**
     * https://github.com/Matryoshika/Saligia/blob/master/src/main/java/se/Matryoshika/Saligia/Content/Tiles/Utility/TileItemPickUpper.java
     */

    private void insert(IItemHandler inv, List<EntityItem> items){

         for(EntityItem entity : items){ //for each
            ItemStack stack = entity.getEntityItem(); //grab an itemStack

            for(int i = 0; i < inv.getSlots(); i++){
                if(inv.getStackInSlot(i) == null //slot is empty
                        || (inv.getStackInSlot(i) != null  // or slot contains a non-full stack of that item
                        && inv.getStackInSlot(i).getItem() == stack.getItem()
                        && inv.getStackInSlot(i).stackSize < 64)){
                    ItemStack over = inv.insertItem(i, stack, false); //insert items into itemstack and store surplus
                    if(over != null) //if surplus
                        stack.stackSize -= over.stackSize; //decrease stacksize by surplus?
                        //shouldn't it be stack.stackSize -= stack.stackSize - over.stackSize?
                    else{
                        entity.setDead();
                    }
                    if(stack.stackSize <= 0) //if there is no items in item stack, delete itemStack
                        entity.setDead();
                    break;
                }
            }
        }
    }
}

 

Tests have failed. The item despawns and no chest is place down. The idea is.

1) an item dies

2) it creates a custom container called item pile

3) it searches for nearby items and adds them to the item pile

 

If someone could answer the following as well as point out where I went wrong or what I could do better, that would be most helpful. Thanks

event.setExtraLife(10);// Don't know if this is needed.

 

  stack.stackSize -= over.stackSize; //decrease stacksize by surplus?

//shouldn't it be stack.stackSize -= stack.stackSize - over.stackSize?

 

diesieben07 I totally acknowledge you know more about minecraft modding than I do. I am making a garbage mod that stops items from despawning and instead makes garbage piles. I want to use it to teach children the importance of conservation and recycling and what happens when your trash doesn't magically disappear so any help you can give me would be most appreciated.

 

-update-

I commented everything out and just did a print statement and it didn't print anything when the item despawned. I must have the setup wrong.

 

public class ItemDespawn {

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

    @SubscribeEvent
    public void itemDespawnEvent(ItemExpireEvent event){
        System.out.println("did get called");

    }
}

 

 

Didn't call it in preInit. Trying that now. Cool now it's being called. Retrying.

 

Alright it works. Now all I need is code improvement suggestions and the questions above answered about itemstack and setextralife. I should also add a black list for organics. I see a bunch of chests with eggs.

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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