Jump to content

[1.12.2] Wanting to know if dropped item is gone.


Lea9ue

Recommended Posts

Trying to do something for when dropped item is destroyed by something, possibly lava .  Having trouble getting it.

 

code 

@SubscribeEvent

	public static void onEvent(EntityJoinWorldEvent event) {
		
		Entity entity = event.getEntity();
		World world =event.getWorld();	
	
		if(event.getEntity() instanceof EntityItem) {
			ItemStack stack = (((EntityItem) entity).getItem());
			if(stack.getItem() == Items.DIAMOND) {
				System.out.println("diamond on ground");		
				if(event.getEntity().isDead) {
					System.out.println("diamonds gone");
				}
			}
		}	
	}

this prints "diamond on ground" but the next if statement seems to be useless.  Is there a solution I'm not thinking of. I don't know if its even possible to get position from an EntityItem to see if block is lava.  I also tried to us ItemTossEvent but I don't really think I can do much with item once its tossed. Any help would be appreciated

 

 

Edited by Lea9ue
misspelling
Link to comment
Share on other sites

EntityJoinWorldEvent is fired when an Entity is added to the world, during which Entity#isDead cannot be true.

 

AFAIK there is no way to do this with an event.

Neither Entity#onUpdate nor ItemEntity#onUpdate fires an event every tick.

 

The only way I see is to replace the vanilla ItemEntity with your own version and override Entity#attackEntityFrom and check whether the DamageSource is from lava.

That would not work, as dropped items are created by directly instantiating an instance of the vanilla ItemEntity.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

3 hours ago, DavidM said:

The only way I see is to replace the vanilla ItemEntity with your own version and override Entity#attackEntityFrom and check whether the DamageSource is from lava.

That would not work, as dropped items are created by directly instantiating an instance of the vanilla ItemEntity.

After creating your custom EntityItem class, you would subscribe to EntityJoinWorldEvent and replace the entity with your custom one. I think this post ended up doing that:

 

 

  • Like 1
Link to comment
Share on other sites

18 minutes ago, Daeruin said:

After creating your custom EntityItem class, you would subscribe to EntityJoinWorldEvent and replace the entity with your custom one. I think this post ended up doing that:

Isnt there just a method in Item called something like createItemEntity that is used for this in particular? @Lea9ue

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

1 hour ago, Animefan8888 said:

Isnt there just a method in Item called something like createItemEntity that is used for this in particular? @Lea9ue

You are right. I had forgotten about that. There are a handful of methods you can override to get custom control over the EntityItem created by a specific Item: hasCustomEntity, createEntity, onEntityItemUpdate, and getEntityLifespan. You would override hasCustomEntity to return true, then override createEntity to supply your custom EntityItem.

Edited by Daeruin
Link to comment
Share on other sites

2 hours ago, Daeruin said:

You are right. I had forgotten about that. There are a handful of methods you can override to get custom control over the EntityItem created by a specific Item: hasCustomEntity, createEntity, onEntityItemUpdate, and getEntityLifespan. You would override hasCustomEntity to return true, then override createEntity to supply your custom EntityItem.

 

So it sounds like the only way to do this is to replace the EntityItem with a custom EntityItem. Don't really want to do that. Is there any way possible to instead get the  block location of EntityItem to see if block is lava?

Link to comment
Share on other sites

30 minutes ago, Lea9ue said:

Is there any way possible to instead get the  block location of EntityItem to see if block is lava?

Kinda. The event in particular isn't meant for this use. What is it that you are trying to accomplish? Aka what are you gonna do once the item is in lava? Is it your own item?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

56 minutes ago, Animefan8888 said:

Kinda. The event in particular isn't meant for this use. What is it that you are trying to accomplish? Aka what are you gonna do once the item is in lava? Is it your own item?

Once Vahnilla item is in lava.  I would like to spawn custom item or maybe custom mob (little undecided) .  If I could get block pos and check it, I could probably spawn item.  This has issues (eg. mining diamond ore would have a chance to give item if diamond fell in lava)  I would rather use ItemTossEvent.  I am hoping that I can spawn custom item in on toss event and copy motion of first item. Basically keep both items and with my custom item I should be able to set a pretty short life span and make it so you cant pick it up and if it takes damage, check if damage soure is lava. Then spawn item at players position. Does that sound right? At work for the next 12 hours so cant try anything. My guess is that theres going to be some problems like getting motion from vahnilla item wont be possible with this method.  

Link to comment
Share on other sites

57 minutes ago, Lea9ue said:

Once Vahnilla item is in lava.  I would like to spawn custom item or maybe custom mob (little undecided) .  If I could get block pos and check it, I could probably spawn item.  This has issues (eg. mining diamond ore would have a chance to give item if diamond fell in lava)  I would rather use ItemTossEvent.  I am hoping that I can spawn custom item in on toss event and copy motion of first item. Basically keep both items and with my custom item I should be able to set a pretty short life span and make it so you cant pick it up and if it takes damage, check if damage soure is lava. Then spawn item at players position. Does that sound right? At work for the next 12 hours so cant try anything. My guess is that theres going to be some problems like getting motion from vahnilla item wont be possible with this method.  

You could try using

