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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Different problem now. https://paste.ee/p/iDo8lS35
    • I would like to have a BoP sapling drop from my block if it is also installed. I think I have done everything and I cannot pinpoint the problem, which is the error in the logs that appears when joining a world:   [Worker-Main-11/ERROR] [ne.mi.co.ForgeHooks/]: Couldn't parse element loot_tables:grasses:blocks/leaves_block com.google.gson.JsonSyntaxException: Expected name to be an item, was unknown string 'biomesoplenty:magic_sapling' My code:   LootItemConditions.CONDITIONS.register(modEventBus); public class LootItemConditions { public static final DeferredRegister<LootItemConditionType> CONDITIONS = DeferredRegister.create(Registries.LOOT_CONDITION_TYPE, Grasses.MOD_ID); public static final RegistryObject<LootItemConditionType> IS_MOD_LOADED = CONDITIONS.register("is_mod_loaded", () -> new LootItemConditionType(new IsModLoaded.ConditionSerializer())); } public class IsModLoaded implements LootItemCondition { private final boolean exists; private final String modID; public IsModLoaded(String modID) { this.exists = ModList.get().isLoaded(modID); this.modID = modID; } @Override public LootItemConditionType getType() { return LootItemConditions.IS_MOD_LOADED.get(); } @Override public boolean test(LootContext context) { return this.exists; } public static LootItemCondition.Builder builder(String modid) { return () -> new IsModLoaded(modid); } public static class ConditionSerializer implements Serializer<IsModLoaded> { @Override public void serialize(JsonObject json, IsModLoaded instance, JsonSerializationContext ctx) { json.addProperty("modid", instance.modID); } @Override public IsModLoaded deserialize(JsonObject json, JsonDeserializationContext ctx) { return new IsModLoaded(GsonHelper.getAsString(json, "modid")); } } } protected LootTable.Builder createLeavesDropsWithModIDCheck(Block selfBlock, Item sapling, Property<?>[] properties, String modIDToCheck, float... chances) { CopyBlockState.Builder blockStateCopyBuilder = CopyBlockState.copyState(selfBlock); for(Property<?> property : properties) { blockStateCopyBuilder.copy(property); } return LootTable.lootTable() .withPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1.0F)) .add(LootItem.lootTableItem(selfBlock) .when(HAS_SHEARS_OR_SILK_TOUCH) .apply(blockStateCopyBuilder))) .withPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1.0F)) .add(this.applyExplosionCondition(selfBlock, LootItem.lootTableItem(sapling)) .when(IsModLoaded.builder(modIDToCheck))) .when(BonusLevelTableCondition.bonusLevelFlatChance(Enchantments.BLOCK_FORTUNE, chances)) .when(HAS_NO_SHEARS_OR_SILK_TOUCH)) .withPool(LootPool.lootPool().name("sticks").setRolls(ConstantValue.exactly(1.0F)) .add(this.applyExplosionDecay(selfBlock, LootItem.lootTableItem(Items.STICK). apply(SetItemCountFunction.setCount(UniformGenerator.between(1.0F, 2.0F)))) .when(BonusLevelTableCondition.bonusLevelFlatChance(Enchantments.BLOCK_FORTUNE, NORMAL_LEAVES_STICK_CHANCES)) .when(HAS_NO_SHEARS_OR_SILK_TOUCH))); } I don't know. Am I making a mistake somewhere? Am I forgetting something? Should there be something else?
  • Topics

×
×
  • Create New...

Important Information

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