Jump to content

Recommended Posts

Posted

I am trying to make a grappling hook (well, I already have but I'm improving the existing one) and the

.setDead();

method is acting very strangely. On one place in my code it works fine and kills the hook as it should (marked with "//this works fine."), so I don't think the fault is in the entity class, but in another place (marked with "//this doesn't work."), it sortof works because an

if()

statement would tell me it's dead, but it's still in the world and can, for some reason, still be killed with the /kill command. I've tried everything: putting the

setDead();

method in client side only, server side only and no specific side (which is where it's at in my code right now), I've tried replicating the /kill command by not using the

setDead();

method directly but instead using the

onKillCommand();

method in the entity class and even creating my own similar method in my entity class. And nothing works! It's like it gets marked as being dead but then all the other things that should happen when it's dead don't bother to happen! Can someone please explain what's going on? Here's my code:

package themattyboy.gadgetsngoodies.items;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import themattyboy.gadgetsngoodies.achievements.GadgetAchievements;
import themattyboy.gadgetsngoodies.entity.projectile.EntityGrapplingHook;

public class ItemGrapplingHook extends Item {

private EntityGrapplingHook hook;
private EntityPlayer player;
private boolean isShot = false;

@Override
public ItemStack onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn) {
	worldIn.playSoundAtEntity(playerIn, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 0.8F));
	hook = new EntityGrapplingHook(worldIn, playerIn, 1.5F);
	if(!this.isShot) {
		if(!worldIn.isRemote) {
			worldIn.spawnEntityInWorld(hook);
			this.isShot = true;
		}
		stack.damageItem(1, playerIn);
	}
	else {
		hook.setDead(); //this doesn't work.
		if(!worldIn.isRemote) {
			this.isShot = false;
		}
	}
	player = playerIn;
	return stack;
}

@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
	if(hook != null && hook.getInGround() && !hook.isDead) {
		if(hook.destdistX <= 32 && hook.destdistX >= -32 && hook.destdistY <= 32 && hook.destdistY >= -32 && hook.destdistZ <= 32 && hook.destdistZ >= -32) {
			entityIn.motionX = hook.destdistX / 5;
			entityIn.motionY = hook.destdistY / 5;
			entityIn.motionZ = hook.destdistZ / 5;
			if(hook.destdistY >= 25 && entityIn instanceof EntityPlayer) {
				player.triggerAchievement(GadgetAchievements.high_flier);
			}
		}
		else {
			hook.setDead(); //this works fine.
			this.isShot = false;
		}
	}
}
}

IGN: matte006

Played Minecraft since summer 2011.

Modding is my life now.

Please check out my mod :)

https://minecraft.curseforge.com/projects/gadgets-n-goodies-mod?gameCategorySlug=mc-mods&projectID=230028

Posted

hook = new EntityGrapplingHook(worldIn, playerIn, 1.5F);

 

You are replacing global field with new object. Then you are killing it, leaving old intact.

 

Not to mention this (whole class) is not gonna work, everything will break and stuff...

You CAN'T hold ANY (non-shared) fields in Item class iteslef, Item is a singleton! You need to save stuff in ItemStack (entity reference can be saved as UUID) or @Capability of ItemStack (as of 1.8.9+).

1.7.10 is no longer supported by forge, you are on your own.

Posted

hook = new EntityGrapplingHook(worldIn, playerIn, 1.5F);

 

You are replacing global field with new object. Then you are killing it, leaving old intact.

That was it. Thank you!

 

As for the singleton; I am aware of it, and now that I have this fixed I will be dealing with it, but my main focus was on fixing

setDead();

.

IGN: matte006

Played Minecraft since summer 2011.

Modding is my life now.

Please check out my mod :)

https://minecraft.curseforge.com/projects/gadgets-n-goodies-mod?gameCategorySlug=mc-mods&projectID=230028

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.