Jump to content

Getting an item to change into another item when dropped in water


Knifesurge

Recommended Posts

if (event.entity instanceof EntityItem && !(event.entity instanceof SpecialEntityItem))

 

Trying it out, doesn't crash :3 also, whenever the event (EntityJoinWorldEvent) is triggered, I'm getting the same problem as perromercenary00 did, where it says "Item entity x has no item?!", where x is a number. As well, whenever I drop the blaze rod, I instantly pick it up. Not really sure what to do about that but everything else seems to work fine

Link to comment
Share on other sites

That "no item" error message happens when the entityItem has no itemStack associated with it. It is probably solved by d7's suggestion, but in general, I think a call to setEntityItemStack is in order for all entityItems (something that happens in the NBT methods). It's this setting of the itemStack that one entityItem might be able to handle the entire life cycle of your entity from blaze rod to cooling to pick up without being replaced again.

 

PS: Operate on the old and new entities inside your event handler where you have both at hand.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Changed my EntityJoinWorldEvent method in my EntityJoinWorldEventHandler to this, like d7 suggested to do (adding NBTTagCompound):

if(((EntityItem)event.entity).getEntityItem().getItem() == Items.blaze_rod)
		{
			event.setCanceled(true);
			SpecialEntityItem entityItem = new SpecialEntityItem(event.entity.worldObj, event.entity.posX, event.entity.posY, event.entity.posZ, ((EntityItem)event.entity).getEntityItem());
			NBTTagCompound nbt = new NBTTagCompound();
			event.entity.writeToNBTOptional(nbt);
			entityItem.readFromNBT(nbt);
			event.entity.worldObj.spawnEntityInWorld(entityItem);
		}

 

The Blaze Rod now drops like a regular blaze rod, however, I cannot pick it back up, and just "bounces" in the same spot(looks like it is dropping after a 2 second delay where I dropped it). It also, in contact with water, does not do anything. Any suggestions as to why this is?

Link to comment
Share on other sites

:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D

 

Omg it finally works! I set some debug System.out.println() to see if it it reaches where I want it to, but everything worked out fine. Then I thought, EntityItem has a way more extensive onUpdate() method/event than my SpecialEntityItem did. So I copy pasted (probably not the best thing to do but it gets the result I want) the rest of it. I noticed this little piece of code that is in the vanilla EntityItem onUpdate():

if (!this.worldObj.isRemote)
                {
                    this.searchForOtherItemsNearby();
                }

 

However it is a private method that if you implement into the class, you need more methods which lead to a dead end (at least for me). So, I commented it out and Voila! it works. It drops the Blaze Rod, it acts as a vanilla Blaze Rod, and when it hits water, it "changes" into my cooledBlazeRod. It does everything I want it to do at the moment.

 

onUpdate() in SpecialEntityItem

 

 

Code:

@Override
public void onUpdate()
{
	if(this.inWater)
	{
		this.setDead();
		//DEBUG
		System.out.println("this set to Dead");
		EntityItem entity = new EntityItem(worldObj, this.posX, this.posY, this.posZ);
		//DEBUG
		System.out.println("EntityItem entity created at this position");
		entity.setEntityItemStack(new ItemStack(ToolExpansionItems.cooledFireRod));
		//DEBUG
		System.out.println("entity's EntityItemStack set to cooledFireRod");
		worldObj.spawnEntityInWorld(entity);
		//DEBUG
		System.out.println("entity spawned in world");	
	}

	ItemStack stack = this.getDataWatcher().getWatchableObjectItemStack(10);
	if (stack != null && stack.getItem() != null && stack.getItem().onEntityItemUpdate(this)) return;
        if (this.getEntityItem() == null)
        {
            this.setDead();
        }
        else
        {
            super.onUpdate();

            if (this.delayBeforeCanPickup > 0 && this.delayBeforeCanPickup != 32767)
            {
                --this.delayBeforeCanPickup;
            }

            this.prevPosX = this.posX;
            this.prevPosY = this.posY;
            this.prevPosZ = this.posZ;
            this.motionY -= 0.03999999910593033D;
            this.noClip = this.pushOutOfBlocks(this.posX, (this.getEntityBoundingBox().minY + this.getEntityBoundingBox().maxY) / 2.0D, this.posZ);
            this.moveEntity(this.motionX, this.motionY, this.motionZ);
            boolean flag = (int)this.prevPosX != (int)this.posX || (int)this.prevPosY != (int)this.posY || (int)this.prevPosZ != (int)this.posZ;

            if (flag || this.ticksExisted % 25 == 0)
            {
                if (this.worldObj.getBlockState(new BlockPos(this)).getBlock().getMaterial() == Material.lava)
                {
                    this.motionY = 0.20000000298023224D;
                    this.motionX = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
                    this.motionZ = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
                    this.playSound("random.fizz", 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
                }
                /*
                if (!this.worldObj.isRemote)
                {
                    this.searchForOtherItemsNearby();
                }*/
            }

            float f = 0.98F;

            if (this.onGround)
            {
                f = this.worldObj.getBlockState(new BlockPos(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.getEntityBoundingBox().minY) - 1, MathHelper.floor_double(this.posZ))).getBlock().slipperiness * 0.98F;
            }

            this.motionX *= (double)f;
            this.motionY *= 0.9800000190734863D;
            this.motionZ *= (double)f;

            if (this.onGround)
            {
                this.motionY *= -0.5D;
            }

            if (this.age != -32768)
            {
                ++this.age;
            }

            this.handleWaterMovement();

            ItemStack item = getDataWatcher().getWatchableObjectItemStack(10);

            if (!this.worldObj.isRemote && this.age >= lifespan)
            {
                int hook = net.minecraftforge.event.ForgeEventFactory.onItemExpire(this, item);
                if (hook < 0) this.setDead();
                else          this.lifespan += hook;
            }
            if (item != null && item.stackSize <= 0)
            {
                this.setDead();
            }
        }
}

 

 

 

