I'm working on a new mod and I have a problem with one of the GUIs. Its purpose is to remove a certain amount of substance ("chemical") from one item ("jar") and add it to another item ("beaker"). The amount of substance is typed into the text field ("text") and the weighting is performed by the only button in the GUI (ID 1). After weighting, the beaker turns into item called ("Mixture"), which is simply a beaker with content, and amount of substance in the jar decreases accordingly. The problem is that while the GUI is working mostly right, when you remove either if the containers, it resets both containers to the previous state. For example, if I weight, let's say, 10 grams of sugar from a jar containing 100 grams of sugar into an empty beaker, it first turns the beaker into mixture containing 10 grams of sugat and turns the jar into jar containing 90 grams of sugar. But after I remove either the jar or the beaker from the GUI, the jar will have 100 grams of sugat in it and the beaker becomes empty again. Does someone know how to fix this? Thanks.
Code of the GUI:
package gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import tileentities.TileEntityScale;
public class GuiScale extends GuiContainer{
private static final ResourceLocation furnaceGuiTextures = new ResourceLocation("tm:textures/gui/GUIScale.png");
private TileEntityScale tileFurnace;
private GuiTextField text;
public GuiScale(InventoryPlayer invPlayer, TileEntityScale tile) {
super(new ContainerScale(invPlayer, tile));
this.tileFurnace = tile;
}
protected void drawGuiContainerForegroundLayer(int par1, int par2){
String string = this.tileFurnace.hasCustomInventoryName() ? this.tileFurnace.getInventoryName() : I18n.format(this.tileFurnace.getInventoryName(), new Object[0]);
this.fontRendererObj.drawString(string, this.xSize / 2 - this.fontRendererObj.getStringWidth(string)/2, 6, 4210752);
}
@Override
protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(furnaceGuiTextures);
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
}
public void initGui(){
super.initGui();
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
this.text = new GuiTextField(this.fontRendererObj, k+65, l+57, 45, 11);
text.setMaxStringLength(9);
text.setText("");
text.setTextColor(-1);
text.setDisabledTextColour(-1);
text.setEnableBackgroundDrawing(true);
this.text.setFocused(true);
this.buttonList.add(new GuiButton(1, k+74, l+69, 27, 10, "Weight"));
}
protected void keyTyped(char par1, int par2){
super.keyTyped(par1, par2);
this.text.textboxKeyTyped(par1, par2);
}
public void updateScreen(){
super.updateScreen();
this.text.updateCursorCounter();
}
public void drawScreen(int par1, int par2, float par3){
this.drawDefaultBackground();
this.text.drawTextBox();
super.drawScreen(par1, par2, par3);
}
protected void mouseClicked(int x, int y, int btn){
super.mouseClicked(x, y, btn);
this.text.mouseClicked(x, y, btn);
}
protected void actionPerformed (GuiButton gb){
tileFurnace.weight(text.getText());
}
}
And code of the method weight() from TileEntity class:
@SideOnly(Side.CLIENT)
public void weight(String amount){
if(slots[0] != null && slots[1] != null){
if(slots[1].getItem() instanceof ItemChemical && slots[0].getItem().equals(ItemsManager.itemBeaker)){
try{
double a = Double.parseDouble(amount);
Chemical c = ((ItemChemical)slots[1].getItem()).content;
NBTTagCompound tag = slots[1].getTagCompound();
double amountInJar = tag.getDouble("amount");
if(amountInJar >= a && a>0){
Mixture m = new Mixture();
m.add(c, a);
ItemStack itemstack = new ItemStack(ItemsManager.itemMixture, 1);
ItemMixture.setComposition(itemstack, m);
slots[0] = itemstack;
if(amountInJar > a){
tag.setDouble("amount", amountInJar-a);
}else{
slots[1] = new ItemStack(ItemsManager.itemJar, 1);
}
}
}catch(Exception e){
}
}
}
}
Make sure you're not using the client when dealing with server side things, like items.
Thanks for the advice, but it still doesn't fix the issue. To tell the truth, I have put that Client side annotation there just as an attempt to fix the issue.
Hello,
I'm working on a new mod and I have a problem with one of the GUIs. Its purpose is to remove a certain amount of substance ("chemical") from one item ("jar") and add it to another item ("beaker"). The amount of substance is typed into the text field ("text") and the weighting is performed by the only button in the GUI (ID 1). After weighting, the beaker turns into item called ("Mixture"), which is simply a beaker with content, and amount of substance in the jar decreases accordingly. The problem is that while the GUI is working mostly right, when you remove either if the containers, it resets both containers to the previous state. For example, if I weight, let's say, 10 grams of sugar from a jar containing 100 grams of sugar into an empty beaker, it first turns the beaker into mixture containing 10 grams of sugat and turns the jar into jar containing 90 grams of sugar. But after I remove either the jar or the beaker from the GUI, the jar will have 100 grams of sugat in it and the beaker becomes empty again. Does someone know how to fix this? Thanks.
Code of the GUI:
And code of the method weight() from TileEntity class:
Make sure you're not using the client when dealing with server side things, like items.
Thanks for the advice, but it still doesn't fix the issue. To tell the truth, I have put that Client side annotation there just as an attempt to fix the issue.