Jump to content

Recommended Posts

Posted

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?

Posted

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.

Posted

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?

Posted

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

    }

Posted

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.

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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