onEvent(EntityJoinWorldEvent event) in my EntityJoinWorldEventHandler class

 

 

Code:

@SubscribeEvent(priority=EventPriority.HIGHEST)
public void onEntityJoinWorld(EntityJoinWorldEvent event)
{
	if(event.entity instanceof EntityItem && !(event.entity instanceof SpecialEntityItem))
	{
		if(((EntityItem)event.entity).getEntityItem().getItem() == Items.blaze_rod)
		{
			event.setCanceled(true);
			//DEBUG
			System.out.println("EntityJoinWorldEvent has been canceled");
			SpecialEntityItem entityItem = new SpecialEntityItem(event.entity.worldObj, event.entity.posX, event.entity.posY, event.entity.posZ);
			//DEBUG
			System.out.println("SpecialEntityItem created at position X: "+event.entity.posX+", Y: "+event.entity.posY+", Z: "+event.entity.posZ);
			entityItem.setEntityItemStack(((EntityItem)event.entity).getEntityItem());
			System.out.println("event.entity has had its EntityItemStack set");
			//DEBUG
			System.out.println("Registering NBTTagCompound on " + entityItem.toString());
			NBTTagCompound nbt = new NBTTagCompound();
			//DEBUG
			System.out.println("nbt var created");
			event.entity.writeToNBTOptional(nbt);
			//DEBUG
			System.out.println("event.entity written to nbt");
			entityItem.readFromNBT(nbt);
			//DEBUG
			System.out.println("entityItem read from nbt");
			event.entity.worldObj.spawnEntityInWorld(entityItem);
			//DEBUG
			System.out.println("entityItem spawned");
		}
	}
}

 

 

 

Thank you everyone for their help. It has been a very interesting lesson ;D ;D

Link to comment
Share on other sites

I'm not a master of Java but I know my way around the language. I actually didn't think of that tbh... I feel dumb. Cuts down on code, thanks!

 

(I like the pic xD )

 

Also, I've only been learning Java for about 4 months so cut me some slack ;P When I say I know Java, I mean that I can work through the code and stuff. I don't know everything yet but I'm still learning as I go

Link to comment
Share on other sites

Don' feel bad. When I came to MC modding in spring 2014, I had never seen a stitch of Java in my life (but I had done object-oriented  {O-O} design and programming in 3 other languages). Being unemployed at the time, I justified my modding effort as a learning experience for both Java and Eclipse (see my signature). It took me almost two weeks to write and debug my 1st basic mod (a new gem for the nether with tools and armor to suit). I blame conflicting tutorials and the jump from 1.6.4 to 1.7.2.

 

If nothing else, then get this:

 

1) Eclipse is your friend, especially when you use it to jump (visually) into the vanilla code referenced by your mod (you really want to have setup a  "decomp" workspace).

2) The debugger is also your friend, because it lets you see what is REALLY happening (no more guesswork!). I so deeply wish that every new modder would set break points and fire up the debugger before posting his/her 1st runtime "doesn't work" thread.

 

However, if one is new to more than Java... If one is not familiar with O-O design, then there is much more to learn. Java is merely a tool like many other tools (though reflection was new to me when I arrived). If you've already mastered O-O design in one or more other languages, then the basics of Java are a snap.

 

On the other hand, O-O design is a paradigm -- a kick-in-the-head, counter-intuitive thought process that is best acquired via mentoring (e.g. junior-college or better class with a good teacher/professor, or a real programming job with a good tech lead). If O-O design and coding (e.g. inheritance and run-time polymorphism) are new to you, then get help. Sign up for a class, or ask your boss for a mentor, and (to the reader finding this thread in the future) come back in a few months.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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

    • Add the crash-report or latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here  
    • Add the ful lcrash-report or latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here
    • I cant craft any of the epic fight mod weapons. I try using the recipes in the crafting table but nothing is working. and when i click on the epic fight weapon in jei there is no recipe at all.
    • Hello All! Started a MC Eternal 1.6.2.2 server on Shockbyte hosting. The only other mod I added was betterfarmland v0.0.8BETA. Server is 16GB and Shockbyte wont tell me how many CPU cores i have.  We are having problems now when players log in it seems to crash the server. At other times it seems fine and we can have 3 people playing for hours at a time. Usually always when it does crash it is when someone logs in. Crash Reports Below. To the person who can post the fix I will reward $100 via Paypal.   ---- Minecraft Crash Report ---- // This is a token for 1 free hug. Redeem at your nearest Mojangsta: [~~HUG~~] Time: 2024-09-19 21:04:58 UTC Description: Exception in server tick loop java.lang.StackOverflowError     at net.minecraft.advancements.PlayerAdvancements.hasCompletedChildrenOrSelf(PlayerAdvancements.java:451)     at net.minecraft.advancements.PlayerAdvancements.shouldBeVisible(PlayerAdvancements.java:419)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:385)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.P  
  • Topics

×
×
  • Create New...

Important Information

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