Jump to content

Recommended Posts

Posted (edited)

Hello, I have a mod with some custom effects. I am trying to make if player has X custom effect player can't crouch. 

Here's my code for this :

@SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {

        if(event.phase != TickEvent.Phase.START || !event.player.isAlive()) {
            return;
        }

        final boolean lazyLeg = event.player.hasEffect(ModEffects.LAZY_LEGS.get());
        final Pose forcedPose = event.player.getForcedPose();

        if (lazyLeg && forcedPose != Pose.STANDING) {
            event.player.setForcedPose(Pose.STANDING);
            event.player.setPose(Pose.STANDING);
        } else if (!lazyLeg && Pose.STANDING == forcedPose) {
            event.player.setForcedPose(null);
        }

    }

If I switch Pose.STANDING's to Pose.FALL_FLYING the code is working but it is not working on STANDING pose. I couldn't understand why it is happening.

Edited by FantaLaTone
solved
Posted

Hello, I am trying to cancel the crouch if player has an custom effect. I tried some methods but none of them have worked for me.

Here's my current code:

@SubscribeEvent
    public static void onKeyInput(InputEvent.Key event) {
        if (Minecraft.getInstance().screen != null) return;
        if (event.getKey() == 340) {

            Player pPlayer = Minecraft.getInstance().player;
            if (pPlayer.hasEffect(ModEffects.LAZY_LEGS.get())) {
                if (event.isCancelable()) {
                    event.setCanceled(true);
                }
//                event.setCanceled(true);
//                pPlayer.setPose(Pose.STANDING);
            }
        }
    }

    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {

        if(event.phase != TickEvent.Phase.START || !event.player.isAlive()) {
            return;
        }

        final boolean lazyLeg = event.player.hasEffect(ModEffects.LAZY_LEGS.get());
        final Pose forcedPose = event.player.getForcedPose();

        if (lazyLeg && forcedPose != Pose.FALL_FLYING) {
            event.player.setForcedPose(Pose.FALL_FLYING);
            event.player.setPose(Pose.FALL_FLYING);
        } else if (!lazyLeg && Pose.FALL_FLYING == forcedPose) {
            event.player.setForcedPose(null);
        }

    }

Nothing works here!

Posted

The crouching is not controlled by the player's Pose.

It is handled by the logic in LocalPlayer.aiStep() which is hardwired to check the isShiftKeyDown()

I guess you could hack something in the MovementInputUpdateEvent? But you can't just set shiftKeyDown = false,

because the isCrouching check is done before that event is fired.

 

I can't see a clean way do it other than using an access transformer to be able to change the LocalPlayer's isCrouching field?

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted
1 hour ago, FantaLaTone said:

Here's my current code:

I hope that the events are located in two different classes, since InputEvent.Key is a client side only Event and TickEvent.PlayerTickEvent is a client and server Event (common)

Posted (edited)

I decided to use AccessTransforms to do it but how I can change public methods code? How do I can override it?

 

Edit: I made AccessTransforms work. I can make fields public and change values but I couldn't understand how I can change a methods code?

Edited by FantaLaTone
Posted
1 hour ago, diesieben07 said:

LocalPlayer#crouching

It's is a field, you don't need to override any method (this is btw without using Mixin not possible),

you need to set LocalPlayer#crouching in MovementInputUpdateEvent to the value you want.

Posted
10 minutes ago, Luis_ST said:

It's is a field, you don't need to override any method (this is btw without using Mixin not possible),

you need to set LocalPlayer#crouching in MovementInputUpdateEvent to the value you want.

I changed it to a public field and this my code for now and it is not working

@SubscribeEvent
    public static void onPlayerTick(MovementInputUpdateEvent event) {
        if (event.getEntity() instanceof Player) {
            Player p = event.getEntity();
            Minecraft.getInstance().player.crouching = true;
        }
    }

 

Posted (edited)
18 minutes ago, diesieben07 said:

You need to set it to false obviously. Also you will need to set forwardImpulse and leftImpulse on the KeyboardInput as KeyboardInput#tick is called before MovementInputUpdateEvent.

it is just making player can't crouch visually I can still crouch

Eyes are going down and player gets slower

Edited by FantaLaTone
Posted
5 minutes ago, diesieben07 said:

Show updated code.

package com.fantalatone.sculknightmare.event;

import com.fantalatone.sculknightmare.SculkNightmare;
import com.fantalatone.sculknightmare.effect.ModEffects;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.monster.Creeper;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.event.MovementInputUpdateEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;


@Mod.EventBusSubscriber(modid = SculkNightmare.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ModEvents {

    @SubscribeEvent
    public static void onKeyInput(InputEvent.Key event) {
        if (Minecraft.getInstance().screen != null) return;
        if (event.getKey() == 340) {

            Player pPlayer = Minecraft.getInstance().player;
            if (pPlayer.hasEffect(ModEffects.LAZY_LEGS.get())) {
            }
        }
    }

    @SubscribeEvent
    public static void onPlayerTick(MovementInputUpdateEvent event) {
        Minecraft.getInstance().player.crouching = false;
    }

    @SubscribeEvent
    public static void onEntityJoin(EntityJoinLevelEvent event) {
        if (event.getEntity() instanceof Creeper) {
            ((Creeper) event.getEntity()).explosionRadius = 20;
        }
    }

}

 

Posted
3 hours ago, diesieben07 said:
  • Use the player given to you in the event.
  • You have not done this.

  • You also need to set shiftKeyDown to false on the Input instance, as that is what will be sent to the server.

Ok I finally did it working. Everything works fine. Here's the last version of my codes:

accesstransformers.cfg

public net.minecraft.client.player.LocalPlayer f_108601_ # crouching

ModEvents.java :

package com.fantalatone.sculknightmare.event;

import com.fantalatone.sculknightmare.SculkNightmare;
import com.fantalatone.sculknightmare.effect.ModEffects;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.MovementInputUpdateEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = SculkNightmare.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ModEvents {

    @SubscribeEvent
    public static void onPlayerTick(MovementInputUpdateEvent event) {
        LocalPlayer pPlayer = Minecraft.getInstance().player;
        if (pPlayer.hasEffect(ModEffects.LAZY_LEGS.get())) {
            event.getInput().shiftKeyDown = false;
            pPlayer.crouching = false;
        }
    }
}

 

Thanks for everybody helped me!

  • FantaLaTone changed the title to [SOLVED] [1.19] Cancel the crouching [CODES]

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.