Jump to content

Recommended Posts

Posted (edited)

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
Posted (edited)

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:

  Reveal hidden contents

 

Posted
  On 8/31/2019 at 12:56 PM, 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.

Expand  

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
Posted
  On 8/31/2019 at 4:40 PM, 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:

Expand  

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.

Posted (edited)
  On 8/31/2019 at 5:00 PM, Animefan8888 said:

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

Expand  

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
Posted
  On 8/31/2019 at 6:04 PM, 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.

Expand  

 

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?

Posted
  On 8/31/2019 at 9:14 PM, Lea9ue said:

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

Expand  

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.

Posted
  On 8/31/2019 at 9:49 PM, 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?

Expand  

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.  

Posted
  On 8/31/2019 at 11:08 PM, 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.  

Expand  

You could try using

  On 8/31/2019 at 4:40 PM, 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:

 

Expand  

as this should be the easiest way I see.

Some tips:

  Reveal hidden contents

 

Posted (edited)

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.
Posted
  On 9/2/2019 at 4:40 AM, Lea9ue said:

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

Expand  

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:

  Reveal hidden contents

 

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.