Résolu Valeur dans le gui nulle
-
Bonjour, j’ai créé une machine et le tile entity contient une valeur timePassed qui s’incremente a chaque tick, j’ai mis un println qui print la valeur timePassed dans le tile et elle me retourne la bonne valeur mais quand dans la classe du gui je met un println de getField(3) qui me retourne timePassed dans le tileEntity mais dans le gui, la valeur retournée est toujours 0 et je ne comprend absolument pas pourquoi car j’ai plusieurs valeurs qui s’incremente dans le tile entity et qui me retourne la bonne valeur une fois récuperé dans la classe du gui. Si quelq’un peut m’aider je serais ravis, merci ^^
mon code:
TileEntity:
package fr.zunf1x.energy.machines.coalgenerator; import fr.zunf1x.energy.init.ModItems; import fr.zunf1x.energy.tileentity.TileMachine; import net.minecraft.block.BlockFurnace; import net.minecraft.client.gui.inventory.GuiFurnace; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.init.Items; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ItemStackHelper; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityLockable; import net.minecraft.util.EnumFacing; import net.minecraft.util.ITickable; import net.minecraft.util.NonNullList; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.oredict.OreDictionary; public class TileEntityCoalGenerator extends TileMachine implements ITickable { private NonNullList<ItemStack> stacks = NonNullList.withSize(3, ItemStack.EMPTY); private String customName; private int burningTimeLeft = 0; private int timePassed = 0; public TileEntityCoalGenerator() { super(0, 2147483647, 1000); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); this.stacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY); ItemStackHelper.loadAllItems(compound, this.stacks); if (compound.hasKey("CustomName", 8)) { this.customName = compound.getString("CustomName"); } this.burningTimeLeft = compound.getInteger("burningTimeLeft"); this.timePassed = compound.getInteger("timePassed"); this.energy = compound.getInteger("energy"); } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); ItemStackHelper.saveAllItems(compound, this.stacks); if (this.hasCustomName()) { compound.setString("CustomName", this.customName); } compound.setInteger("burningTimeLeft", this.burningTimeLeft); compound.setInteger("timePassed", this.timePassed); compound.setInteger("energy", this.energy); return compound; } @Override public boolean hasCustomName() { return this.customName != null && !this.customName.isEmpty(); } @Override public String getName() { return hasCustomName() ? this.customName : "tile.custom_furnace"; } public void setCustomName(String name) { this.customName = name; } @Override public int getField(int id) { switch (id) { case 0: return this.burningTimeLeft; case 1: return energy; case 2: return generationRate; case 3: return timePassed; } return 0; } @Override public void setField(int id, int value) { switch (id) { case 0: this.burningTimeLeft = value; break; case 1: this.energy = value; break; case 2: this.generationRate = value; break; case 3: this.timePassed = value; } } @Override public int getFieldCount() { return 4; } @Override public int getSizeInventory() { return this.stacks.size(); } @Override public ItemStack getStackInSlot(int index) { return this.stacks.get(index); } @Override public ItemStack decrStackSize(int index, int count) { return ItemStackHelper.getAndSplit(this.stacks, index, count); } @Override public ItemStack removeStackFromSlot(int index) { return ItemStackHelper.getAndRemove(stacks, index); } @Override public void setInventorySlotContents(int index, ItemStack stack) { this.stacks.set(index, stack); if (stack.getCount() > this.getInventoryStackLimit()) { stack.setCount(this.getInventoryStackLimit()); } } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isEmpty() { for (ItemStack stack : this.stacks) { if (!stack.isEmpty()) { return false; } } return true; } @Override public void clear() { for (int i = 0; i < this.stacks.size(); i++) { this.stacks.set(i, ItemStack.EMPTY); } } @Override public void openInventory(EntityPlayer player) { } @Override public void closeInventory(EntityPlayer player) { } @Override public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) { return null; } @Override public String getGuiID() { return null; } @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return true; } public boolean isUsableByPlayer(EntityPlayer player) { return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D; } @SideOnly(Side.CLIENT) public int getEnergyScaled() { return this.energy * 42 / this.maxEnergy; } @SideOnly(Side.CLIENT) public int getBatteryCreatingTimeScaled() { return this.timePassed * 24 / this.getFullBatteryCreatingTime(); } public int getFullBurnTime() { return 300; } public int getFullBatteryCreatingTime() { return 300; } public boolean isBurning() { return burningTimeLeft > 0; } public boolean canSmelt() { return !this.getStackInSlot(0).isEmpty() && energy < maxEnergy ? true : false; } public boolean canMakeBattery() { return this.getStackInSlot(1).getItem() == ModItems.raw_battery && !this.getStackInSlot(1).isEmpty() && this.getStackInSlot(2).getCount() < this.getInventoryStackLimit() && energy >= 5000; } @Override public void update() { if (energy >= maxEnergy) { energy = maxEnergy; } if (!this.world.isRemote) { if (isBurning()) { burningTimeLeft--; } if (!isBurning() && canSmelt()) { burningTimeLeft = getFullBurnTime(); this.decrStackSize(0, 1); } if (isBurning()) { energy += generationRate; } if (canMakeBattery()) { timePassed++; if (timePassed >= getFullBatteryCreatingTime()) { timePassed = 0; createBattery(); } } else { timePassed = 0; } } } public void createBattery() { ItemStack stack4 = this.getStackInSlot(2); energy -= 5000; this.decrStackSize(1, 1); if (stack4.isEmpty()) { this.setInventorySlotContents(2, new ItemStack(ModItems.battery)); } else { stack4.setCount(stack4.getCount() + 1); } } }
mon gui:
package fr.zunf1x.energy.machines.coalgenerator; import fr.zunf1x.energy.utils.Constants; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiFurnace; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.tileentity.TileEntityFurnace; import net.minecraft.util.ResourceLocation; public class GuiCoalGenerator extends GuiContainer { private static final ResourceLocation background = new ResourceLocation(Constants.MODID,"textures/gui/container/coal_generator.png"); private TileEntityCoalGenerator tile; public GuiCoalGenerator(InventoryPlayer inventory, TileEntityCoalGenerator tile) { super(new ContainerCoalGenerator(inventory, tile)); this.tile = tile; this.ySize = 180; } @Override public void initGui() { super.initGui(); } @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { int i = (this.width - this.xSize) / 2; int j = (this.height - this.ySize) / 2; this.drawDefaultBackground(); this.mc.getTextureManager().bindTexture(background); this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize); if (this.tile.isBurning()) { int burningTime = this.tile.getField(0); int textureHeight = (int) (13f / this.tile.getFullBurnTime() * burningTime); this.drawTexturedModalRect(i + 105, j + 45 + 12 - textureHeight, 190, 12 - textureHeight, 14, textureHeight + 1); } if (this.tile.canMakeBattery()) { int textureWidth = 0; this.drawTexturedModalRect(i + 28, j + 76, 190, 14, textureWidth, 17); } int energy = this.tile.getField(1); int energyHeight = (int) (42f / this.tile.maxEnergy * energy); this.drawTexturedModalRect(i + 153, j + 29 + 42 - energyHeight, 176, 42 - energyHeight, 14, energyHeight); this.drawTexturedModalRect(i + 181, j, 0, 180, 110, 50); } @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { int i = (this.width - this.xSize) / 2; int j = (this.height - this.ySize) / 2; renderHoveredToolTip(mouseX - i, mouseY - j); this.drawString(this.mc.fontRenderer, this.tile.getField(1) + "/" + this.tile.maxEnergy + "ZE", 187, 6, 0xffffff); this.drawString(this.mc.fontRenderer, "Rate: " + this.tile.generationRate + "ZE/t", 187, 16, 0xffffff); } }
-
Bonjour,
Ta variable n’est que incrémenté sur la partie serveur, sur la partie client elle vaut toujours 0. Il faut synchroniser les valeurs entre le client et le serveur.
-
@robin4002 merci de ta réponse, je me doutais un peu de ça mais j’attendais une confirmation, merci
-
-