Jump to content

Getting player position? [1.19.2]


CheeseAndRice

Recommended Posts

I just got started with Forge modding the other day and I know this should be simple, but I'm having trouble getting the player's position. Older threads say to use PlayerEntity.getPosition(), which seems to be replaced by player.getPosition() since I can't find PlayerEntity (the Player class inherits LivingEntity so I'm pretty sure it's the same), but when I try to call the function there's a parameter that none of them seem to mention, float pPartialTicks, which I'm assuming is the tick at which you're getting the entity position. Was this a recent change or am I missing something? And how would I get the current tick from my applyEffectTick method? I'm on 1.19.2, using Parchment mappings. Any help would be greatly appreciated, thanks!

 

What I'm trying to do specifically is prevent the player from sleeping while they have this status effect. I couldn't figure out the event way of doing it (the line I commented out, if anyone knows how to do it that way that'd be even better), so now I'm trying teleporting the player up a block, or just to their current location maybe, to get them out of the bed. My plan c is having the player take damage to wake them but I feel like getting/setting the player's position is something I should know how to do.

package com.cheeseandrice.myfirstmod.effect;

import com.cheeseandrice.myfirstmod.MyFirstMod;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = MyFirstMod.MODID)
public class NoSleepEffect extends MobEffect
{
    static boolean inBed = false;
    static Player sleepingPlayer;
    PlayerSleepInBedEvent sleepEvent = null;
    public NoSleepEffect(MobEffectCategory mobEffectCategory, int color) {
        super(mobEffectCategory, color);
    }

    @Override
    public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
        if(inBed) {
            if(pLivingEntity == sleepingPlayer) {
                System.out.println("Player should be kicked out");
                //sleepEvent.setResult(Player.BedSleepingProblem.OTHER_PROBLEM);
                pLivingEntity.getPosition();
            }
            inBed = false;
        }
        super.applyEffectTick(pLivingEntity, pAmplifier);
    }

    @Override
    public boolean isDurationEffectTick(int pDuration, int pAmplifier) {
        return true;
    }

    @SubscribeEvent
    public static void unsleep(PlayerSleepInBedEvent event) {
        inBed = true;
        sleepingPlayer = event.getEntity();
        System.out.println("sleeping");

    }

}

 

Link to comment
Share on other sites

The short answer to your question is Entity.blockPosition()

 

But you are doing your MobEffect wrong.

You don't store static state in the MobEffect, there is only ever one of them.

You need to create a MobEffectInstance and add it to the entity. See for example WitherSkeleton.doHurtTarget()

Then you check if the entity has the effect, LivingEntity.hasEffect()

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.

Link to comment
Share on other sites

Okay, thanks! Yeah, using a static variable seemed like it could probably cause some problems but the event subscriber wasn't working unless the method was static. I'll try LivingEntity.hasEffect(). Also I have a couple other classes, ModEffects, which registers the effect, and an item that applies it to the player on use (that part works, just figuring out the effect itself now). (I'm following Kaupenjoe's 1.18.2 Forge tutorials.) Thanks for your help!

Edited by CheeseAndRice
Link to comment
Share on other sites

Hmm, so I can't find a getBlockPosition() in the Entity class or its inheritors but there is a getBlockPosBelowThatAffectsMyMovement() which seems like the same thing, it's a protected method though 

Found it! Was accidentally looking for a getBlockPosition() instead of blockPosition(), which is there. Thanks!

Edited by CheeseAndRice
Link to comment
Share on other sites

Okay, so I figured out how to teleport the player when they sleep in a bed. Problem is that while the ingame tp command removes a player from bed, Entity#moveTo (which I used to teleport) doesn't. I added a delay thinking that might fix it but it doesn't. It just teleports the player without taking them out of the sleeping state/pose, which is kinda funny but not what I'm going for. Gonna look through the tp command code some more and see if I can find out how it wakes the player.

package com.cheeseandrice.myfirstmod.effect;

import com.cheeseandrice.myfirstmod.MyFirstMod;
import net.minecraft.core.BlockPos;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = MyFirstMod.MODID)
public class NoSleepEffect extends MobEffect
{
    int timer = 0;
    boolean sleeping = false;
    public NoSleepEffect(MobEffectCategory mobEffectCategory, int color) {
        super(mobEffectCategory, color);
    }

    @Override
    public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
        if(sleeping) {
            timer++;
            System.out.println(timer);
            if(timer > 20){
                sleeping = false;
                tpInPlace(pLivingEntity);
            }
        }
        super.applyEffectTick(pLivingEntity, pAmplifier);
    }

    @Override
    public boolean isDurationEffectTick(int pDuration, int pAmplifier) {
        return true;
    }

    @SubscribeEvent
    public static void unsleep(PlayerSleepInBedEvent event) {
        Player player = event.getEntity();
        if(player.hasEffect(ModEffects.SLEEPLESSNESS.get())) {
            NoSleepEffect nse = ((NoSleepEffect)player.getEffect(ModEffects.SLEEPLESSNESS.get()).getEffect());
            nse.startTimer();
        }
    }

    public void tpInPlace(LivingEntity entity) {
        BlockPos pos = entity.blockPosition();
        Vec3 newPos = new Vec3(pos.getX(), pos.getY() + 4, pos.getZ());
        entity.moveTo(newPos);
    }

    public void startTimer() {
        timer = 0;
        sleeping = true;
    }

}

 

