Jump to content

[1.7.10][Unsolved] Adding NBT tags appears to do nothing


Recommended Posts

Posted

What I want to do is have certain values about an item saved to the item using NBT. The item has its own inventory, so it seemed like a good idea to load/save the values after the inventory contents had been loaded/saved. As far as I can tell though, the NBT tags other than the list of items are not actually being saved anywhere (and so when loaded are empty).

 

Below are the three classes linked to the process. ConfigGui handles the changing of these values, Config handles loading the default values (from a config file) and storing the current values in memory, and BagInventory unsurprisingly is where the inventory is saved/loaded (the NBT methods are right at the bottom).

 

You may notice that in ConfigGui updateInventoryNBT() isn't actually being called: it was in initGui(), and both the button and slider listeners however caused some interesting problems where every value was set to 0 when the GUI was opened (and some of the sliders were actually set to < 0, which was a little odd).

 

ConfigGui

 

 

public class GuiConfig extends GuiScreen implements ISlider
{
private Config config;

private static final ResourceLocation texture = new ResourceLocation("roboguy99", "textures/gui/Config.png"); // Background texture

// Component IDs
private static final int ID_BACKGROUND_RED = 0;
private static final int ID_BACKGROUND_GREEN = 1;
private static final int ID_BACKGROUND_BLUE = 2;

private static final int ID_HIGHLIGHT_RED = 3;
private static final int ID_HIGHLIGHT_GREEN = 4;
private static final int ID_HIGHLIGHT_BLUE = 5;

private static final int ID_TRIANGLES = 6;
private static final int ID_RADIUS = 7;
private static final int ID_ITEM_RADIUS = 8;
private static final int ID_ITEM_RADIUS_AUTOMATIC = 9;

private static final int ID_NAME = 10;

private static final int ID_MUTE = 11;
private static final int ID_RESET = 12;

// Gui sizes
private final int xSize = 215;
private final int ySize = 226;

private int guiLeft, guiTop;

// Components
private GuiSlider sldHighlightRed;
private GuiSlider sldHighlightGreen;
private GuiSlider sldHighlightBlue;

private GuiSlider sldBackgroundRed;
private GuiSlider sldBackgroundGreen;
private GuiSlider sldBackgroundBlue;

private GuiSlider sldTriangles;
private GuiSlider sldRadius;
private GuiSlider sldItemRadius;

private GuiTextField txtName;

@Override
public void initGui()
{
	this.guiLeft = (this.width - this.xSize) / 2;
	this.guiTop = (this.height - this.ySize) / 2;

	this.config = HotbarBag.instance.config;

	// Create instances of each component TODO Switch out the huge ocean of magic numbers before you're banned from technology forever
	this.sldHighlightRed = new GuiSlider(this.ID_HIGHLIGHT_RED, this.guiLeft + 5, this.guiTop + 17, 100, 20, "Red ", "", 0d, 255d, this.config.getHighlightRed(), false, true, this);
	this.sldHighlightGreen = new GuiSlider(this.ID_HIGHLIGHT_GREEN, this.guiLeft + 5, this.guiTop + 17 + 22, 100, 20, "Green ", "", 0d, 255d, this.config.getHighlightGreen(), false, true, this);
	this.sldHighlightBlue = new GuiSlider(this.ID_HIGHLIGHT_BLUE, this.guiLeft + 5, this.guiTop + 17 + 22 * 2, 100, 20, "Blue ", "", 0d, 255d, this.config.getHighlightBlue(), false, true, this);

	this.sldBackgroundRed = new GuiSlider(this.ID_BACKGROUND_RED, this.guiLeft + 5 + 100 + 5, this.guiTop + 17, 100, 20, "Red ", "", 0d, 255d, this.config.getBackgroundRed(), false, true, this);
	this.sldBackgroundGreen = new GuiSlider(this.ID_BACKGROUND_GREEN, this.guiLeft + 5 + 100 + 5, this.guiTop + 17 + 22, 100, 20, "Green ", "", 0d, 255d, this.config.getBackgroundGreen(), false, true, this);
	this.sldBackgroundBlue = new GuiSlider(this.ID_BACKGROUND_BLUE, this.guiLeft + 5 + 100 + 5, this.guiTop + 17 + 22 * 2, 100, 20, "Blue ", "", 0d, 255d, this.config.getBackgroundBlue(), false, true, this);

	this.sldTriangles = new GuiSlider(this.ID_TRIANGLES, this.guiLeft + 5, this.guiTop + 17 + 22 * 3 + 10, 100, 20, "Triangles ", "", 10000d, 100000d, this.config.getTriangles(), false, true, this);
	this.sldRadius = new GuiSlider(this.ID_RADIUS, this.guiLeft + 5, this.guiTop + 17 + 22 * 4 + 10, 100, 20, "Radius ", "", 10d, 120d, this.config.getRadius(), false, true, this);
	this.sldItemRadius = new GuiSlider(this.ID_ITEM_RADIUS, this.guiLeft + 5, this.guiTop + 17 + 22 * 5 + 10, 100, 20, "Item Radius ", "", 10d, 120d, this.config.getItemRadius(), false, true, this);

	// Add each component to the GUI
	this.buttonList.add(this.sldHighlightRed);
	this.buttonList.add(this.sldHighlightGreen);
	this.buttonList.add(this.sldHighlightBlue);

	this.buttonList.add(this.sldBackgroundRed);
	this.buttonList.add(this.sldBackgroundGreen);
	this.buttonList.add(this.sldBackgroundBlue);

	this.buttonList.add(this.sldTriangles);
	this.buttonList.add(this.sldRadius);
	this.buttonList.add(this.sldItemRadius);
	this.buttonList.add(new GuiButton(this.ID_ITEM_RADIUS_AUTOMATIC, this.guiLeft + 5 + 100 + 5, this.guiTop + 17 + 22 * 5 + 10, 52, 20, this.config.isUpdateItemRadiusAutomatic() ? "Automatic" : "Manual"));

	this.txtName = new GuiTextField(this.fontRendererObj, this.guiLeft + 5, this.guiTop + 17 + 22 * 6 + 20, 205, 20);

	this.buttonList.add(new GuiButton(this.ID_MUTE, this.guiLeft + 5, this.guiTop + 17 + 22 * 7 + 30, 52, 20, this.config.isMuted() ? "Muted" : "Unmuted"));
	this.buttonList.add(new GuiButton(this.ID_RESET, this.guiLeft + 5 + 52 + 5, this.guiTop + 17 + 22 * 7 + 30, 52, 20, "Reset"));

	// Text field setup TODO finish this
	this.txtName.setEnableBackgroundDrawing(true);
	this.txtName.setMaxStringLength(30);
	if (this.config.getName() != null) this.txtName.setText(this.config.getName());
}

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
	int k = this.guiLeft;
	int l = this.guiTop;

