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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://pastebin.com/VwpAW6PX My game crashes upon launch when trying to implement the Oculus mod to this mod compilation, above is the crash report, I do not know where to begin to attempt to fix this issue and require assistance.
    • https://youtube.com/shorts/gqLTSMymgUg?si=5QOeSvA4TTs-bL46
    • CubeHaven is a SMP server with unique features that can't be found on the majority of other servers! Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132 3 different stores: - CubeHaven Store: Our store to purchase using real money. - Bitcoin Store: Store for Bitcoin. Bitcoin can be earned from playing the server. Giving options for players if they want to spend real money or grind to obtain exclusive packages. - Black Market: A hidden store for trading that operates outside our traditional stores, like custom enchantments, exclusive items and more. Some of our features include: Rank Up: Progress through different ranks to unlock new privileges and perks. 📈 Skills: RPG-style skill system that enhances your gaming experience! 🎮 Leaderboards: Compete and shine! Top players are rewarded weekly! 🏆 Random Teleporter: Travel instantly across different worlds with a click! 🌐 Custom World Generation: Beautifully generated world. 🌍 Dungeons: Explore challenging and rewarding dungeons filled with treasures and monsters. 🏰 Kits: Unlock ranks and gain access to various kits. 🛠️ Fishing Tournament: Compete in a friendly fishing tournament! 🎣 Chat Games: Enjoy games right within the chat! 🎲 Minions: Get some help from your loyal minions. 👥 Piñata Party: Enjoy a festive party with Piñatas! 🎉 Quests: Over 1000 quests that you can complete! 📜 Bounty Hunter: Set a bounty on a player's head. 💰 Tags: Displayed on nametags, in the tab list, and in chat. 🏷️ Coinflip: Bet with other players on coin toss outcomes, victory, or defeat! 🟢 Invisible & Glowing Frames: Hide your frames for a cleaner look or apply a glow to it for a beautiful look. 🔲✨[ Player Warp: Set your own warp points for other players to teleport to. 🌟 Display Shop: Create your own shop and sell to other players! 🛒 Item Skins: Customize your items with unique skins. 🎨 Pets: Your cute loyal companion to follow you wherever you go! 🐾 Cosmetics: Enhance the look of your character with beautiful cosmetics! 💄 XP-Bottle: Store your exp safely in a bottle for later use! 🍶 Chest & Inventory Sorting: Keep your items neatly sorted in your inventory or chest! 📦 Glowing: Stand out from other players with a colorful glow! ✨ Player Particles: Over 100 unique particle effects to show off. 🎇 Portable Inventories: Over virtual inventories with ease. 🧳 And a lot more! Become part of our growing community today! Discord: https://cubehaven.net/discord Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132
    • # Problematic frame: # C [libopenal.so+0x9fb4d] It is always the same issue - this refers to the Linux OS - so your system may prevent Java from working   I am not familiar with Linux - check for similar/related issues  
  • Topics

×
×
  • Create New...

Important Information

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