Jump to content

[1.12] ItemStack NBT data synchronised in SP but not MP?


Recommended Posts

Posted

Hi all,

 

I have some items where the name of the ItemStack varies based on NBT data stored in the stack, using setStackDisplayName() after some logic based on the content of getTagCompound(). In SP, this works fine. In MP, it doesn't, the name show is what I'd expect if the item's NBT tag was empty. Since it worked in SP without any particular synchronisation logic, I assumed that would be the same in MP. Is this wrong? Do I have to handle the sync myself, and if yes, is there a standard way?

 

Thanks.

 

The full class, if it helps:

 

package org.millenaire.common.item;

import org.millenaire.common.utilities.LanguageUtilities;
import org.millenaire.common.utilities.MillCommonUtilities;
import org.millenaire.common.utilities.WorldUtilities;

import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;

public class ItemPurse extends ItemMill {

	private static final String ML_PURSE_DENIER = "ml_Purse_DENIER";
	private static final String ML_PURSE_DENIERARGENT = "ml_Purse_DENIERargent";
	private static final String ML_PURSE_DENIEROR = "ml_Purse_DENIERor";
	private static final String ML_PURSE_RAND = "ml_Purse_rand";

	public ItemPurse(final String itemName) {
		super(itemName);
		setMaxStackSize(1);
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(final World worldIn, final EntityPlayer playerIn,
			final EnumHand handIn) {

		final ItemStack purse = playerIn.getHeldItem(handIn);

		if (totalDeniers(purse) > 0) {
			removeDeniersFromPurse(purse, playerIn);
		} else {
			storeDeniersInPurse(purse, playerIn);
		}

		return super.onItemRightClick(worldIn, playerIn, handIn);
	}

	private void removeDeniersFromPurse(final ItemStack purse, final EntityPlayer player) {
		if (purse.getTagCompound() != null) {
			final int DENIERs = purse.getTagCompound().getInteger(ML_PURSE_DENIER);
			final int DENIERargent = purse.getTagCompound().getInteger(ML_PURSE_DENIERARGENT);
			final int DENIERor = purse.getTagCompound().getInteger(ML_PURSE_DENIEROR);

			int result = MillCommonUtilities.putItemsInChest(player.inventory, MillItems.DENIER, DENIERs);
			purse.getTagCompound().setInteger(ML_PURSE_DENIER, DENIERs - result);

			result = MillCommonUtilities.putItemsInChest(player.inventory, MillItems.DENIER_ARGENT, DENIERargent);
			purse.getTagCompound().setInteger(ML_PURSE_DENIERARGENT, DENIERargent - result);

			result = MillCommonUtilities.putItemsInChest(player.inventory, MillItems.DENIER_OR, DENIERor);
			purse.getTagCompound().setInteger(ML_PURSE_DENIEROR, DENIERor - result);

			purse.getTagCompound().setInteger(ML_PURSE_RAND, player.world.isRemote ? 0 : 1);

			setItemName(purse);
		}
	}

	public void setDeniers(final ItemStack purse, final EntityPlayer player, final int amount) {

		final int denier = amount % 64;
		final int denier_argent = ((amount - denier) / 64) % 64;
		final int denier_or = (amount - denier - (denier_argent * 64)) / (64 * 64);

		setDeniers(purse, player, denier, denier_argent, denier_or);

	}

	public void setDeniers(final ItemStack purse, final EntityPlayer player, final int DENIER, final int DENIERargent,
			final int DENIERor) {
		if (purse.getTagCompound() == null) {
			purse.setTagCompound(new NBTTagCompound());
		}

		purse.getTagCompound().setInteger(ML_PURSE_DENIER, DENIER);
		purse.getTagCompound().setInteger(ML_PURSE_DENIERARGENT, DENIERargent);
		purse.getTagCompound().setInteger(ML_PURSE_DENIEROR, DENIERor);

		purse.getTagCompound().setInteger(ML_PURSE_RAND, player.world.isRemote ? 0 : 1);

		setItemName(purse);
	}

	private void setItemName(final ItemStack purse) {
		if (purse.getTagCompound() == null) {
			purse.setStackDisplayName(I18n.format(MillItems.PURSE.getUnlocalizedName()+".name"));
		} else {
			final int DENIERs = purse.getTagCompound().getInteger(ML_PURSE_DENIER);
			final int DENIERargent = purse.getTagCompound().getInteger(ML_PURSE_DENIERARGENT);
			final int DENIERor = purse.getTagCompound().getInteger(ML_PURSE_DENIEROR);

			String label = "";

			if (DENIERor != 0) {
				label = "\247" + LanguageUtilities.YELLOW + DENIERor + "o ";
			}
			if (DENIERargent != 0) {
				label += "\247" + LanguageUtilities.WHITE + DENIERargent + "a ";
			}
			if ((DENIERs != 0) || (label.length() == 0)) {
				label += "\247" + LanguageUtilities.ORANGE + DENIERs + "d";
			}

			label = label.trim();

			purse.setStackDisplayName("\247" + LanguageUtilities.WHITE + I18n.format(MillItems.PURSE.getUnlocalizedName()+".name") + ": " + label);
		}

	}

	private void storeDeniersInPurse(final ItemStack purse, final EntityPlayer player) {

		final int deniers = WorldUtilities.getItemsFromChest(player.inventory, MillItems.DENIER, 0, Integer.MAX_VALUE);
		final int deniersargent = WorldUtilities.getItemsFromChest(player.inventory, MillItems.DENIER_ARGENT, 0,
				Integer.MAX_VALUE);
		final int deniersor = WorldUtilities.getItemsFromChest(player.inventory, MillItems.DENIER_OR, 0,
				Integer.MAX_VALUE);

		final int total = totalDeniers(purse) + deniers + (deniersargent * 64) + (deniersor * 64 * 64);

		final int new_denier = total % 64;
		final int new_deniers_argent = ((total - new_denier) / 64) % 64;
		final int new_deniers_or = (total - new_denier - (new_deniers_argent * 64)) / (64 * 64);

		setDeniers(purse, player, new_denier, new_deniers_argent, new_deniers_or);
	}

	public int totalDeniers(final ItemStack purse) {
		if (purse.getTagCompound() == null) {
			return 0;
		}

		final int deniers = purse.getTagCompound().getInteger(ML_PURSE_DENIER);
		final int denier_argent = purse.getTagCompound().getInteger(ML_PURSE_DENIERARGENT);
		final int denier_or = purse.getTagCompound().getInteger(ML_PURSE_DENIEROR);

		return deniers + (denier_argent * 64) + (denier_or * 64 * 64);
	}

}

 

Posted (edited)

Got it working. The issue was that setStackDisplayName() stores the calculated name in the NBT compound itself... which is promptly overwritten by the server-side data coming in. So presumably what Mojang had in mind was calculating the name server-side as well, but how to do that without localization? O.o

 

Instead I'm overriding getItemStackDisplayName() on the client-side and recalculating the name there. But that means doing it each time MC needs it, which is wasteful.

 

Anybody has a better idea?

Edited by Kinniken
Posted

All data is server side, the client merely maintains a copy. You want to change the data...? Change it on the server.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

It exists, but it will be overwritten without notice.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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