	// Draw background
	this.mc.getTextureManager().bindTexture(texture);
	this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);

	this.txtName.drawTextBox();

	// Add text labels, coloured to RGB values.
	this.fontRendererObj.drawString("Highlight Colour", this.guiLeft + 17, this.guiTop + 5, new Color(this.config.getHighlightRed(), this.config.getHighlightGreen(), this.config.getHighlightBlue()).getRGB(), false);
	this.fontRendererObj.drawString("Background Colour", this.guiLeft + 5 + 100 + 10, this.guiTop + 5, new Color(this.config.getBackgroundRed(), this.config.getBackgroundGreen(), this.config.getBackgroundBlue()).getRGB(), false);

	super.drawScreen(mouseX, mouseY, partialTicks);

}

@Override
public void onChangeSliderValue(GuiSlider slider)
{
	if (slider == this.sldBackgroundRed) this.config.setBackgroundRed(slider.getValueInt());
	if (slider == this.sldBackgroundGreen) this.config.setBackgroundGreen(slider.getValueInt());
	if (slider == this.sldBackgroundBlue) this.config.setBackgroundBlue(slider.getValueInt());

	if (slider == this.sldHighlightRed) this.config.setHighlightRed(slider.getValueInt());
	if (slider == this.sldHighlightGreen) this.config.setHighlightGreen(slider.getValueInt());
	if (slider == this.sldHighlightBlue) this.config.setHighlightBlue(slider.getValueInt());

	if (slider == this.sldTriangles) this.config.setTriangles(slider.getValueInt());
	if (slider == this.sldRadius)
	{
		this.config.setRadius(slider.getValueInt());
		if (this.config.isUpdateItemRadiusAutomatic()) this.config.setItemRadius(Math.round(slider.getValueInt() * 0.71f));
	}
	if (slider == this.sldItemRadius) this.config.setItemRadius(slider.getValueInt());
}

