Jump to content

[1.7.2] Customizing ItemStacks with GUI


hugo_the_dwarf

Recommended Posts

Right now my luck finally ran out, I have a class, well a few. WeaponCustom and two extensions of it WeaponSlash(sword) and WeaponHack(Axe) and most of their values are NBT (so it is unique for each instance of it) well up until a few mins ago, The GUI would let you change the blades, guards, and handles and even color each piece (was about to move onto size and special upgrades) and it saved (no packets, just changing the NBT clientside with a GUI) but now it stopped. No biggy I will just make a packet to send from client to update server. Made it, registered it, and used it. Still no saving.

 

Idk why it worked without and Idk why it won't work at all now.

 

[spoiler=GUI]

public class GuiItemCustom extends GuiContainer {
public static final ResourceLocation	texture				= new ResourceLocation(
																	Rot.MODID
																			.toLowerCase(),
																	"textures/gui/base.png");

private int[] colors = new int[]{0xFFFFFF,0x999999,0x666666,0x333333, 
		0xD9D100,0x918b00,
		0xb36000,0x7C4300,
		0x619b00,0x4D6D17,
		0x47C0FF,0x21489C,
		0xe40004,0xaa0003};
private int[] selectedColors = new int[]{0,0,0};
private EntityPlayer					player;
private ItemStack						item;
private int								pad					= 4;
private int								ch					= 20;
private int								selectedBladeHead	= 0;
private int								selectedGuard		= 0;
private int								selectedHandle		= 0;
private int								weaponStyles		= 1;
private boolean swordStaff = false;


public GuiItemCustom(EntityPlayer player) {
	super(new ContainerNull());

	this.player = player;
	this.item = player.getHeldItem();
	this.xSize = 176;
	this.ySize = 166;
	this.selectedBladeHead = UtilityNBTHelper.getInt(this.item,
			UtilityWeaponNBTKeyNames.bladeHead);
	this.selectedGuard = UtilityNBTHelper.getInt(this.item,
			UtilityWeaponNBTKeyNames.guard);
	this.selectedHandle = UtilityNBTHelper.getInt(this.item,
			UtilityWeaponNBTKeyNames.handle);

	if (this.item.getItem() instanceof WeaponCustom)
	{
		weaponStyles = ((WeaponCustom) this.item.getItem()).getNumberOfTypes();
	}
	if ((this.item.getItem() instanceof WeaponSlash)
			|| (this.item.getItem() instanceof WeaponStaff)) {
		swordStaff = true;
	}

	for (int i = 0; i < colors.length;i++)
	{
		if (UtilityNBTHelper.getInt(item, UtilityWeaponNBTKeyNames.layerColor+0) == colors[i])
			selectedColors[2] = i;
		if (UtilityNBTHelper.getInt(item, UtilityWeaponNBTKeyNames.layerColor+2) == colors[i])
			selectedColors[0] = i;
		if (UtilityNBTHelper.getInt(item, UtilityWeaponNBTKeyNames.layerColor+4) == colors[i])
			selectedColors[1] = i;
	}
}

/** Button Clicks **/
@Override
protected void actionPerformed(GuiButton button) {
	switch (button.id) {
		case 0 : // Class Forward
			if (button.displayString.contains("Blade")
					|| button.displayString.contains("Head")) {
				if (this.selectedBladeHead < (this.weaponStyles - 1)) {
					this.selectedBladeHead++;
				} else {
					this.selectedBladeHead = 0;
				}
			} else if (button.displayString.contains("Guard")) {
				if (this.selectedGuard < (this.weaponStyles - 1)) {
					this.selectedGuard++;
				} else {
					this.selectedGuard = 0;
				}
			} else if (button.displayString.contains("Extra")) {
				UtilityNBTHelper.setBoolean(item, UtilityWeaponNBTKeyNames.showExtra, !UtilityNBTHelper.getBoolean(item, UtilityWeaponNBTKeyNames.showExtra));
			}else if (button.displayString.contains("Handle")) {
				if (this.selectedHandle < (this.weaponStyles - 1)) {
					this.selectedHandle++;
				} else {
					this.selectedHandle = 0;
				}
			}
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.bladeHead, this.selectedBladeHead);
			UtilityNBTHelper.setInteger(this.item, UtilityWeaponNBTKeyNames.handle,
					this.selectedHandle);
			UtilityNBTHelper.setInteger(this.item, UtilityWeaponNBTKeyNames.guard,
					this.selectedGuard);
			break;
		case 1 : // Class Back
			if (button.displayString.contains("Blade")
					|| button.displayString.contains("Head")) {
				if (this.selectedBladeHead > 0) {
					this.selectedBladeHead--;
				} else {
					this.selectedBladeHead = this.weaponStyles - 1;
				}
			} else if (button.displayString.contains("Guard")) {
				if (this.selectedGuard > 0) {
					this.selectedGuard--;
				} else {
					this.selectedGuard = this.weaponStyles - 1;
				}
			}
			else if (button.displayString.contains("Extra")) {
				UtilityNBTHelper.setBoolean(item, UtilityWeaponNBTKeyNames.showExtra, !UtilityNBTHelper.getBoolean(item, UtilityWeaponNBTKeyNames.showExtra));
			} else if (button.displayString.contains("Handle")) {
				if (this.selectedHandle > 0) {
					this.selectedHandle--;
				} else {
					this.selectedHandle = this.weaponStyles - 1;
				}
			}
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.bladeHead, this.selectedBladeHead);
			UtilityNBTHelper.setInteger(this.item, UtilityWeaponNBTKeyNames.handle,
					this.selectedHandle);
			UtilityNBTHelper.setInteger(this.item, UtilityWeaponNBTKeyNames.guard,
					this.selectedGuard);
			break;
		case 2 :
			if (button.displayString.contains("CB")) {
				if (selectedColors[0] < colors.length - 1)
					selectedColors[0]++;
				else
					selectedColors[0] = 0;
			}
			else if (button.displayString.contains("CG")) {
				if (selectedColors[1] < colors.length - 1)
					selectedColors[1]++;
				else
					selectedColors[1] = 0;
			}
			else if (button.displayString.contains("CH")) {
				if (selectedColors[2] < colors.length - 1)
					selectedColors[2]++;
				else
					selectedColors[2] = 0;
			}
			if (swordStaff)
			{
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.layerColor+0, colors[selectedColors[2]]);//handle
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.layerColor+2, colors[selectedColors[0]]);//blade
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.layerColor+4, colors[selectedColors[1]]);//guard
			}
			else
			{
				UtilityNBTHelper.setInteger(this.item,
						UtilityWeaponNBTKeyNames.layerColor+0, colors[selectedColors[2]]);//handle
				UtilityNBTHelper.setInteger(this.item,
						UtilityWeaponNBTKeyNames.layerColor+2, colors[selectedColors[0]]);//blade
				UtilityNBTHelper.setInteger(this.item,
						UtilityWeaponNBTKeyNames.layerColor+4, colors[selectedColors[0]]);//guard
			}
			break;
		case 3 :
			if (button.displayString.contains("CB")) {
				if (selectedColors[0] > 0)
					selectedColors[0]--;
				else
					selectedColors[0] = colors.length - 1;
			}
			else if (button.displayString.contains("CG")) {
				if (selectedColors[1] > 0)
					selectedColors[1]--;
				else
					selectedColors[1] = colors.length - 1;
			}
			else if (button.displayString.contains("CH")) {
				if (selectedColors[2] > 0)
					selectedColors[2]--;
				else
					selectedColors[2] = colors.length - 1;
			}
			if (swordStaff)
			{
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.layerColor+0, colors[selectedColors[2]]);//handle
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.layerColor+2, colors[selectedColors[0]]);//blade
			UtilityNBTHelper.setInteger(this.item,
					UtilityWeaponNBTKeyNames.layerColor+4, colors[selectedColors[1]]);//guard
			}
			else
			{
				UtilityNBTHelper.setInteger(this.item,
						UtilityWeaponNBTKeyNames.layerColor+0, colors[selectedColors[2]]);//handle
				UtilityNBTHelper.setInteger(this.item,
						UtilityWeaponNBTKeyNames.layerColor+2, colors[selectedColors[0]]);//blade
				UtilityNBTHelper.setInteger(this.item,
						UtilityWeaponNBTKeyNames.layerColor+4, colors[selectedColors[0]]);//guard
			}
			break;
		case 4 :
			Rot.net.sendToServer(new CustomItemPacket(item));
			break;
	}
}

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	GL11.glColor4f(1F, 1F, 1F, 1F);
	Minecraft.getMinecraft().renderEngine.bindTexture(texture);
	drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);

	TextureManager manager = Minecraft.getMinecraft().renderEngine;

	this.buttonList.clear();
	// Start with main control buttons

	int x1 = this.guiLeft + this.pad;
	int x2 = (this.guiLeft + this.xSize) - (50 + this.pad);

	this.buttonList.add(new GuiButton(0, x2, this.guiTop + this.pad, 50, this.ch,
			(swordStaff ? "Blade" : "Head") + " ->"));
	this.buttonList.add(new GuiButton(2, x2 - 20, this.guiTop + this.pad, 20, this.ch,
			"CB>")/*.packedFGColour = colors[selectedColors[0]]*/);
	this.drawCenteredString(this.fontRendererObj, "" + this.selectedBladeHead,
			(x1 + 50 + x2) / 2, this.guiTop + this.pad + 5, colors[selectedColors[0]]);
	this.buttonList.add(new GuiButton(1, x1, this.guiTop + this.pad, 50, this.ch,
			"<- " + (swordStaff ? "Blade" : "Head")));
	this.buttonList.add(new GuiButton(3, x1 + 50, this.guiTop + this.pad, 20, this.ch,
			"<CB")/*.packedFGColour = colors[selectedColors[0]]*/);

	if (swordStaff)
	{
		this.buttonList.add(new GuiButton(0, x2, this.guiTop + this.pad + 20, 50,
				this.ch, "Guard ->"));
		this.buttonList.add(new GuiButton(2, x2 - 20, this.guiTop + this.pad + 20, 20,
				this.ch, "CG>")/*.packedFGColour = colors[selectedColors[1]]*/);
		this.drawCenteredString(this.fontRendererObj, "" + this.selectedGuard,
				(x1 + 50 + x2) / 2, this.guiTop + this.pad + 5 + 20, colors[selectedColors[1]]);
		this.buttonList.add(new GuiButton(1, x1, this.guiTop + this.pad + 20, 50,
				this.ch, "<- Guard"));
		this.buttonList.add(new GuiButton(3, x1 + 50, this.guiTop + this.pad + 20, 20,
				this.ch, "<CG")/*.packedFGColour = colors[selectedColors[1]]*/);
	}
	else
	{
		this.buttonList.add(new GuiButton(0, x2, this.guiTop + this.pad + 20, 50,
				this.ch, "Extra ->"));
		this.drawCenteredString(this.fontRendererObj, "" + UtilityNBTHelper.getBoolean(item, UtilityWeaponNBTKeyNames.showExtra),
				(x1 + 50 + x2) / 2, this.guiTop + this.pad + 5 + 20, colors[selectedColors[0]]);
		this.buttonList.add(new GuiButton(1, x1, this.guiTop + this.pad + 20, 50,
				this.ch, "<- Extra"));
	}

	this.buttonList.add(new GuiButton(0, x2, this.guiTop + this.pad + (20 * 2),
			50, this.ch, "Handle ->"));
	this.buttonList.add(new GuiButton(2, x2 - 20, this.guiTop + this.pad + (20 * 2),
			20, this.ch, "CH>")/*.packedFGColour = colors[selectedColors[2]]*/);
	this.drawCenteredString(this.fontRendererObj, "" + this.selectedHandle,
			(x1 + 50 + x2) / 2, this.guiTop + this.pad + 5 + (20 * 2), colors[selectedColors[2]]);
	this.buttonList.add(new GuiButton(1, x1, this.guiTop + this.pad + (20 * 2),
			50, this.ch, "<- Handle"));
	this.buttonList.add(new GuiButton(3, x1 + 50, this.guiTop + this.pad + (20 * 2),
			20, this.ch, "<CH")/*.packedFGColour = */);

	this.buttonList.add(new GuiButton(4, x1, this.guiTop + this.pad
			+ (20 * 3), 50, this.ch, "Save"));

	manager.bindTexture(manager.getResourceLocation(1));
	// RENDER ITEMS
	IIcon displayItem;
	for (int l = 0; l < 3; l++) {
		displayItem = this.item.getItem().getIcon(this.item, l);
		if (displayItem != null) {
			drawTexturedModelRectFromIcon(x2, this.guiTop + this.pad
					+ (20 * 3), displayItem, 16, 16);
		}
	}
}