    • I am having some issues starting an RLCraft server on a minimal install of Debian 12. I have Java installed and I'm able to start the vanilla Minecraft server jar no problem and people can join and play without any issues, as soon as I try to create a new directory with the Forge jar the initial install with the INSTALLER jar works when I use the java command with the --installServer flag, but as soon as I try to start the server using the forge jar that is NOT labelled with installer I get the following error: A problem occurred running the Server launcher.java.lang.reflect.InvocationTargetException         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)         at java.base/java.lang.reflect.Method.invoke(Method.java:569)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.run(ServerLaunchWrapper.java:70)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.main(ServerLaunchWrapper.java:34) Caused by: java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')         at net.minecraft.launchwrapper.Launch.<init>(Launch.java:34)         at net.minecraft.launchwrapper.Launch.main(Launch.java:28)         ... 6 more   I have tried using newer versions of Java directly from Oracle as well. Has anybody been successful in starting and running a RLCraft server from the terminal on a Linux machine? I cannot figure out why it doesn't want to work but the vanilla jar works without issue. Thank you in advance!
    • This is my latest attempt :  public class ManaScreen extends Screen { Mana mana = new Mana(); boolean removeManaBar = false; ResourceLocation manaBar = ResourceLocation.fromNamespaceAndPath(RSGArmoury.MOD_ID, "/textures/block/spawnable_arena_wall.png"); public ManaScreen() { super(Component.literal("Mana")); } @Override protected void init() { super.init(); Minecraft.getInstance().setScreen(this); } @Override public boolean isPauseScreen() { return false; } @Override public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) { pGuiGraphics.blit(manaBar, 10, -10, 0, 0, mana.getMana(), 10, mana.getMana(), 10); if (removeManaBar) { this.onClose(); return; } super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick); } public void addManaBar() { removeManaBar = false; Minecraft.getInstance().setScreen(new ManaScreen()); } public boolean removeManaBar() { return removeManaBar = true; } }
    • I tried a few different things that all didnt work. Right now I have nothing but what I had that seemed most likely to work was just a guiOverlay.blit(x, y, z, vx, vy, getMana()). I dont remember the exact code but it was somthing along those lines. It was in a new class extending screen I believe.
    • yo help pls  i get this error message The game crashed whilst rendering screen Error: java.lang.NullPointerException: Rendering screen https://pastebin.com/fHwr7nXA
    • Please share a link to your crash report, as explained in the FAQ
  • Topics

×
×
  • Create New...

Important Information

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