@Override
protected void actionPerformed(GuiButton btn)
{
	if (btn.id == this.ID_ITEM_RADIUS_AUTOMATIC) this.config.setUpdateItemRadiusAutomatically(!this.config.isUpdateItemRadiusAutomatic());
	if (btn.id == this.ID_MUTE) this.config.setMuted(true);

	if (btn.id == this.ID_RESET) // Reset everything back to the default value
	{
		this.config.setBackgroundRed(this.config.DEFAULT_BACKGROUND_RED);
		this.config.setBackgroundGreen(this.config.DEFAULT_BACKGROUND_GREEN);
		this.config.setBackgroundBlue(this.config.DEFAULT_BACKGROUND_BLUE);

		this.config.setHighlightRed(this.config.DEFAULT_HIGHLIGHT_RED);
		this.config.setHighlightGreen(this.config.DEFAULT_HIGHLIGHT_GREEN);
		this.config.setHighlightBlue(this.config.DEFAULT_HIGHLIGHT_BLUE);

		this.config.setTriangles(this.config.DEFAULT_TRIANGLES);
		this.config.setRadius(this.config.DEFAULT_RADIUS);
		this.config.setItemRadius(this.config.DEFAULT_ITEM_RADIUS);
		this.config.setName(this.config.DEFAULT_NAME);

		this.sldBackgroundRed.setValue(this.config.getBackgroundRed());
		this.sldBackgroundGreen.setValue(this.config.getBackgroundGreen());
		this.sldBackgroundBlue.setValue(this.config.getBackgroundBlue());

		this.sldHighlightRed.setValue(this.config.getHighlightRed());
		this.sldHighlightGreen.setValue(this.config.getHighlightGreen());
		this.sldHighlightBlue.setValue(this.config.getHighlightBlue());

		this.sldTriangles.setValue(this.config.getTriangles());
		this.sldRadius.setValue(this.config.getRadius());
		this.sldItemRadius.setValue(this.config.getItemRadius());

		this.txtName.setText(this.config.getName());

		this.sldBackgroundRed.updateSlider();
		this.sldBackgroundGreen.updateSlider();
		this.sldBackgroundBlue.updateSlider();

		this.sldHighlightRed.updateSlider();
		this.sldHighlightGreen.updateSlider();
		this.sldHighlightBlue.updateSlider();

		this.sldTriangles.updateSlider();
		this.sldRadius.updateSlider();
		this.sldItemRadius.updateSlider();
	}
}

private void updateInventoryNBT()
{
	BagInventory inventory;
	ItemStack heldItem = Minecraft.getMinecraft().thePlayer.getHeldItem();
	if (heldItem.getItem() instanceof ItemBag) inventory = new BagInventory(heldItem);
}

@Override
public boolean doesGuiPauseGame()
{
	return true;
}

 

 

 

Config

 

 