Edited by CheeseAndRice
Link to comment
Share on other sites

public class NoSleepEffect extends MobEffect
{
    int timer = 0;
    boolean sleeping = false;

Like I said before. You don't store state in the MobEffect.

There is only ever one instance of this class.

This data is player specific. So it would normally need to be stored for each player, e.g. using a capability

But there is already a player.isSleeping() and player.getSleepCounter() so the data you want already exists.

 

For exiting sleeping properly, see ServerPlayer.stopSleepInBed()

But, you can see the normal automatic wake up code in Player.tick()

Edited by warjort

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.

Link to comment
Share on other sites

FOUND IT! The line of code to remove a player from bed, for anyone wondering, is: 

((ServerPlayer)pLivingEntity).stopSleepInBed(true, true);

It basically works good enough for me now, the only problem is that if I don't have a delay of at least 8 ticks before running this line, it has a chance to crash the game with this error: "java.lang.ClassCastException: class net.minecraft.client.player.LocalPlayer cannot be cast to class net.minecraft.server.level.ServerPlayer (net.minecraft.client.player.LocalPlayer and net.minecraft.server.level.ServerPlayer are in module [email protected] of loader 'TRANSFORMER' @7544ac86)" Guessing this has to do with some kind of transition between using the client and server player when you sleep? Tbh my understanding of how to work with server/client stuff is pretty weak. The client Player doesn't have stopSleepInBed, only ServerPlayer does. Seems like if I keep the delay as is though it works fine. Any ideas?

Full class code:

package com.cheeseandrice.myfirstmod.effect;

import com.cheeseandrice.myfirstmod.MyFirstMod;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = MyFirstMod.MODID)
public class NoSleepEffect extends MobEffect
{
    int timer = 0;
    boolean sleeping = false;
    public NoSleepEffect(MobEffectCategory mobEffectCategory, int color) {
        super(mobEffectCategory, color);
    }

    @Override
    public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
        if(sleeping) {
            timer++;
            if (timer > 8) {
                sleeping = false;
                ((ServerPlayer)pLivingEntity).stopSleepInBed(true, true);
            }
        }
        super.applyEffectTick(pLivingEntity, pAmplifier);
    }

    @Override
    public boolean isDurationEffectTick(int pDuration, int pAmplifier) {
        return true;
    }

    @SubscribeEvent
    public static void unsleep(PlayerSleepInBedEvent event) {
        Player player = event.getEntity();
        if(player.hasEffect(ModEffects.SLEEPLESSNESS.get())) {
            NoSleepEffect nse = ((NoSleepEffect)player.getEffect(ModEffects.SLEEPLESSNESS.get()).getEffect());
            nse.startTimer();
        }
    }

    public void startTimer() {
        timer = 0;
        sleeping = true;
    }

}

 

Link to comment
Share on other sites

Your ClassCastException is because a MobEffect ticks on both the client and server.

net.minecraft.client.player.LocalPlayer is the client side implementation of the player.

You need to check pLivingEntity.level.isClientSide

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.

Link to comment
Share on other sites

14 minutes ago, warjort said:
public class NoSleepEffect extends MobEffect
{
    int timer = 0;
    boolean sleeping = false;

Like I said before. You don't store state in the MobEffect.

There is only ever one instance of this class.

This data is player specific. So it would normally need to be stored for each player, e.g. using a capability

But there is already a player.isSleeping() and player.getSleepCounter() so the data you want already exists.

 

For exiting sleeping properly, see ServerPlayer.stopSleepInBed()

But, you can see the normal automatic wake up code in Player.tick()

Just saw your comment. Okay, so when I apply the effect it doesn't create a new MobEffect instance, just a MobEffectInstance instance? I'll try using player.isSleeping() and player.SleepCounter() instead of my variables. Thanks!

Link to comment
Share on other sites

8 minutes ago, warjort said:

Your ClassCastException is because a MobEffect ticks on both the client and server.

net.minecraft.client.player.LocalPlayer is the client side implementation of the player.

You need to check pLivingEntity.level.isClientSide

Got it, so if I do 

if(pLivingEntity.level.isClientSide) ((ServerPlayer)pLivingEntity).stopSleepInBed(true, true);

then it should work?

Or should I be checking if it's not client side?

Edited by CheeseAndRice
Link to comment
Share on other sites

5 minutes ago, CheeseAndRice said:

Got it, so if I do 

if(pLivingEntity.level.isClientSide) ((ServerPlayer)pLivingEntity).stopSleepInBed(true, true);

then it should work?

Why are you asking me? You spent less than 2 minutes thinking about that code and didn't test it.

The answer is obviously no.

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.

Link to comment
Share on other sites

Okay, should've run it first then posted. The solution turned out to be pretty simple after all. Thanks for your help

    @Override
    public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
        if(pLivingEntity.isSleeping()) {
            if(!pLivingEntity.level.isClientSide) ((ServerPlayer)pLivingEntity).stopSleepInBed(true, true);
        }
        super.applyEffectTick(pLivingEntity, pAmplifier);
    }
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.



×
×
  • Create New...

Important Information

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