Jump to content

[1.7.10] Something weird with buttons (GUI) and Enchantments


Recommended Posts

So now, after I've gone through networking, I found something that I don't quite understand:

I created a function to display buttons in a GUI for every enchantment available for the item in a specific slot; that's working pretty well, but for items with >5 possible enchantments, it only changes server-side until you pull it out or add an enchantment with one of the buttons with ID 0-4. (as one may have noticed, the client-server-thingy doesn't really like me :-\)



What to do now?


Here's the code:


GUI class:

public class GuiEnchantingTablePlus extends GuiContainer{

private ResourceLocation texture = new ResourceLocation(SimonsMod.MODID, "textures/gui/EnchantingTablePlus.png");

private int[] buttonListToolsID = new int[]{32, 33, 34, 35};
private int[] buttonListSwordID = new int[]{16, 17, 18, 19, 20, 21, 34};

private ItemStack tempItem;

private World worldObj;
private int posX;
private int posY;
private int posZ;

private TileEntityEnchantingTablePlus teetp;

private Item currentItem;
private Set possibleEnchantmentIDs;

public GuiEnchantingTablePlus(InventoryPlayer inventory, World world, int x, int y, int z) {
	super(new ContainerEnchantingTablePlus(inventory, world, x, y, z));

	this.worldObj = world;
	this.posX = x;
	this.posY = y;
	this.posZ = z;

	this.xSize = 176;
	this.ySize = 166;

protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

public boolean doesGuiPauseGame(){
	return false;

public void initGui(){

public void actionPerformed(GuiButton button){

	SimonsMod.simpleNW.sendToServer(new SimonsMessage(1, posX, posY, posZ, (Integer) possibleEnchantmentIDs.toArray()[button.id], 0));

	System.out.println("ID: " + button.id + " EnchID: " + (Integer) possibleEnchantmentIDs.toArray()[button.id]);
	//for(int i = 0; i < Enchantment.enchantmentsList.length; i++) System.out.println(i + ": " + Enchantment.enchantmentsList[i]);

public void updateScreen(){
	if(this.inventorySlots.getSlot(0) != null){
			currentItem = this.inventorySlots.getSlot(0).getStack().getItem();
			if(EnchantmentHelper.mapEnchantmentData(currentItem.getItemEnchantability(), new ItemStack(currentItem)).keySet() != null){
				possibleEnchantmentIDs = EnchantmentHelper.mapEnchantmentData(currentItem.getItemEnchantability(), new ItemStack(currentItem)).keySet();

				for(int i = 0; i < possibleEnchantmentIDs.size(); i++){
					if(Enchantment.enchantmentsList[(Integer) possibleEnchantmentIDs.toArray()[i]] != null)
						this.buttonList.add(new GuiButton(i, guiLeft + 20 + i*30, guiTop + 30, 30, 20, StatCollector.translateToLocal(Enchantment.enchantmentsList[(Integer) possibleEnchantmentIDs.toArray()[i]].getName())));



Message class:

public class SimonsMessage implements IMessage{

public int packetInt;
public int posX;
public int posY;
public int posZ;
public int enchID;
public int enchLv;

public SimonsMessage() {}

public SimonsMessage(int a, int x, int y, int z, int enchID, int enchLv) {
	this.packetInt = a;
	this.posX = x;
	this.posY = y;
	this.posZ = z;
	this.enchID = enchID;
	this.enchLv = enchLv;

public void fromBytes(ByteBuf buf) {
	this.packetInt = buf.readInt();
	this.posX = buf.readInt();
	this.posY = buf.readInt();
	this.posZ = buf.readInt();
	this.enchID = buf.readInt();
	this.enchLv = buf.readInt();

public void toBytes(ByteBuf buf) {



MessageHandler class:

public class SimonsMessageHandler implements IMessageHandler<SimonsMessage, IMessage>{

private boolean hasEnchantment = false;
private ItemStack tempItem;
private TileEntityEnchantingTablePlus teetp;
private World worldObj;

public IMessage onMessage(SimonsMessage message, MessageContext ctx) {
	worldObj = ctx.getServerHandler().playerEntity.worldObj;

		case 1:	teetp = (TileEntityEnchantingTablePlus)worldObj.getTileEntity(message.posX, message.posY, message.posZ);

				if(teetp.getStackInSlot(0) != null){						
					tempItem = teetp.getStackInSlot(0).copy();
						for(int i = 0; i < tempItem.getEnchantmentTagList().tagCount(); i++){
							if(message.enchID == tempItem.getEnchantmentTagList().getCompoundTagAt(i).getInteger("id")) hasEnchantment = true;

						tempItem.addEnchantment(Enchantment.enchantmentsList[message.enchID], 1);

						teetp.setInventorySlotContents(0, tempItem);
					hasEnchantment = false;



	return null;



P.S.: I'm just testing, so ignore the overcomplicated code...

P.P.S.: The cosmetic part is still TODO, right now I'm just trying to get the code to work  ::)

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.

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.