public class Config
{
public final int DEFAULT_BACKGROUND_RED, DEFAULT_BACKGROUND_GREEN, DEFAULT_BACKGROUND_BLUE;
public final int DEFAULT_HIGHLIGHT_RED, DEFAULT_HIGHLIGHT_GREEN, DEFAULT_HIGHLIGHT_BLUE;
public final int DEFAULT_TRIANGLES, DEFAULT_RADIUS, DEFAULT_ITEM_RADIUS;
public final boolean DEFAULT_MUTE, DEFAULT_UPDATE_ITEMS_AUTO;
public final String DEFAULT_NAME;

private int backgroundRed, backgroundGreen, backgroundBlue, highlightRed, highlightGreen, highlightBlue, triangles, radius, itemRadius;
private boolean updateItemRadiusAutomatically, mute;
private String name;

private final Configuration config;

public Config(FMLPreInitializationEvent event)
{
	this.config = new Configuration(event.getSuggestedConfigurationFile());

	this.config.load();
	{
		this.DEFAULT_BACKGROUND_RED = this.config.getInt("BackgroundRed", "Background Colours", 127, 0, 255, "RGB red value for selector background");
		this.DEFAULT_BACKGROUND_GREEN = this.config.getInt("BackgroundGreen", "Background Colours", 127, 0, 255, "RGB green value for selector background");
		this.DEFAULT_BACKGROUND_BLUE = this.config.getInt("BackgroundBlue", "Background Colours", 127, 0, 255, "RGB blue value for selector background");

		this.DEFAULT_HIGHLIGHT_RED = this.config.getInt("HighlightRed", "Highlight Colours", 0, 0, 255, "RGB red value for selector highlighted sector");
		this.DEFAULT_HIGHLIGHT_GREEN = this.config.getInt("HighlightGreen", "Highlight Colours", 102, 0, 255, "RGB green value for selector highlighted sector");
		this.DEFAULT_HIGHLIGHT_BLUE = this.config.getInt("HighlightBlue", "Highlight Colours", 153, 0, 255, "RGB blue value for selector highlighted sector");

		this.DEFAULT_TRIANGLES = this.config.getInt("Triangles", "Circle Settings", 20000, 10000, 100000, "Number of triangles used to draw circle. More triangles = higher definition = more lag");
		this.DEFAULT_RADIUS = this.config.getInt("Radius", "Circle Settings", 100, 10, 120, "The radius of the circle");
		this.DEFAULT_ITEM_RADIUS = this.config.getInt("ItemRadius", "Circle Settings", 85, 10, 120, "The radius of the items");

		this.DEFAULT_UPDATE_ITEMS_AUTO = this.config.getBoolean("AutoItemRadius", "General", true, "Update item radius automatically when radius is changed. You can still manually change both values.");
		this.DEFAULT_MUTE = this.config.getBoolean("Mute", "General", false, "Mute the sound when moving the mouse between sectors");
		this.DEFAULT_NAME = this.config.getString("Name", "General", "Hotbar Bag", "The name of the bag");
	}
	this.config.save();
}
        //A multitude of setters and getters I don't really need to include for each private field
}

 

 

 

BagInventory

 

 

