Jump to content

[1.12.2] PlayerTickEvent [Solved]


poopoodice

Recommended Posts

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 by poopoodice
Link to comment
Share on other sites

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.

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.

Link to comment
Share on other sites

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 by Differentiation
Link to comment
Share on other sites

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 by poopoodice
Link to comment
Share on other sites

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 by Differentiation
Link to comment
Share on other sites

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 by poopoodice
Link to comment
Share on other sites

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.

  • Like 1

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.

Announcements



×
×
  • Create New...

Important Information

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