/** Keyboard Clicks **/
@Override
protected void keyTyped(char par1, int par2) {
	if ((par2 == 1) || (par2 == this.mc.gameSettings.keyBindInventory.getKeyCode())) {
		this.mc.thePlayer.closeScreen();
	}
}
}

 

 

[spoiler=WeaponCustom]

public class WeaponCustom extends ItemSword {

private int	numberOfLayers = 6;
private int numberOfTypes;

public WeaponCustom(ToolMaterial p_i45356_1_) {
	super(p_i45356_1_);
}

@Override
public void addInformation(ItemStack par1ItemStack,
		EntityPlayer par2EntityPlayer, List par3List, boolean par4) {
	super.addInformation(par1ItemStack, par2EntityPlayer, par3List, par4);
	String size = UtilityNBTHelper.getString(par1ItemStack,
			UtilityWeaponNBTKeyNames.size);
	String name = (String) par3List.get(0);
	if ((size != "normal") && (size != "")) {
		par3List.set(0, size.substring(0, 1).toUpperCase() + size.substring(1) + " " + name);
	}
}

public IIcon[] getIcons(ItemStack stack)
{
	return null;
}


public int[] getLayerColors(ItemStack is) {
	int[] colorList = new int[this.numberOfLayers];
	for (int c = 0; c < this.numberOfLayers; c++) {
		int color = UtilityNBTHelper.getInt(is, UtilityWeaponNBTKeyNames.layerColor
				+ c);
		colorList[c] = (color == 0 ? 0xFFFFFF : color);
	}
	return colorList;
}	

public int getNumberOfTypes()
{
	return numberOfTypes;
}

public void setNumberOfTypes(int num)
{
	numberOfTypes = num;
}
}

 

 