public class BagInventory implements IInventory
{
public static final int SIZE = 18; // Equal to 2 rows - must be a multiple of 9

private final ItemStack invItem;
ItemStack[] inventory = new ItemStack[size];

public BagInventory(ItemStack itemStack)
{
	this.invItem = itemStack;

	// If for any reason the itemstack has no NBT data
	if (itemStack != null && !itemStack.hasTagCompound()) // Sometimes if you're too fast the held item returns null when it isn't
	{
		itemStack.setTagCompound(new NBTTagCompound());
	}

	// Read inventory from NBT
	if (itemStack != null) this.readFromNBT(itemStack.getTagCompound());

	for(int i = 0; i < 17; i++) // Repeat process 17 times to remove all gaps, not just the first one
	{
		for(int j = this.getSizeInventory() - 1; j >= 0; j--)
		{
			if (this.getStackInSlot(j) == null && !(j + 1 == this.getSizeInventory()))
			{
				this.setInventorySlotContents(j, this.getStackInSlot(j + 1));
				this.setInventorySlotContents(j + 1, null);
			}
		}
	}
}

@Override
public int getSizeInventory()
{
	return this.inventory.length;
}

@Override
public ItemStack getStackInSlot(int slot)
{
	return this.inventory[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amount)
{
	ItemStack stack = this.getStackInSlot(slot);

	if (stack != null)
	{
		if (stack.stackSize > amount)
		{
			stack = stack.splitStack(amount);
			if (stack.stackSize == 0)
			{
				this.setInventorySlotContents(slot, null);
			}
		}
		else
		{
			this.setInventorySlotContents(slot, null);
		}
		this.markDirty();
	}
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot)
{
	ItemStack stack = this.getStackInSlot(slot);

	if (stack != null)
	{
		this.setInventorySlotContents(slot, null);
	}
	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemStack)
{
	this.inventory[slot] = itemStack;

	if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit())
	{
		itemStack.stackSize = this.getInventoryStackLimit();
	}

	this.markDirty();
}

@Override
public String getInventoryName()
{
	return HotbarBag.instance.config.getName();
}

@Override
public boolean hasCustomInventoryName()
{
	return HotbarBag.instance.config.getName().length() > 0;
}

@Override
public int getInventoryStackLimit()
{
	return 64;
}

@Override
public void markDirty()
{
	for(int i = 0; i < this.getSizeInventory(); ++i)
	{
		if (this.getStackInSlot(i) != null && this.getStackInSlot(i).stackSize == 0)
		{
			this.inventory[i] = null;
		}
	}

	if (this.invItem != null) this.writeToNBT(this.invItem.getTagCompound());
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityPlayer)
{
	return true;
}

@Override
public void openInventory()
{
}

@Override
public void closeInventory()
{
}

@Override
public boolean isItemValidForSlot(int slot, ItemStack itemStack)
{
	return !(itemStack.getItem() instanceof ItemBag);
}

public void readFromNBT(NBTTagCompound compound)
{
	NBTTagList items = compound.getTagList("ItemBag", NBT.TAG_COMPOUND);

	for(int i = 0; i < items.tagCount(); i++)
	{
		NBTTagCompound item = items.getCompoundTagAt(i);
		int slot = item.getInteger("Slot");

		// Double-check has loaded correctly.
		if (slot >= 0 && slot < this.getSizeInventory())
		{
			this.inventory[slot] = ItemStack.loadItemStackFromNBT(item);
		}
	}

	Config config = HotbarBag.instance.config; // Get config instance

	// Read item settings
	config.setBackgroundRed(compound.getInteger("backgroundRed"));
	config.setBackgroundGreen(compound.getInteger("backgroundGreen"));
	config.setBackgroundBlue(compound.getInteger("backgroundBlue"));

	config.setHighlightRed(compound.getInteger("highlightRed"));
	config.setHighlightGreen(compound.getInteger("highlightGreen"));
	config.setHighlightBlue(compound.getInteger("highlightBlue"));

	config.setTriangles(compound.getInteger("triangles"));
	config.setRadius(compound.getInteger("radius"));
	config.setItemRadius(compound.getInteger("itemRadius"));

	config.setUpdateItemRadiusAutomatically(compound.getBoolean("itemRadiusAuto"));
	config.setMuted(compound.getBoolean("muted"));
	config.setName(compound.getString("name"));
}

public void writeToNBT(NBTTagCompound compound)
{
	NBTTagList items = new NBTTagList();

	for(int i = 0; i < this.getSizeInventory(); i++)
	{
		// Don't write empty slots
		if (this.getStackInSlot(i) != null)
		{
			NBTTagCompound item = new NBTTagCompound();
			item.setInteger("Slot", i);

			this.getStackInSlot(i).writeToNBT(item);

			items.appendTag(item);
		}
	}
	compound.setTag("ItemBag", items);

	Config config = HotbarBag.instance.config; // Get config instance

	// Write item settings
	compound.setInteger("backgroundRed", config.getBackgroundRed());
	compound.setInteger("backgroundGreen", config.getBackgroundGreen());
	compound.setInteger("backgroundBlue", config.getBackgroundBlue());

	compound.setInteger("highlightRed", config.getHighlightRed());
	compound.setInteger("highlightGreen", config.getHighlightGreen());
	compound.setInteger("highlightBlue", config.getHighlightBlue());

	compound.setInteger("triangles", config.getTriangles());
	compound.setInteger("radius", config.getRadius());
	compound.setInteger("itemRadius", config.getItemRadius());

	compound.setBoolean("itemRadiusAuto", config.isUpdateItemRadiusAutomatic());
	compound.setBoolean("muted", config.isMuted());
	compound.setString("name", config.getName());
}
}

 

 

 

