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



×
×
  • Create New...

Important Information

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