Non résolu Afficher la variable de notre TileEntity dans un gui [1.7.10]
-
Bonjour, je souhaiterait afficher une variable d’une valeur de ma tileEntity dans un gui, mais avec la méthode que j’utilise actuellement, la valeur est gardée côté serveur lors de la déconnexion, mais côté client, la valeur affichée est remise à 0 lors de la connexion, que faire :
Voici mon code :
Class de la TileEntity :package fr.ciolgaming.themod.tileentities; import java.util.Random; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import fr.ciolgaming.themod.init.BlockMod; import fr.ciolgaming.themod.init.ItemMod; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; public class TileEntityFlowerGrinder extends TileEntity implements IInventory { private ItemStack[] contents = new ItemStack[128]; private int workingTime = 0; private int workingTimeNeeded = 200; private int minerais = 0; private int max = 7; @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); NBTTagList nbttaglist = new NBTTagList(); for (int i = 0; i < this.contents.length; ++i) { if (this.contents[i] != null) { NBTTagCompound nbttagcompound1 = new NBTTagCompound(); nbttagcompound1.setByte("Slot", (byte)i); this.contents[i].writeToNBT(nbttagcompound1); nbttaglist.appendTag(nbttagcompound1); } } compound.setTag("Items", nbttaglist); compound.setShort("workingTime",(short)this.workingTime); compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded); compound.setShort("minerais", (short)this.minerais); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); NBTTagList nbttaglist = compound.getTagList("Items", 10); this.contents = new ItemStack[this.getSizeInventory()]; for (int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); int j = nbttagcompound1.getByte("Slot") & 255; if (j >= 0 && j < this.contents.length) { this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1); } } this.workingTime = compound.getShort("workingTime"); this.workingTimeNeeded = compound.getShort("workingTimeNeeded"); this.minerais = compound.getShort("minerais"); } public int getSizeInventory() { return this.contents.length; } public ItemStack getStackInSlot(int slotIndex) { return this.contents[slotIndex]; } public ItemStack decrStackSize(int slotIndex, int amount) { if (this.contents[slotIndex] != null) { ItemStack itemstack; if (this.contents[slotIndex].stackSize <= amount) { itemstack = this.contents[slotIndex]; this.contents[slotIndex] = null; this.markDirty(); return itemstack; } else { itemstack = this.contents[slotIndex].splitStack(amount); if (this.contents[slotIndex].stackSize == 0) { this.contents[slotIndex] = null; } this.markDirty(); return itemstack; } } else { return null; } } public ItemStack getStackInSlotOnClosing(int slotIndex) { if (this.contents[slotIndex] != null) { ItemStack itemstack = this.contents[slotIndex]; this.contents[slotIndex] = null; return itemstack; } else { return null; } } public void setInventorySlotContents(int slotIndex, ItemStack stack) { this.contents[slotIndex] = stack; if (stack != null && stack.stackSize > this.getInventoryStackLimit()) { stack.stackSize = this.getInventoryStackLimit(); } this.markDirty(); } public String getInventoryName() { return " "; } public boolean hasCustomInventoryName() { return false; } public int getInventoryStackLimit() { return 64; } public boolean isUseableByPlayer(EntityPlayer player) { return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D; } public void openInventory() { } public void closeInventory() { } public boolean isItemValidForSlot(int slot, ItemStack stack) { return slot == 1 ? false : true; } public boolean isBurning() { return this.workingTime > 0; } private boolean canSmelt() { if (this.contents[0] == null) { return false; } else { if (new ItemStack(BlockMod.fleur).getItem() == this.contents[0].getItem() && this.contents[1] == null) { return true; } else { return false; } } } public void updateEntity() { if(this.isBurning() && this.canSmelt()) { workingTime++; } if(this.canSmelt() && !this.isBurning()) { this.workingTime = 1; } if(this.canSmelt() && this.workingTime == this.workingTimeNeeded) { this.smeltItem(); this.workingTime = 0; } if(!this.canSmelt()) { this.workingTime= 0; } } public void smeltItem() { if (this.canSmelt()) { ItemStack it = new ItemStack(Items.diamond, 1); minerais++; if (this.contents[1] == null) { if(minerais >= max) { this.contents[1] = it; minerais = 0; } } --this.contents[0].stackSize; if (this.contents[0].stackSize <= 0) { this.contents[0] = null; } } } public int getCookProgress() { return workingTime; } public int getMinerais() { return minerais; } }
Code du gui :
package fr.ciolgaming.themod.gui; import org.lwjgl.opengl.GL11; import fr.ciolgaming.themod.Reference; import fr.ciolgaming.themod.container.ContainerFlowerGrinder; import fr.ciolgaming.themod.tileentities.TileEntityFlowerGrinder; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.util.ResourceLocation; public class GuiFlowerGrinder extends GuiContainer { private static final ResourceLocation texture = new ResourceLocation(Reference.MODID ,"textures/gui/flower.png"); private TileEntityFlowerGrinder tileEntityMachine; private IInventory playerInv; public int valbar = 0; public GuiFlowerGrinder(TileEntityFlowerGrinder tile, InventoryPlayer inventory) { super(new ContainerFlowerGrinder(tile, inventory)); this.tileEntityMachine = tile; this.playerInv = inventory; this.allowUserInput = false; this.xSize = 176; this.ySize = 166; } GuiButton helpButton; @Override protected void drawGuiContainerBackgroundLayer(float partialRenderTick, int x, int y) { int guiX = (width - xSize) / 2; int guiY = (height - ySize) / 2; GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(texture); int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); this.drawTexturedModalRect(guiX + 82, guiY + 39, 176, 52 , 12, this.tileEntityMachine.getCookProgress() / 12); this.drawTexturedModalRect(guiX + 152, guiY + 75 - this.tileEntityMachine.getMinerais() * (58/7), 205, 0, 16, this.tileEntityMachine.getMinerais() * (58/7)); fontRendererObj.drawString("Flower Grinder :", guiX + 5, guiY + 4, 0xFFFFFF); fontRendererObj.drawString(this.tileEntityMachine.getMinerais() + "/7", guiX + 151, guiY + 6, 0xFFFFFF); } @Override protected void actionPerformed(GuiButton button) { } @Override public void updateScreen() { super.updateScreen(); if (!this.mc.thePlayer.isEntityAlive() || this.mc.thePlayer.isDead) { this.mc.thePlayer.closeScreen(); } } }
Merci de votre réponse,
Bonne soirée,
Ciolgaming
-
yo, tu dois utiliser les packets pour faire passer des donné de serveur => client et de client => serveur
il ya un tuto sur le forum, il est vieux mais c’est la meme chose -
Ok merci je vais voir ça
-
Salut,
Il y a déjà un paquet de mc pour le tite entity, il faut juste ajouter les informations à ce paquet.
J’ai montré comment faire dans le tutoriel sur les tile entity : https://www.minecraftforgefrance.fr/topic/995/ajouter-une-entité-à-votre-bloc-tile-entity -
Ok je me senseigne merci beaucoup