As always, all help is appreciated.

I have no idea what I'm doing.

Posted

You will probably have to force-put the modified ItemStack back in the players inventory. There is a lot that goes into making a fully functional ItemInventory. You can see a working example here. Feel free to use it or ask questions.

 

Sorry, I may not have been clear. The itemstacks are being saved to NBT absolutely fine. The issue lies with the values (such as the background colours etc.)

I have no idea what I'm doing.

Posted

A little standard debugging should help narrow down the issue:

If you print out the values of the config color values when you open up your GUI, what do you get?

If you print out the values of the NBT color values when loading the inventory, what do you get?

If you print out the values of the config color values when saving the inventory, what do you get?

 

I'm guessing that the values are fine, but Inventory NBT is probably only read on the server and so the client GUI has no idea what the values are. The Container class takes care of synchronizing the actual Inventory, but unless you override #detectAndSendChanges to also sync your color values, it does not send them.

 

Also, you will want to send a packet to the server from your GUI to change the config values - setting them on the client will result in them not getting saved.

Posted

Ok so after printing the values I've found that although they're all zero when the GUI opens, and all 0 when reading/writing the NBT. The GUI is currently a GuiScreen so doesn't have a container, so I guess I'll have to change that. How come if items can be saved to NBT without a packet, integers and other raw data types do?

 

Also something I didn't mention:

 

When the data is loaded I want to prioritise the NBT, and then if the the tags are missing use the defaults from the config file.

I have no idea what I'm doing.

Posted

Ok so after printing the values I've found that although they're all zero when the GUI opens, and all 0 when reading/writing the NBT. The GUI is currently a GuiScreen so doesn't have a container, so I guess I'll have to change that. How come if items can be saved to NBT without a packet, integers and other raw data types do?

Any GUI that involves manipulating ItemStacks typically has a Container to keep it in sync; the fact that you don't makes me wonder how anything works at all :P

 

Perhaps my inventory-storing-item tutorial will help you out.

 

When the data is loaded I want to prioritise the NBT, and then if the the tags are missing use the defaults from the config file.

To do that, simply set the values of your various fields to the Config values in the Inventory constructor - it is read from NBT after it is constructed, so any value in NBT will overwrite the default.

Posted

Any GUI that involves manipulating ItemStacks typically has a Container to keep it in sync; the fact that you don't makes me wonder how anything works at all :P

 

Ah! So I forgot to explain that there are 2 GUIs - one handles the inventory and has a container, the other is the settings menu (GuiConfig) and does not have a container. I followed your tutorial to get the inventory GUI working.

I have no idea what I'm doing.

Posted

Since these are Config settings that pertain only to the client's rendering of the inventory, you can actually get away with setting them only on the client side. Every client has their own config file that is separate from the server's, so pass the Config instance to your GUI and be sure to save it when the GUI closes so any changes the user makes are saved.

 

