Jump to content

Apply effect when crouched? 1.19.4


Naamah

Recommended Posts

Hello,
So far I was able to add an armor item, but I cant find a way to check if the player is crouched while wearing the "chestplate" and if they are crouched, apply an effect while crouching. Would it cause lag if it were to check every tick? Is there a better way around this? Is it not possible? Here is my code:

public static final RegistryObject<ArmorItem> CLOAK_OF_SHADOWS = ITEMS.register("cloak_of_shadows", () ->
            new ArmorItem(ArmorMaterials.LEATHER, ArmorItem.Type.CHESTPLATE, new Item.Properties() {
                public void onArmorTick(Player player, ItemStack stack) {
                    if (player.getItemBySlot(EquipmentSlot.CHEST) == stack) {
                        if (player.isCrouching()) { // Check if player is crouching
                            player.addEffect(new MobEffectInstance(MobEffects.INVISIBILITY, 20, 0, true, false));
                            player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 20, 1, true, false));
                        }
                    }
                }
    }));

tenor.gif?itemid=27352888

 

Not even gonna lie, I think I have autism

Link to comment
Share on other sites

13 hours ago, ChampionAsh5357 said:

You should check by item (#getItem). Stacks are almost never equal.

I'd recommend using (stack.getEquipmentSlot() == EquipmentSlot.CHEST) instead. All you need to do is check if it's the correct slot, not if the stacks are equal.

Also it isn't necessary to do this every tick. Although the logic of the IF statement is pretty simple, Minecraft does a lot of stuff when adding potion effects (mainly syncing the player data). Every 5 ticks or so would be good enough, which you can do by checking if (player.tickCount % 5 == 0).

Edited by Mikul
  • Like 1
Link to comment
Share on other sites

On 6/9/2023 at 8:48 AM, ChampionAsh5357 said:

Not really. There's not enough logic for that to matter.

You should check by item (#getItem). Stacks are almost never equal.

I am now using the following code. Maybe I'm slow or something, but the effect still doesn't work, I even tried it with Mikul's solution.

public void onArmorTick(Player player, ItemStack stack) {
                    if (player.getItemBySlot(EquipmentSlot.CHEST).getItem() == stack.getItem() && player.isCrouching()) {
                            player.addEffect(new MobEffectInstance(MobEffects.INVISIBILITY, 20, 0, true, false));
                            player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 20, 1, true, false));
                    }
                }

 

tenor.gif?itemid=27352888

 

Not even gonna lie, I think I have autism

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



×
×
  • Create New...

Important Information

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