7 hours ago, Daeruin said:

After creating your custom EntityItem class, you would subscribe to EntityJoinWorldEvent and replace the entity with your custom one. I think this post ended up doing that:

 

as this should be the easiest way I see.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

15 minutes ago, DavidM said:

as this should be the easiest way I see.

I wouldn't say it's the easiest way, but it is the best way.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

First off Thanks to @Animefan8888, @DavidM, @Daeruin.  This is the code, I feel its good enough. Just wanted to share incase anyone comes across it that needs it.

 

Entity class:

package com.lea9ue.mobwars.common.entity;


import com.lea9ue.mobwars.event.item.EntityItemDeathEvent;
import com.lea9ue.mobwars.event.item.EntityItemHurtEvent;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;

public class EntityItemFakeDiamond extends EntityItem {

	private DamageSource previousDamageSource;

	public EntityItemFakeDiamond(World world, double x, double y, double z, ItemStack itemStack) {
		super(world, x, y, z, itemStack);
	}

	@Override
	public boolean attackEntityFrom(DamageSource damageSource, float damage) {

		boolean returnValue = super.attackEntityFrom(damageSource, damage);
		this.previousDamageSource = damageSource;

		if (this.velocityChanged)
			MinecraftForge.EVENT_BUS.post(new EntityItemHurtEvent(this, damageSource, damage));

		if (this.isDead)
			MinecraftForge.EVENT_BUS.post(new EntityItemDeathEvent(this, damageSource));

		return returnValue;

	}

	public DamageSource getPreviousDamageSource(){
		return this.previousDamageSource;
	}

	public static EntityItemFakeDiamond copy(EntityItem oldEntityItem){
		EntityItemFakeDiamond newEntityItem =  new EntityItemFakeDiamond(oldEntityItem.world, oldEntityItem.posX, oldEntityItem.posY, oldEntityItem.posZ, oldEntityItem.getItem());
		newEntityItem.motionX = oldEntityItem.motionX;
		newEntityItem.motionY = oldEntityItem.motionY;
		newEntityItem.motionZ = oldEntityItem.motionZ;
		newEntityItem.hoverStart = oldEntityItem.hoverStart;
		newEntityItem.lifespan = oldEntityItem.lifespan;

		return newEntityItem;
	}
}

 

 

Handler class:

package com.lea9ue.mobwars.server;

import com.lea9ue.mobwars.common.entity.EntityItemFakeDiamond;
import com.lea9ue.mobwars.event.item.EntityItemDeathEvent;
import com.lea9ue.mobwars.init.ModItems;

import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class DiamondHandler{
	
	    @SubscribeEvent

	    public static void onEntityJoinWorld(EntityJoinWorldEvent event){

	        if (event.getWorld().isRemote) return;
	        if (!(event.getEntity() instanceof EntityItem)) return;
	        if (event.getEntity() instanceof EntityItemFakeDiamond) return;
	        EntityItem entityItem = (EntityItem) event.getEntity();
	        

	        if (isDiamond(entityItem)){
	        	
	        	System.out.println("Diamond has been dropped");
	
	        	EntityItemFakeDiamond newEntityItem = EntityItemFakeDiamond.copy(entityItem);
	        	event.getWorld().spawnEntity(newEntityItem);
	        	event.setCanceled(true);
	            newEntityItem.setDefaultPickupDelay();
	        }
	    }
	        
	    @SubscribeEvent
	    public static void onItemDeath(EntityItemDeathEvent event){
	        
	    	if (isDiamond(event.getEntityItem()) && event.damageSource.isFireDamage()){
	            
	    		System.out.println("Diamond on Died in the fire/lava");
	    		//DO SOMETHING//
	    	
	    	}
	    }

	    private static boolean isDiamond(EntityItem entityItem){
	    	return entityItem.getItem().getUnlocalizedName().equals(Items.DIAMOND.getUnlocalizedName());
	    }
}

 

 

Event class:

package com.lea9ue.mobwars.event.item;



import net.minecraft.entity.item.EntityItem;
import net.minecraft.util.DamageSource;
import net.minecraftforge.event.entity.item.ItemEvent;



public class EntityItemDeathEvent  extends ItemEvent{

	public final DamageSource damageSource;
	public EntityItemDeathEvent(EntityItem itemEntity, DamageSource damageSource) {
		super(itemEntity);
		this.damageSource = damageSource;
	}
}

 

 

Event class:

package com.lea9ue.mobwars.event.item;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.util.DamageSource;
import net.minecraftforge.event.entity.item.ItemEvent;

public class EntityItemHurtEvent extends ItemEvent {

	public final DamageSource damageSource;
	public final float amount;
	public EntityItemHurtEvent(EntityItem entity, DamageSource damageSource, float amount) {
		super(entity);
		this.damageSource = damageSource;
		this.amount = amount;
	}
}

 

Edited by Lea9ue
Removed code from DavidM comment below.
Link to comment
Share on other sites

1 minute ago, Lea9ue said:

if (entityItem.getItem() == null) return;

ItemStack will never be null.

ItemStack from an EntityItem will never be empty, as the ones that are are removed from the world.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

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.

×
×
  • Create New...

Important Information

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