[spoiler=WeaponSlash (Hack is pretty much a direct copy, only a few different NBT values)]

public class WeaponSlash extends WeaponCustom {
private int	numOfTypes	= 8;
IIcon[]				blades		= new IIcon[numOfTypes];
IIcon[]				bladeEffectsFrost		= new IIcon[numOfTypes];
IIcon[]				bladeEffectsBleed		= new IIcon[numOfTypes];
IIcon[]				bladeEffectsVamp		= new IIcon[numOfTypes];
IIcon[]				guards		= new IIcon[numOfTypes];
IIcon[]				guardEffects0		= new IIcon[numOfTypes];
IIcon[]				handles		= new IIcon[numOfTypes];
IIcon[]				handleEffects0		= new IIcon[numOfTypes];
IIcon nullIcon;
IIcon				defaultIcon;

public WeaponSlash(ToolMaterial mat) {
	super(mat);
	setNumberOfTypes(numOfTypes);
}

@Override
public IIcon getIcon(ItemStack stack, int pass) {
	switch (pass) {
		case 0 :
			return this.handles[utilityNBTHelper.getInt(stack,
					UtilityWeaponNBTKeyNames.handle)];
		case 1 :
			return this.blades[utilityNBTHelper.getInt(stack,
					UtilityWeaponNBTKeyNames.bladeHead)];
		case 2 :
			return this.guards[utilityNBTHelper.getInt(stack,
					UtilityWeaponNBTKeyNames.guard)];
		default :
			break;
	}
	return this.defaultIcon;
}

@Override
public IIcon[] getIcons(ItemStack stack) {
	//0 is handle, 2 is blade, 4 is guard
	//1 he, 3 be, and 5 ge; are effects
	IIcon[] icons = new IIcon[6];

	icons[0] = this.handles[utilityNBTHelper.getInt(stack, UtilityWeaponNBTKeyNames.handle)];
	icons[2] = this.blades[utilityNBTHelper.getInt(stack, UtilityWeaponNBTKeyNames.bladeHead)];
	icons[4] = this.guards[utilityNBTHelper.getInt(stack, UtilityWeaponNBTKeyNames.guard)];

	switch(UtilityNBTHelper.getInt(stack, UtilityWeaponNBTKeyNames.bladeHeadEffect))
	{
	case 1:
		icons[3] = this.bladeEffectsFrost[utilityNBTHelper.getInt(stack,
				UtilityWeaponNBTKeyNames.bladeHead)];
		break;
	case 2:
		icons[3] = this.bladeEffectsBleed[utilityNBTHelper.getInt(stack,
				UtilityWeaponNBTKeyNames.bladeHead)];
		break;
	case 3:
		icons[3] = this.bladeEffectsVamp[utilityNBTHelper.getInt(stack,
				UtilityWeaponNBTKeyNames.bladeHead)];
		break;
	default:	
		icons[3] = nullIcon;
		break;
	}
	switch(UtilityNBTHelper.getInt(stack, UtilityWeaponNBTKeyNames.handleEffect))
	{
	case 1:
		icons[1] = this.handleEffects0[utilityNBTHelper.getInt(stack,
				UtilityWeaponNBTKeyNames.handle)];
		break;
	default:	
		icons[1] = nullIcon;
		break;
	}
	switch(UtilityNBTHelper.getInt(stack, UtilityWeaponNBTKeyNames.guardEffect))
	{
	case 1:
		icons[5] = this.guardEffects0[utilityNBTHelper.getInt(stack,
				UtilityWeaponNBTKeyNames.guard)];
		break;
	default:	
		icons[5] = nullIcon;
		break;
	}

	return icons;
}

@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item p_150895_1_, CreativeTabs p_150895_2_, List p_150895_3_) {
	ItemStack[] swords = new ItemStack[numOfTypes];
	for (int i = 0; i < numOfTypes; i++) {
		swords[i] = new ItemStack(p_150895_1_, 1, 0);
		UtilityNBTHelper.setString(swords[i], UtilityWeaponNBTKeyNames.type, "slash");
		UtilityNBTHelper
				.setString(swords[i], UtilityWeaponNBTKeyNames.size, "large");
		UtilityNBTHelper.setInteger(swords[i], UtilityWeaponNBTKeyNames.handle, i);
		UtilityNBTHelper.setInteger(swords[i], UtilityWeaponNBTKeyNames.bladeHead, i);
		UtilityNBTHelper.setInteger(swords[i], UtilityWeaponNBTKeyNames.guard, i);
		p_150895_3_.add(swords[i]);
	}
}

