Jump to content

Recommended Posts

Posted

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");

    }

}

 

Posted

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.

Posted (edited)

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
Posted (edited)

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
Posted (edited)

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
Posted (edited)
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.

Posted

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;
    }

}

 

Posted

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.

Posted
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!

Posted (edited)
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
Posted
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.

Posted

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);
    }

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Forge is probably the most feature loaded mod loader currently. You can change just about anything with the game mechanics by using mixins. You can also add more items and feature than what the docs show you. It is good to start with a Modding tutorial series like this one by Kaupenjoe.  Some good examples of what is possible with forge is clockwork, dynamic trees, and I guess create but its on fabric too.
    • I'm new to modding and trying to make a mod in 1.20.1. My mod currently just has a custom bow, but when I use my bow, it does not play the animation. I have the jsons and pngs in the right spots and I've compared my jsons with that of existing mods, everything seems to line up just fine. Anyone know what might be causing this? I've changed the item between a basic BowItem and a custom class extending from BowItem, even overriding the animation code, no dice. odysseus_bow.json: { "parent": "item/generated", "textures": { "layer0": "odyssey:item/odysseus_bow" }, "overrides": [ { "predicate": { "pulling": 1 }, "model": "odyssey:item/odysseus_bow_pulling_0" }, { "predicate": { "pulling": 1, "pull": 0.65 }, "model": "odyssey:item/odysseus_bow_pulling_1" }, { "predicate": { "pulling": 1, "pull": 0.9 }, "model": "odyssey:item/odysseus_bow_pulling_2" } ] } odysseus_bow_pulling_0.json: { "parent": "item/generated", "textures": { "layer0": "odyssey:item/odysseus_bow_pulling_0" }, "display": { "thirdperson_righthand": { "rotation": [ -80, 260, -40 ], "translation": [ -1, -2, 2.5 ], "scale": [ 1.2, 0.9, 1.2 ] }, "firstperson_righthand": { "rotation": [ 0, -90, 25 ], "translation": [ 1.13, 3.2, 1.13 ], "scale": [ 0.88, 0.68, 0.88 ] } } } (_1 and _2 are the same with the respective layer0)
    • Hello I've been having some issues with Multipart entities and that it is constantly taking damage. The video below shows the issues. Also the hit boxes that are moving constantly seem to be still, because you can still be seated on the hit box where it is suppose to be.  Here is some modified code taken from EnderDragonPart. public class TheHeartPart extends PartEntity<TheHeart> { public final TheHeart parentMob; public final String name; private final EntityDimensions size; public TheHeartPart(TheHeart pParentMob, String pName, float pWidth, float pHeight) { super(pParentMob); this.size = EntityDimensions.scalable(pWidth, pHeight); this.refreshDimensions(); this.parentMob = pParentMob; this.name = pName; } @Override protected void defineSynchedData() { } @Override protected void readAdditionalSaveData(CompoundTag pCompound) { } @Override protected void addAdditionalSaveData(CompoundTag pCompound) { } @Override public boolean isPickable() { return true; } @Nullable public ItemStack getPickResult() { return this.parentMob.getPickResult(); } @Override public boolean hurt(DamageSource pSource, float pAmount) { return false; //return this.isInvulnerableTo(pSource) ? false : this.parentMob.hurt(pSource, pAmount); } @Override public boolean is(Entity pEntity) { return this == pEntity || this.parentMob == pEntity; } @Override public Packet<ClientGamePacketListener> getAddEntityPacket() { throw new UnsupportedOperationException(); } @Override public EntityDimensions getDimensions(Pose pPose) { return this.size; } @Override public boolean shouldBeSaved() {return false;} } Any help is greatly appreciated!
    • I've been trying to run some modpacks through Prism launcher which haven't been working and I narrowed it down due to some forge issues. The issues only happen with modpacks using forge 1.20.1 so I tried to reinstall 1.20.1, but I keep getting this error log:https://pastebin.com/4w9pGURJ 
    • I split your reply to an unrelated topic into a separate request for help.   Please read the FAQ for how to properly request help and post logs.
  • Topics

×
×
  • Create New...

Important Information

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