Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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 :-\)

J9N3gY5.png

 

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;
}

@Override
protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {
	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
}

public boolean doesGuiPauseGame(){
	return false;
}

public void initGui(){
	super.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(){
	super.updateScreen();
	this.buttonList.clear();
	if(this.inventorySlots.getSlot(0) != null){
		if(this.inventorySlots.getSlot(0).getHasStack()){
			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;
}

@Override
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();
}

@Override
public void toBytes(ByteBuf buf) {
	buf.writeInt(this.packetInt);
	buf.writeInt(this.posX);
	buf.writeInt(this.posY);
	buf.writeInt(this.posZ);
	buf.writeInt(this.enchID);
	buf.writeInt(this.enchLv);
}

}

 

MessageHandler class:

public class SimonsMessageHandler implements IMessageHandler<SimonsMessage, IMessage>{

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

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

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

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

					if(!hasEnchantment){
						tempItem.addEnchantment(Enchantment.enchantmentsList[message.enchID], 1);

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


				}
				break;

	}

	//System.out.println(message.packetInt);
	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  ::)

The buttons that you are saying don't work are outside of your GUI. 

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

The buttons that you are saying don't work are outside of your GUI.

 

I don't think that's the issue, because when printing out like the enchantment-ID, it's correct, so it basically sends and receives the right messages

I would try moving the buttons into the GUI, just to rule it out.

 

Sorry delpi, I should've tried that; It worked... unexpectedly

 

So yeah, I don't know what's up with that, but at least now it works.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.