@Override
public void onCreated(ItemStack par1ItemStack, World par2World,
		EntityPlayer par3EntityPlayer) {
	UtilityNBTHelper.setString(par1ItemStack, UtilityWeaponNBTKeyNames.type, "slash");
	UtilityNBTHelper
			.setString(par1ItemStack, UtilityWeaponNBTKeyNames.size, "normal");
	UtilityNBTHelper.setInteger(par1ItemStack, UtilityWeaponNBTKeyNames.bladeHead, 3);
	UtilityNBTHelper.setInteger(par1ItemStack, UtilityWeaponNBTKeyNames.guard, 3);
	UtilityNBTHelper.setInteger(par1ItemStack, UtilityWeaponNBTKeyNames.handle, 3);
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister ir) {
	for (int i = 0; i < numOfTypes; i++) {
		this.blades[i] = ir.registerIcon(Rot.MODID + ":" + "weapons/blades/blade_"
				+ i);
		this.bladeEffectsFrost[i] = ir.registerIcon(Rot.MODID+":"+"weapons/blades/effects/blade_"
				+ i +"_e_0");
		this.bladeEffectsBleed[i] = ir.registerIcon(Rot.MODID+":"+"weapons/blades/effects/blade_"
				+ i +"_e_1");
		this.bladeEffectsVamp[i] = ir.registerIcon(Rot.MODID+":"+"weapons/blades/effects/blade_"
				+ i +"_e_2"); 
		this.guards[i] = ir.registerIcon(Rot.MODID + ":" + "weapons/guards/guard_"
				+ i);
		this.guardEffects0[i] = ir.registerIcon(Rot.MODID+":"+"weapons/guards/effects/guard_"
				+ i +"_e_0");
		this.handles[i] = ir.registerIcon(Rot.MODID + ":" + "weapons/handles/handle_"
				+ i);
		this.handleEffects0[i] = ir.registerIcon(Rot.MODID+":"+"weapons/handles/effects/handle_"
				+ i +"_e_0");
	}
	this.defaultIcon = ir
			.registerIcon(Rot.MODID + ":" + "weapons/fighter_slash_icon");
	this.nullIcon = ir.registerIcon(Rot.MODID + ":" + "weapons/32x32Null");
}

}

 

 

