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 minecraft@1.19.2 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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I've been trying to make a mod pack for the last day or so now and I haven't been able to launch it once even with all mods disabled. It keeps showing up with Exit Code 1. I gave the modpack to a friend to try and it didn't launch on their pc either due to the same exit code, so I'm not entirely certain what the problem is or how to check it in the logs. So far I've attempted: - Changing from forge version 43.3.0 to 43.3.5 - Downloading Java - Disabling mods (both entirely and individually) I have an NVIDIA GeForce GTX 1070 driver, and I haven't tried to uninstall or reinstall it yet because I'm not certain how to and would have to mess anything up further. I have the latest .txt file of the crash report here: [16:24:34] [main/WARN]: Failed to add PDH Counter: \Paging File(_Total)\% Usage, Error code: 0xC0000BB8 [16:24:34] [main/WARN]: Failed to add counter for PDH counter: \Paging File(_Total)\% Usage [16:24:34] [main/WARN]: Disabling further attempts to query Paging File. [16:24:36] [main/WARN]: COM exception: Invalid Query: SELECT PERCENTUSAGE FROM Win32_PerfRawData_PerfOS_PagingFile [16:24:39] [Datafixer Bootstrap/INFO]: 198 Datafixer optimizations took 149 milliseconds [16:24:40] [Render thread/INFO]: Environment: Environment[sessionHost=https://sessionserver.mojang.com, servicesHost=https://api.minecraftservices.com, name=PROD] [16:24:40] [Render thread/INFO]: Setting user: CatDoodlee [16:24:41] [Render thread/INFO]: Backend library: LWJGL version 3.3.2+13 [16:24:42] [Render thread/INFO]: Reloading ResourceManager: vanilla [16:24:42] [Worker-Main-11/INFO]: Found unifont_all_no_pua-15.1.04.hex, loading [16:24:44] [Render thread/WARN]: Missing sound for event: minecraft:item.goat_horn.play [16:24:44] [Render thread/WARN]: Missing sound for event: minecraft:entity.goat.screaming.horn_break [16:24:44] [Render thread/INFO]: OpenAL initialized on device OpenAL Soft on Speakers (Atrix Wired Elite Headset) [16:24:44] [Render thread/INFO]: Sound engine started [16:24:44] [Render thread/INFO]: Created: 1024x512x4 minecraft:textures/atlas/blocks.png-atlas [16:24:44] [Render thread/INFO]: Created: 256x256x4 minecraft:textures/atlas/signs.png-atlas [16:24:44] [Render thread/INFO]: Created: 512x512x4 minecraft:textures/atlas/shield_patterns.png-atlas [16:24:44] [Render thread/INFO]: Created: 512x512x4 minecraft:textures/atlas/banner_patterns.png-atlas [16:24:44] [Render thread/INFO]: Created: 1024x1024x4 minecraft:textures/atlas/armor_trims.png-atlas [16:24:44] [Render thread/INFO]: Created: 128x64x4 minecraft:textures/atlas/decorated_pot.png-atlas [16:24:44] [Render thread/INFO]: Created: 256x256x4 minecraft:textures/atlas/chest.png-atlas [16:24:44] [Render thread/INFO]: Created: 512x256x4 minecraft:textures/atlas/shulker_boxes.png-atlas [16:24:44] [Render thread/INFO]: Created: 512x256x4 minecraft:textures/atlas/beds.png-atlas [16:24:45] [Render thread/WARN]: Shader rendertype_entity_translucent_emissive could not find sampler named Sampler2 in the specified shader program. [16:24:45] [Render thread/INFO]: Created: 512x256x0 minecraft:textures/atlas/particles.png-atlas [16:24:45] [Render thread/INFO]: Created: 256x256x0 minecraft:textures/atlas/paintings.png-atlas [16:24:45] [Render thread/INFO]: Created: 128x128x0 minecraft:textures/atlas/mob_effects.png-atlas [16:24:45] [Render thread/INFO]: Created: 1024x512x0 minecraft:textures/atlas/gui.png-atlas [16:25:41] [Render thread/INFO]: Stopping! any form of help is greatly appreciated ❤️
    • Tanks for answer, I added SleepTight and I removed Modernfix, but it still crashing: new log new crash log
    • Hi, I'm trying to hide specific vanilla effects such as levitation or slow falling from the inventory GUI, but I haven't been able to figure out how to do this. I need this because they are triggered by another effect and I don't need them all showing up. Unfortunately, I don't know of any mods that include this feature, and I couldn't find any documentation on the process. I've already looked into the IClientMobEffectExtensions interface, but I'm uncertain about how to implement it in my own effect, let alone in any existing vanilla effect. Please help I am using Forge 47.2.0. for Minecraft 1.20.1
    • Also wondering how to create these manually.  First time creating a forge server.
  • Topics

×
×
  • Create New...

Important Information

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