Along with that, the values are already saved in the Config file, so you don't need to mess around with them at all in the IInventory class - just get the values directly from the Config when you need them.

Posted

Ok I mostly get what you're saying and I think I'm already doing most of it, but I'm a little bit confused.

 

...be sure to save it when the GUI closes so any changes the user makes are saved.

 

Do you mean save the config or something else? Is it actually possible to update a config in-game, because if so I can scrap most of the NBT code (keeping the name, but removing it from the config).

 

Along with that, the values are already saved in the Config file, so you don't need to mess around with them at all in the IInventory class

 

I currently have 2 sets of values:

 

Set 1 is the config file, which is the initial defaults for every new bag.

Set 2 is the NBT, which is specific to that item.

 

Because items have their own specific values, I still need to read the NBT data before turning to the config.

I have no idea what I'm doing.

Posted

Yes, config files can be saved in-game.

 

Oh, you want the settings to be unique per item, not just globally applied... well, in that case, why even use the Config? Just use the ItemStack NBT, as that is automatically synced between server (where you set values) and client (where you can read them).

 

When you open your GUI (server-side), you can check the ItemStack's tag compound and set any values that are missing; then from the GUI, allow the user to change settings and when the close the GUI, send a packet to the server containing the new NBTTagCompound for the ItemStack and copy over your settings from that tag to the server-side tag.

 

Don't just set it directly - that would allow clients to potentially cheat and add their own enchantments and other things from your GUI.

Posted

Yes, config files can be saved in-game.

 

Oh, you want the settings to be unique per item, not just globally applied... well, in that case, why even use the Config? Just use the ItemStack NBT, as that is automatically synced between server (where you set values) and client (where you can read them).

 

When you open your GUI (server-side), you can check the ItemStack's tag compound and set any values that are missing; then from the GUI, allow the user to change settings and when the close the GUI, send a packet to the server containing the new NBTTagCompound for the ItemStack and copy over your settings from that tag to the server-side tag.

 

Don't just set it directly - that would allow clients to potentially cheat and add their own enchantments and other things from your GUI.

 

The idea of the config was so players could change the defaults if they had a personal preference they would always use. I might as well keep it, because if I'm going to append missing values they need to come from somewhere (just set some constants to them, and use the constants when filling in missing NBT values).

 

I think other than that I should be OK from here. In the likely scenario this is not the case I'll come back and beg for more help :P Thanks.

I have no idea what I'm doing.

Posted

The problem with a config, though, is that you are using the values on the SERVER to set the defaults in the ItemStack NBT, so the players themselves, unless they are playing single-player, won't actually be able to set their preference at all. Any changes they make to their own config will be completely ignored.

 

You could get around that by using the client-side config values as the defaults for the config GUI, and then overwriting those with any that appear in the ItemStack NBT. That should work fine. Also, you probably don't want changes in the GUI to be saved to the clients config file, since the settings are per-ItemStack, so I would only send the values back to the server to set the ItemStack NBT and not call config.save() at all.

Posted

The problem with a config, though, is that you are using the values on the SERVER to set the defaults in the ItemStack NBT, so the players themselves, unless they are playing single-player, won't actually be able to set their preference at all. Any changes they make to their own config will be completely ignored.

 

You could get around that by using the client-side config values as the defaults for the config GUI, and then overwriting those with any that appear in the ItemStack NBT. That should work fine. Also, you probably don't want changes in the GUI to be saved to the clients config file, since the settings are per-ItemStack, so I would only send the values back to the server to set the ItemStack NBT and not call config.save() at all.

 

Ok yeah. I'll bear this in mind and play with it a bit, but if I can't easily get it working how I want it I'll just scrap the config file.

I have no idea what I'm doing.

Posted

Right, this problem is still ongoing and I have literally no idea why it's happening.

 

