Jump to content

[1.12.2] Changing the player's food and saturation levels


Recommended Posts

Posted

So, I did this:

	@Override
	public void onWornTick(ItemStack itemstack, EntityLivingBase player)
	{
		if(player instanceof EntityPlayer)
		{
			EntityPlayer player1 = (EntityPlayer) player;
			FoodStats food = player1.getFoodStats();
			
			if(player1.ticksExisted % 100 == 0)
			{
				food.setFoodLevel(20);
				
				if(food.getSaturationLevel() == 0)
				{
					food.setFoodSaturationLevel(10);
				}
			}
		}
	}

But on servers, it causes some problems, such as making the player unable to log back in, unless their playerdata is deleted. Apparently, I need to do reflection to make it work on a server? How would I go about that?

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
14 minutes ago, Daring Do said:

But on servers, it causes some problems, such as making the player unable to log back in, unless their playerdata is deleted.

Post the log for the server.

14 minutes ago, Daring Do said:

Apparently, I need to do reflection to make it work on a server?

Uh, no. Where did you hear that?

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.

Posted
8 minutes ago, Animefan8888 said:

Post the log for the server.

Uh, no. Where did you hear that?

Yes I can: https://pastebin.com/Z905PT6n

 

And on MDC, since apparently getFoodStats() is client side only. 

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
22 minutes ago, Daring Do said:

apparently getFoodStats() is client side only. 

No it isn't. FoodStats#setFoodSaturationLevel is though. So in order to modify saturation you indeed need reflection. Or I guess you could write the food stats to the NBT, modify the foodSaturationLevel tag and then read the stats from the modified NBT.

Posted
7 minutes ago, V0idWa1k3r said:

No it isn't. FoodStats#setFoodSaturationLevel is though. So in order to modify saturation you indeed need reflection. Or I guess you could write the food stats to the NBT, modify the foodSaturationLevel tag and then read the stats from the modified NBT.

Okay, well, how would I go about the reflection for the saturation?

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
Just now, V0idWa1k3r said:

Do you need a tutorial on java's reflections? What is your question here?

According to literally everything I could find about doing reflections in modding, regular Java reflections don't work. Instead, you have to use the ReflectionHandler, which I've got no idea how to use...

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
7 minutes ago, Daring Do said:

you have to use the ReflectionHandler

There isn't even such a class in all the libraries forge uses including forge itself.

 

7 minutes ago, Daring Do said:

According to literally everything I could find about doing reflections in modding, regular Java reflections don't work.

This is absolute and total bollocks. I have no idea where you got that impression from but regular reflections work absolutely fine. The only catch is that you have to search 2 methods/fields at the same time - the one with the DEV name and the one with the SRG name. Forge has a utility class ReflectionHelper that allows you to pass multiple names for a field/method when getting it.

Posted (edited)
7 minutes ago, V0idWa1k3r said:

There isn't even such a class in all the libraries forge uses including forge itself.

 

This is absolute and total bollocks. I have no idea where you got that impression from but regular reflections work absolutely fine. The only catch is that you have to search 2 methods/fields at the same time - the one with the DEV name and the one with the SRG name. Forge has a utility class ReflectionHelper that allows you to pass multiple names for a field/method when getting it.

ReflectionHelper, not handler, sorry. And it was from other posts on this site for outdated versions, as well as on other sites for also outdated versions. And also on MDC, from UpcraftLP.

 

And, in general, I have no idea how to do reflections in any form. Up until an hour ago, I didn't even know such a thing existed.

Edited by Daring Do

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
4 minutes ago, Daring Do said:

And it was from other posts on this site for outdated versions, as well as on other sites for also outdated versions.

The version doesn't matter. Reflection always worked just fine. You always had to supply 2 names to it and it would always work. There is no "magic" in minecraft that makes it somehow not work with basics of the language it is written it. Minecraft is just as valid of a java application as any other one and as such is completely compatible with java's API.

 

6 minutes ago, Daring Do said:

MDC, from UpcraftLP.

I have no idea what MDC is or who UpcraftLP is but they are not telling you the truth.

Posted
Just now, V0idWa1k3r said:

The version doesn't matter. Reflection always worked just fine. You always had to supply 2 names to it and it would always work. There is no "magic" in minecraft that makes it somehow not work with basics of the language it is written it. Minecraft is just as valid of a java application as any other one and as such is completely compatible with java's API.

According to those other sites, it's because Minecraft's code is obfuscated, so in the dev environment, it works fine, but when built, it apparently doesn't. Heck if I know, though.

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
7 minutes ago, Daring Do said:

it's because Minecraft's code is obfuscated, so in the dev environment, it works fine, but when built, it apparently doesn't.

This is true and this is exactly why you provide 2 names for the field/method you are looking up. One is a MCP name, that works in your dev environment. The other is an SRG name for the normal "obfuscated"-ish environment. It doesn't prevent you from using reflection, just makes you do an extra lookup. Or a check for the environment.

Posted
7 minutes ago, V0idWa1k3r said:

This is true and this is exactly why you provide 2 names for the field/method you are looking up. One is a MCP name, that works in your dev environment. The other is an SRG name for the normal "obfuscated"-ish environment. It doesn't prevent you from using reflection, just makes you do an extra lookup. Or a check for the environment.

Ah. I... still have no idea how to do it x3

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted

Create a new field that will hold the reference to the foodSaturationLevel field, instantinate it as ReflectionHelper.findField and then use that field just as you would with normal java reflection.

 

public static final Field foodStats_foodSaturationLevel = ReflectionHelper.findField(FoodStats.class, "MCPNAME", "SRGNAME");

 

You can learn the field's SRG name with MCPBot or locally on your computer in %user_home%/.gradle/caches/minecraft/de/oceanlabs/mcp/mcp_snapshot/%snapshot_date%/fields.csv.

Posted (edited)
21 minutes ago, V0idWa1k3r said:

Create a new field that will hold the reference to the foodSaturationLevel field, instantinate it as ReflectionHelper.findField and then use that field just as you would with normal java reflection.

 


public static final Field foodStats_foodSaturationLevel = ReflectionHelper.findField(FoodStats.class, "MCPNAME", "SRGNAME");

 

You can learn the field's SRG name with MCPBot or locally on your computer in %user_home%/.gradle/caches/minecraft/de/oceanlabs/mcp/mcp_snapshot/%snapshot_date%/fields.csv.

I can only find getSaturationLevel()'s SRG name, not setSaturationLevel()'s. That being field_71147_cj

Edited by Daring Do

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
8 minutes ago, Daring Do said:

I can only find getSaturationLevel()'s SRG name, not setSaturationLevel()'s. That being field_71147_cj

You do not need to find a name of a method. The method you used is client-only which crashes the game, reflection isn't going to magically recreate a method for you, and the other one is public anyway. You need to modify the the FoodStats.foodSaturationLevel field

Also field_71147_cj is EntityPlayerMP.wasHungry. It makes no sense for a method to be named field_xxxxx

Posted
1 minute ago, V0idWa1k3r said:

You do not need to find a name of a method. The method you used is client-only which crashes the game, reflection isn't going to magically recreate a method for you, and the other one is public anyway. You need to modify the the FoodStats.foodSaturationLevel field

Also field_71147_cj is EntityPlayerMP.wasHungry. It makes no sense for a method to be named field_xxxxx

Oh

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted

Okay, so this is what I did:

	public static final Field saturationLevel = ReflectionHelper.findField(FoodStats.class, "field_75125_b", "foodSaturationLevel");
	
	@Override
	public void onWornTick(ItemStack itemstack, EntityLivingBase player)
	{
		if(player instanceof EntityPlayer)
		{
			EntityPlayer player1 = (EntityPlayer) player;
			FoodStats food = player1.getFoodStats();
			
			if(player1.ticksExisted % 100 == 0)
			{
				food.setFoodLevel(20);
				
				try
				{
					saturationLevel.setAccessible(true);
					saturationLevel.set(player1, 10);
				}
				catch(Throwable e)
				{
					Log.warn("Reflection on Player Saturation Level Failed");
					e.printStackTrace();
				}
			}
		}
	}

And to no one's surprise, it doesn't work, and instead just throws out the warning instead :/

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
10 minutes ago, Daring Do said:

saturationLevel.set(player1, 10);

EntityPlayer can't be an instance of FoodStats.

 

10 minutes ago, Daring Do said:

saturationLevel.setAccessible(true);

This operation is quite expensie. Do it in your class initializer.

Posted
2 hours ago, diesieben07 said:

This is not necessary when using ReflectionHelper.

 

This is the worst way to handle exceptions.

  • Never catch Throwable. It includes things like OutOfMemoryError, which you should never catch.
  • Don't just do nothing with the exception but log it. Is there any reasonable circumstance you can think of where this exception should occur? No? Okay, then you should not just carry on! If this exception happens, something is very wrong. Let the game crash.

Okay, so, this is what I have now:

	public static final Field saturationLevel = ReflectionHelper.findField(FoodStats.class, "field_75125_b", "foodSaturationLevel");

	@Override
	public void onWornTick(ItemStack itemstack, EntityLivingBase player)
	{
		if(player instanceof EntityPlayer)
		{
			EntityPlayer player1 = (EntityPlayer) player;
			FoodStats food = player1.getFoodStats();
			
			if(player1.ticksExisted % 100 == 0)
			{
				food.setFoodLevel(20);
				
				if(food.getSaturationLevel() == 0)
				{
					try
					{
						saturationLevel.set(food, 10);
					}
					catch(IllegalAccessException e)
					{
						Log.error("Reflection on Player Saturation Level Failed");
						e.printStackTrace();
					}
				}
			}
		}
	}

And it works. Idk if I'm catching the right thing now, that's one of the things the saturationLevel.set() throws, apparently. I'll need to test it outside of the dev workspace still, but I think it's functional now? Do I still have something wrong? Anything that needs to be changed?

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

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.