poopoodice Posted October 15, 2019 Posted October 15, 2019 (edited) I've registered the event using MinecraftForge.EVENT_BUS.register @SubscribeEvent public void onPlayerTick(PlayerTickEvent event) { EntityPlayer player = Minecraft.getMinecraft().player; if(player != null) { ItemStack itemstack = player.getHeldItemMainhand(); if(itemstack.areItemStacksEqual(itemstack, new ItemStack(Items.FEATHER))) { this.holding = true; } if(this.holding && player.isAirBorne) { event.player.fallDistance = 0.0F; event.player.motionY *= 0.90D; player.velocityChanged = true; } this.holding = false; } } So basically everything works fine, whenever the player is holding a feather, the player doesn't take any fall damage and the player falls slower as well. But when I quit the game (singleplayer) and rejoin, everything just not working anymore, the player is no longer has a slower falling speed and it now takes fall damage. What might cause these? Edited October 17, 2019 by poopoodice Quote
Animefan8888 Posted October 15, 2019 Posted October 15, 2019 32 minutes ago, poopoodice said: this.holding = true; You can't do this in an event class. They are singletons. Just do the areItemStacksEqual check every tick. Also the event parameter has a getPlayer method use that instead of Minecraft.getMinecraft().player because that is client only and thus you are reaching across logical sides. Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Differentiation Posted October 16, 2019 Posted October 16, 2019 (edited) 2 hours ago, poopoodice said: I've registered the event using MinecraftForge.EVENT_BUS.register @SubscribeEvent public void onPlayerTick(PlayerTickEvent event) { EntityPlayer player = Minecraft.getMinecraft().player; if(player != null) { ItemStack itemstack = player.getHeldItemMainhand(); if(itemstack.areItemStacksEqual(itemstack, new ItemStack(Items.FEATHER))) { this.holding = true; } if(this.holding && player.isAirBorne) { event.player.fallDistance = 0.0F; event.player.motionY *= 0.90D; player.velocityChanged = true; } this.holding = false; } } So basically everything works fine, whenever the player is holding a feather, the player doesn't take any fall damage and the player falls slower as well. But when I quit the game (singleplayer) and rejoin, everything just not working anymore, the player is no longer has a slower falling speed and it now takes fall damage. What might cause these? You're reaching across logical sides. Use PlayerTickEvent#player. Don't use Minecraft::getMinecraft unless you have to (adding client effects, etc.). Edited October 16, 2019 by Differentiation Quote
poopoodice Posted October 16, 2019 Author Posted October 16, 2019 (edited) Solved by removing boolean variable holding One thing I want to mention is I need to use Minecraft.getMinecraft().player.velocityChanged = true; because when I use event.player.velocityChanged = true; the player will not be able to move in the air, and that's not what I want Thanks for the replies. Edited October 16, 2019 by poopoodice Quote
Differentiation Posted October 16, 2019 Posted October 16, 2019 (edited) 2 hours ago, poopoodice said: Solved by removing boolean variable holding One thing I want to mention is I need to use Minecraft.getMinecraft().player.velocityChanged = true; because when I use event.player.velocityChanged = true; the player will not be able to move in the air, and that's not what I want Thanks for the replies. Well, if you're gonna use that then at least include a check for World#isRemote. Otherwise you'd crash with - I believe - ClassDefNotFoundException on a dedicated server. Also, what do you mean move in the air?? The client thread does run on that event. Lastly, just use EntityPlayer#getHeldItemMainhand method to check for the item they are holding instead of what you're doing. Edited October 16, 2019 by Differentiation Quote
poopoodice Posted October 16, 2019 Author Posted October 16, 2019 (edited) 8 hours ago, Differentiation said: Well, if you're gonna use that then at least include a check for World#isRemote. Otherwise you'd crash with - I believe - ClassDefNotFoundException on a dedicated server. Also, what do you mean move in the air?? The client thread does run on that event. Lastly, just use EntityPlayer#getHeldItemMainhand method to check for the item they are holding instead of what you're doing. Well, it didn't crash or appear any error (I haven't tried it on multiplayer What I mean by the player can't move in the air is when "player.isAirBorne" and it doesn't really matter. I was trying to say when using event.player.velocityChanged = true the player won't be able to move in x and z direction. unless change event.player to Minecraft.getMinecraft().player I don't know why but it is just like this. I might record a vd later on to explain this more clearly. Edited October 16, 2019 by poopoodice Quote
SerpentDagger Posted October 16, 2019 Posted October 16, 2019 9 hours ago, Differentiation said: Well, if you're gonna use that then at least include a check for World#isRemote. Otherwise you'd crash with - I believe - ClassDefNotFoundException on a dedicated server. Sadly that isn't enough. Any reference to Minecraft.getMinecraft() will crash the dedicated server with that exception as soon as the class is loaded. The offending code must be isolated in a separate class and stripped from the server's .jar file using @SideOnly. Careful though, because then the code won't exist at all on the server, so you can still enjoy a crash by referencing a method stripped by @SideOnly. This is why proxies are useful. 52 minutes ago, poopoodice said: Well, it didn't crash or appear any error 52 minutes ago, poopoodice said: I haven't tried it on multiplayer Like Differentiation said, it crashes a dedicated server. (take a look at this) 58 minutes ago, poopoodice said: the player won't be able to move in x and z direction. Not exactly sure why this is, since you're not modifying x/z motion, but my guess would be all the reaching across logical sides that you're doing, which can cause some seemingly weird bugs. 1 Quote Fancy 3D Graphing Calculator mod, with many different coordinate systems. Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.
Differentiation Posted October 16, 2019 Posted October 16, 2019 1 hour ago, poopoodice said: Well, it didn't crash or appear any error (I haven't tried it on multiplayer What I mean by the player can't move in the air is when "player.isAirBorne" and it doesn't really matter. I was trying to say when using event.player.velocityChanged = true the player won't be able to move in x and z direction. unless change event.player to Minecraft.getMinecraft().player I don't know why but it is just like this. I might record a vd later on to explain this more clearly. I don't really understand what you're trying to do here... You have to understand that Minecraft::getMinecraft is a method only on the client thread. And the Minecraft class is only a class on the client thread. You cannot reference these on the running server thread. This will undoubtedly crash a dedicated server. Quote
Differentiation Posted October 16, 2019 Posted October 16, 2019 21 minutes ago, diesieben07 said: And cause strange, rare, hard to debug and inexplicable bug in singleplayer. I'm trying to find that Forge thread for reaching across logical sides to insert here. Do you happen to know the link? Quote
poopoodice Posted October 17, 2019 Author Posted October 17, 2019 After all, I notice that player.velocityChanged was not necessary and I decided to move code from the eventhandler to an itemclass ( in onUpdate() ) @Override public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { if(isSelected && entityIn instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) entityIn; player.fallDistance = 0.0F; player.motionY *= 0.90D; } } I just want to know if this fixes all the problems? Or I will still need some checkings and even more things to solve the client-server issue? Quote
Differentiation Posted October 17, 2019 Posted October 17, 2019 12 minutes ago, poopoodice said: After all, I notice that player.velocityChanged was not necessary and I decided to move code from the eventhandler to an itemclass ( in onUpdate() ) @Override public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { if(isSelected && entityIn instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) entityIn; player.fallDistance = 0.0F; player.motionY *= 0.90D; } } I just want to know if this fixes all the problems? Or I will still need some checkings and even more things to solve the client-server issue? You're good. Now its solved. Note: you don't have to make an instance of EntityPlayer, you can just use Entity. Quote
poopoodice Posted October 17, 2019 Author Posted October 17, 2019 (edited) 1 minute ago, Differentiation said: You're good. Now its solved. Note: you don't have to make an instance of EntityPlayer, you can just use Entity. Okay, thanks for the advice. Appreciated. Edited October 17, 2019 by poopoodice Quote
Recommended Posts
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.