[spoiler=CustomItemPacket]

public class CustomItemPacket implements IMessage {

public static class CustomItemPacketHandler implements IMessageHandler<CustomItemPacket, IMessage> {

	@Override
	public IMessage onMessage(CustomItemPacket message, MessageContext ctx) {
		ItemStack equippedItem = ctx.getServerHandler().playerEntity.getCurrentEquippedItem();
		equippedItem = message.item;
		return null;
	}

}

public ItemStack item;

public CustomItemPacket() {

}

public CustomItemPacket(ItemStack item) {
	this.item = item;
}

@Override
public void fromBytes(ByteBuf buf) {
	this.item = ByteBufUtils.readItemStack(buf); // this class is very
													// useful in general for
													// writing more complex
													// objects
}

@Override
public void toBytes(ByteBuf buf) {
	ByteBufUtils.writeItemStack(buf, this.item);
}

}

 

 

EDIT: ok I had it save a customized sword *once* then it failed and stopped saving again.

Link to comment
Share on other sites

Ok it seems the itemstack will save if I make changes, and then open up the player inventory "E" anyway I can have it save from the GUI that alters it? I'd like to have that GUI add more effects that actually do more then look pretty, and don't want players to spend the resources on something that *might* not save if they don't check their inventory after.

 

So any help? Ideas?

 

EDIT:

seems only the creative mode inventory will save the item. survival mode doesn't.

Link to comment
Share on other sites

You need to use packets to tell the server to customize the ItemStack and then let the server modify the container including the ItenStacks.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Thanks that pointed me in the right direction, fussed around with different ways until it worked

 

@Override
	public IMessage onMessage(CustomItemPacket message, MessageContext ctx) {
		EntityPlayerMP player = ctx.getServerHandler().playerEntity;
		InventoryPlayer ip = player.inventory;
		ip.setInventorySlotContents(ip.currentItem, message.item);
		return null;
	}

 

I should probably do some checks to make sure that the item classes match up but I figure it's hard to change the item when the GUI only opens for WeaponCustom and you can't change items in the GUI.

 

Thanks

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.