Jump to content

Recommended Posts

Posted

So, I want to make an enchant that makes a player have jump boost. I'm using this code, but the effect applies as soon as a player enters the game. How do I fix this?

public class HighStepEnchantment extends Enchantment {

    public HighStepEnchantment(Rarity rarityIn, EnchantmentType typeIn, EquipmentSlotType[] slots) {
        super(rarityIn, typeIn, slots);
    }

    @Override
    public int getMaxLevel() {
        return 1;
    }

    @Override
    public int getMinLevel() {
        return 1;
    }

    @Override
    protected boolean canApplyTogether(Enchantment ench) {
        return !ench.equals(Enchantments.FROST_WALKER);
    }






    @Mod.EventBusSubscriber(modid = MysteriousArtifacts.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
    public static class HighStepEquipped{
        @SubscribeEvent
        public static void action(TickEvent.PlayerTickEvent event) {

            PlayerEntity playerIn = event.player;

            playerIn.addPotionEffect(new EffectInstance(Effects.JUMP_BOOST, 20, 2));

        }

    }
}

 

Posted
6 minutes ago, DaeGrighen said:

So, I want to make an enchant that makes a player have jump boost.

How? Enchanting boots?

More details on how this will be used please. :)

7 minutes ago, DaeGrighen said:

I'm using this code, but the effect applies as soon as a player enters the game.

That's because you are applying the enchantment on PlayerTickEvent, so every tick the enchantment is applied to the player. Where/when you want to apply the enchantment will likely depend on your answer to the above question.

Posted
7 minutes ago, Ugdhar said:

How? Enchanting boots?

More details on how this will be used please. :)

That's because you are applying the enchantment on PlayerTickEvent, so every tick the enchantment is applied to the player. Where/when you want to apply the enchantment will likely depend on your answer to the above question.

Yes, sorry I'll specify. 

I want this enchantment to be applied on boots, and give the player jump boost when said boots are worn

Posted
4 minutes ago, DaeGrighen said:

Yes, sorry I'll specify. 

I want this enchantment to be applied on boots, and give the player jump boost when said boots are worn

Make a custom pair of boots, and override the onArmorTick method (I believe), to apply the enchantment there.

Posted

No need for a custom boots, just check in the tick even if said player has enchantment. Then if he has, apply the effect. EnchantmentHelper#getEnchantmentLevel is what you want here.

As for the ItemStack applying, just set the EnchantmentType to ARMOR_BOOTS or something like that.

  • Like 1
Posted
20 minutes ago, [NoOneButNo] said:

No need for a custom boots, just check in the tick even if said player has enchantment. Then if he has, apply the effect. EnchantmentHelper#getEnchantmentLevel is what you want here.

As for the ItemStack applying, just set the EnchantmentType to ARMOR_BOOTS or something like that.

Sorry if I ask, but can you make like a short line of code as an example? I am new to java and things

 

Posted

Learn java first. I really recommend you doing so as this is just elementary java basics. If I provide you the code, you are going to copy paste it anyways.

Well whatever:

 

@SubscribeEvent
public void onTick(PlayerTickEvent e){
	if(e.side == LogicalSide.CLIENT)
		return; //Checks if its client-side and if it is, return

	if(e.player == null)
		return; //Just in case someone stupid enough to pass in a null player

	PlayerEntity p = e.player;

	int level = EnchantmentHelper.getEnchantmentLevel(INSERT_YOUR_ENCHANTMENT, p.getItemStackFromSlot(EquipmentSlotType.FEET));

	//If level is greater than zero.
	if(level > 0){
		do whatever you want here.
	}
}

 

Posted
29 minutes ago, [NoOneButNo] said:

return; //Just in case someone stupid enough to pass in a null player

I'd say it is safe to not null check that, as firing a player tick event with a null player is definitely going to cause problems, and wouldn't be out of place to expect a NPE.

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.

 

 

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.