Jump to content

CheeseAndRice

Members
  • Posts

    8
  • Joined

  • Last visited

Everything posted by CheeseAndRice

  1. 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); }
  2. 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?
  3. 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!
  4. 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; } }
  5. 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; } }
  6. 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!
  7. 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!
  8. 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"); } }
×
×
  • Create New...

Important Information

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