So my understanding of NBT is very limited, and I've never successfully managed to get it to work, with the exception of the itemstacks. I've taken the settings tags and put them into their own methods so the actual inventory contents isn't unnecessarily read/written every time. When I print the compound, everything is there and correct, however it seems to be saved to some void location and deleted. I tried printing one of the values just before they're set, as you can see, and it came out correct (the same as the value on the slider).

 

Here's the NBT code:

 

public void writeSettingsToNBT(NBTTagCompound compound)
{
	Config config = HotbarBag.instance.config; // Get config instance

	// Write item settings
	HotbarBag.logger.info("Saving item settings");

	NBTTagList settings = new NBTTagList();

	NBTTagCompound background = new NBTTagCompound();
	NBTTagCompound highlight = new NBTTagCompound();
	NBTTagCompound circle = new NBTTagCompound();
	NBTTagCompound general = new NBTTagCompound();

	background.setInteger("backgroundRed", config.getBackgroundRed());
	background.setInteger("backgroundGreen", config.getBackgroundGreen());
	background.setInteger("backgroundBlue", config.getBackgroundBlue());

	highlight.setInteger("highlightRed", config.getHighlightRed());
	highlight.setInteger("highlightGreen", config.getHighlightGreen());
	highlight.setInteger("highlightBlue", config.getHighlightBlue());

	circle.setInteger("triangles", config.getTriangles());
	circle.setInteger("radius", config.getRadius());
	circle.setInteger("itemRadius", config.getItemRadius());

	general.setBoolean("itemRadiusAuto", config.isUpdateItemRadiusAutomatic());
	general.setBoolean("muted", config.isMuted());
	// general.setString("name", config.getName());
	general.setString("name", "JESUS CHRIST OUR LORD AND SAVIOUR");

	settings.appendTag(background);
	settings.appendTag(highlight);
	settings.appendTag(circle);
	settings.appendTag(general);

	compound.setTag("Settings", settings);
	HotbarBag.logger.info(settings);
}

public void writeSettingsToNBT(NBTTagCompound compound)
{
	Config config = HotbarBag.instance.config; // Get config instance

	// Write item settings
	HotbarBag.logger.info("Saving item settings");

	NBTTagList settings = new NBTTagList();

	NBTTagCompound background = new NBTTagCompound();
	NBTTagCompound highlight = new NBTTagCompound();
	NBTTagCompound circle = new NBTTagCompound();
	NBTTagCompound general = new NBTTagCompound();

	background.setInteger("backgroundRed", config.getBackgroundRed());
	background.setInteger("backgroundGreen", config.getBackgroundGreen());
	background.setInteger("backgroundBlue", config.getBackgroundBlue());

	highlight.setInteger("highlightRed", config.getHighlightRed());
	highlight.setInteger("highlightGreen", config.getHighlightGreen());
	highlight.setInteger("highlightBlue", config.getHighlightBlue());

	circle.setInteger("triangles", config.getTriangles());
	circle.setInteger("radius", config.getRadius());
	circle.setInteger("itemRadius", config.getItemRadius());

	general.setBoolean("itemRadiusAuto", config.isUpdateItemRadiusAutomatic());
	general.setBoolean("muted", config.isMuted());
	// general.setString("name", config.getName());
	general.setString("name", "Test");

	settings.appendTag(background);
	settings.appendTag(highlight);
	settings.appendTag(circle);
	settings.appendTag(general);

	compound.setTag("Settings", settings);
	HotbarBag.logger.info(settings);
}

 

 

I know it's a bit of a mess at the moment and I could probably make it 4x shorter using a loop, but I just want to get it working first :P

 

I'm fairly certain it's not a server/client sync problem because even when I've tried directly writing a string, nothing has happened. None of the compounds, or list exist.

 

Any idea why this is?

I have no idea what I'm doing.

Posted

Bump again.

 

I've tried combining both write methods, and now the settings are stored for as long as the game is running, then lost when it restarts.

I have no idea what I'm doing.

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.

×
×
  • Create New...

Important Information

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