Résolu TileEntity ; données sauvé mais pas côté serveur
-
Bonjour, je reviens avec un nouveau problème que je ne comprend pas trop.
Donc je vais d’abord expliquer la démarche que je faite et je vous met ensuite le code commenté par mes soins en anglais du coup plus facile à comprendre car il y a beaucoup de choses xD
Du coup mon joueur A en posant un block seller va pouvoir mettre dans un slot ce qu’il veut vendre et la quantité qu’il veut partant de 1 à 64, le slot ne peut donc pas être de l’air, il a le choix entre plusieurs prix de ventes (5,10,20,50,100,200,500), une fois qu’il a tout mis (blocks,le prix de vente) il clique sur valider et HOP la machine est crée. Les données tels que le propriétaire,l’item,le nombre, le prix, l’argent récolté sont stocké dans le tile entity grâce aux NBT du côté client et serveur. Une fois crée la personne B qui viens acheter peut acheter grâce à sa carte de crédit, si sa carte est bien la sienne, qu’il a l’upgrade permettant d’acheter en sans-fil, si il a plus de fonds que le coût de l’item alors en appuyant sur BUY il reçoit l’item et sa carte diminue du coût dans la machine. Du côté du tile entity un calcul de ouf s’opère x) Donc côté client et server : dans le slot on retire 1 à chaque fois que la personne appuie sur buy et reçoit un item et l’amount diminue lui également de 1 jusqu’à arriver à 0. Une fois à 0, aucune personne ne peut plus acheter. Tout ça fonctionne NIQUEL. Sauf que quand je me déconnecte et reconnecte, bah server et client sont pas synchro, je me remtrouve avec plus rien comme données alors qu’elles sont stocké grâce à l’envoi des packets. Voici mes codes :
Mon block et la fonction on block activated qui prouve que les données sont enregistrée car même en déconnectant reconnectant j’arrive bien au bon Gui :
@Override public boolean onBlockActivated(World worldIn, BlockPos pos,IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { TileEntityBlockSeller te = (TileEntityBlockSeller)worldIn.getTileEntity(pos); if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH)) { if(te.getOwner() != null) { String checkONBT = te.getOwner(); String checkOBA = playerIn.getUniqueID().toString(); if(checkONBT.equals(checkOBA)) { if(te.getCreated() == false) { playerIn.openGui(ModEconomy.instance, GuiHandler.BLOCK_SELLER, worldIn, pos.getX(), pos.getY(), pos.getZ()); } else { playerIn.openGui(ModEconomy.instance, GuiHandler.BLOCK_SELLER_BUY, worldIn, pos.getX(), pos.getY(), pos.getZ()); } } else { playerIn.openGui(ModEconomy.instance, GuiHandler.BLOCK_SELLER_BUY, worldIn, pos.getX(), pos.getY(), pos.getZ()); } } else { System.out.println(te.getOwner()); } } return true; }
Le GUI de la personne qui met en vente (une fois validée il n’y a plus accès pour éviter la triche, càd reprendre les items dans le slot. C’est fait par une variable stockée dans le tile entity) :
package fr.fifou.economy.gui; import java.awt.Color; import java.io.IOException; 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.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.init.Blocks; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; import net.minecraftforge.fml.client.config.GuiButtonExt; import org.lwjgl.opengl.GL11; import fr.fifou.economy.ModEconomy; import fr.fifou.economy.blocks.tileentity.TileEntityBlockSeller; import fr.fifou.economy.blocks.tileentity.TileEntityBlockVault; import fr.fifou.economy.containers.ContainerSeller; import fr.fifou.economy.packets.PacketCardChange; import fr.fifou.economy.packets.PacketSellerCreated; public class GuiSeller extends GuiContainer { private TileEntityBlockSeller tile; public GuiSeller(InventoryPlayer playerInventory, TileEntityBlockSeller tile) { super(new ContainerSeller(playerInventory, tile)); this.tile = tile; } private static final ResourceLocation background = new ResourceLocation(ModEconomy.MODID ,"textures/gui/screen/gui_seller.png"); protected int xSize = 176; protected int ySize = 168; protected int guiLeft; protected int guiTop; private GuiButtonExt validate; private GuiButtonExt five; private GuiButtonExt ten; private GuiButtonExt twenty; private GuiButtonExt fifty; private GuiButtonExt hundreed; private GuiButtonExt twoHundreed; private GuiButtonExt fiveHundreed; private static int cost; @Override public void updateScreen() { super.updateScreen(); } @Override public void initGui() { super.initGui(); if(tile.getCreated() == false) { this.buttonList.add(this.validate = new GuiButtonExt(1, width / 2 + 46, height / 2 - 33, 35, 20, "Validate")); this.buttonList.add(this.five = new GuiButtonExt(2, width / 2 - 122, height / 2 - 66, 35, 20, "5")); this.buttonList.add(this.ten = new GuiButtonExt(3, width / 2- 122, height / 2 -47, 35, 20, "10")); this.buttonList.add(this.twenty = new GuiButtonExt(4, width / 2 - 122, height / 2 - 28, 35, 20, "20")); this.buttonList.add(this.fifty = new GuiButtonExt(5, width / 2 - 122, height / 2 -9, 35, 20, "50")); this.buttonList.add(this.hundreed = new GuiButtonExt(6, width / 2 - 122, height / 2 + 10, 35, 20, "100")); this.buttonList.add(this.twoHundreed = new GuiButtonExt(7, width / 2 - 122, height / 2 +29, 35, 20, "200")); this.buttonList.add(this.fiveHundreed = new GuiButtonExt(8, width / 2 - 122, height / 2 + 48, 35, 20, "500")); } } @Override protected void actionPerformed(GuiButton button) throws IOException { super.actionPerformed(button); World worldIn = mc.world; EntityPlayer playerIn = mc.player; if(tile != null) { if(button == this.five) { tile.setCost(5); this.cost = 5; } else if(button == this.ten) { tile.setCost(10); this.cost = 10; } else if(button == this.twenty) { tile.setCost(20); this.cost = 20; } else if(button == this.fifty) { tile.setCost(50); this.cost = 50; } else if(button == this.hundreed) { tile.setCost(100); this.cost = 100; } else if(button == this.twoHundreed) { tile.setCost(200); this.cost = 200; } else if(button == this.fiveHundreed) { tile.setCost(500); this.cost = 500; } else if(button == this.validate) { if(!(tile.getCost() == 0)) // IF TILE HAS NOT A COST OF 0 THEN WE PASS TO THE OTHER { if(!tile.getStackInSlot(0).equals(Blocks.AIR)) // IF SLOT 0 IS NOT BLOCKS.AIR, WE PASS { tile.setCreated(true); // CLIENT SET CREATED AT TRUE final int x = tile.getPos().getX(); // GET X final int y = tile.getPos().getY(); // GET Y final int z = tile.getPos().getZ(); // GET Z int amount = tile.getStackInSlot(0).getCount(); // GET COUNT IN TILE THANKS TO STACK IN SLOT String name = tile.getStackInSlot(0).getDisplayName(); // GET ITEM NAME IN TILE THANKS TO STACK IN SLOT tile.setAmount(amount); //CLIENT SET AMOUNT tile.setItem(name); // CLIENT SET ITEM NAME playerIn.closeScreen(); // CLOSE SCREEN ModEconomy.network.sendToServer(new PacketSellerCreated(true, this.cost, name, amount, x,y,z)); // SEND SERVER PACKET FOR CREATED, COST, NAME, AMOUNT, X,Y,Z ARE TILE COORDINATES } else { playerIn.sendMessage(new TextComponentString("You can't sell air buddy ;)")); } } else // IT MEANS THAT PLAYER HAS NOT SELECTED A COST { playerIn.sendMessage(new TextComponentString("You must set a cost for the items.")); } } } } @Override public boolean doesGuiPauseGame() { return false; } @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { fontRenderer.drawString(new TextComponentTranslation(I18n.format("title.block_seller")).getFormattedText(), 8, 5, Color.DARK_GRAY.getRGB()); fontRenderer.drawString(new TextComponentTranslation("Inventory").getFormattedText(), 8, 73, Color.DARK_GRAY.getRGB()); } @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { this.drawDefaultBackground(); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(background); int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); } }
Le gui de la personne qui achète :
package fr.fifou.economy.gui; import java.awt.Color; import java.io.IOException; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; import net.minecraftforge.fml.client.config.GuiButtonExt; import org.lwjgl.opengl.GL11; import fr.fifou.economy.ModEconomy; import fr.fifou.economy.blocks.tileentity.TileEntityBlockSeller; import fr.fifou.economy.containers.ContainerSeller; import fr.fifou.economy.items.ItemCreditcard; import fr.fifou.economy.packets.PacketCardChange; import fr.fifou.economy.packets.PacketCardChangeAdmin; import fr.fifou.economy.packets.PacketCardChangeSeller; import fr.fifou.economy.packets.PacketSellerCreated; import fr.fifou.economy.packets.PacketSellerFundsTotal; public class GuiSellerBuy extends GuiScreen { private TileEntityBlockSeller tile; public GuiSellerBuy(TileEntityBlockSeller tile) { this.tile = tile; } private static final ResourceLocation background = new ResourceLocation(ModEconomy.MODID ,"textures/gui/screen/gui_item.png"); protected int xSize = 256; protected int ySize = 124; protected int guiLeft; protected int guiTop; private GuiButtonExt slot1; private String item = ""; private int amount; @Override public void updateScreen() { super.updateScreen(); amount = tile.getAmount(); } @Override public void initGui() { this.guiLeft = (this.width - this.xSize) / 2; this.guiTop = (this.height - this.ySize) / 2; if(tile != null) { this.buttonList.add(this.slot1 = new GuiButtonExt(1, width / 2, height / 2 -80, 100, 20, "Buy")); } else { System.out.println("null"); } } @Override public boolean doesGuiPauseGame() { return false; } @Override protected void actionPerformed(GuiButton button) throws IOException { super.actionPerformed(button); World worldIn = mc.world; EntityPlayer player = mc.player; if(tile != null) // WE CHECK IF TILE IS NOT NULL FOR AVOID CRASH { System.out.println("–---------------------BEGIN------------------------"); tile.markDirty(); System.out.println(tile.getAmount()); System.out.println(tile.getOwner()); System.out.println(tile.getCost()); //DEBUG CLIENT & SERVER System.out.println(tile.getCreated()); System.out.println(tile.getItem()); System.out.println(tile.getFundsTotal()); System.out.println("-----------------------END------------------------"); if(button == this.slot1) //IF PLAYER BUY { for(int i = 0; i < player.inventory.getSizeInventory(); i++) // WE CHECK FOR THE CREDIT CARD IN PLAYER'S INVENTORY { if(player.inventory.getStackInSlot(i) != null) //IF SLOT "i" IS NOT NULL { if(player.inventory.getStackInSlot(i).getItem() instanceof ItemCreditcard) // AND THE ITEM IS THE CREDIT CARD { ItemStack hasCardIS = player.inventory.getStackInSlot(i); // WE CREATE A NEW ITEM STACK WITH THE CARD IN SLOT if(hasCardIS.hasTagCompound() && hasCardIS.getTagCompound().hasKey("Owner")) // CHECK IF CARD HAS OWNER { String playerCardO = hasCardIS.getTagCompound().getString("OwnerUUID"); // GET THE OWNER UUID IN CARD'S NBT String playerInWorld = player.getUniqueID().toString(); // GET THE PLAYER UUID IN WORLD if(playerCardO.equals(playerInWorld)) // IF THEY ARE THE SAME WE PASS { if(hasCardIS.getTagCompound().getInteger("Funds") >= tile.getCost()) //IF FUNDS IN CARD ARE SUPERIOR OR EQUAL TO THE COST WE PASS { if(hasCardIS.getTagCompound().getBoolean("Linked") == true) { if(tile.getAmount() >= 1) { tile.markDirty(); //UPDATE TILE ENTITY int fundTotal = tile.getFundsTotal(); // WE GET THE TOTAL FUNDS tile.setFundsTotal(fundTotal + tile.getCost()); // CLIENT ADD TOTAL FUNDS + THE COST OF THE ITEM final int x = tile.getPos().getX(); // GET X COORDINATES final int y = tile.getPos().getY(); // GET Y COORDINATES final int z = tile.getPos().getZ(); // GET Z COORDINATES final int cost = tile.getCost(); // GET COST OF THE TILE ENTITY int amount = tile.getAmount(); // GET AMOUNT OF THE TILE ENTITY tile.setAmount(amount -1); // CLIENT SET AMOUNT MINUS ONE EACH TIME HE BUY ModEconomy.network.sendToServer(new PacketSellerFundsTotal((fundTotal + tile.getCost()), x,y,z, amount)); //SENDING PACKET TO LET SERVER KNOW CHANGES WITH TOTAL FUNDS, COORDINATES AND AMOUNT ModEconomy.network.sendToServer(new PacketCardChangeSeller(cost)); // SENDING ANOTHER PACKET TO UPDATE CLIENT'S CARD IN SERVER KNOWLEDGE tile.markDirty(); } } } else // ELSE WE SEND THEM A MESSAGE TO TELL THEY DON'T HAVE ENOUGH FUNDS { player.sendMessage(new TextComponentString("You don't have enough funds.")); } } else // ELSE WE SEND HIM A MESSAGE TO TELL HIM IT'S NOT HIS CARD { System.out.println("not same owner"); } } else { } } } } } } } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { this.drawDefaultBackground(); // added this.mc.getTextureManager().bindTexture(background); int i = this.guiLeft; int j = this.guiTop; this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize); this.fontRenderer.drawString(TextFormatting.BOLD + I18n.format("title.ownerCard")+ ": " + String.valueOf(amount), (this.width / 2) - 75, (this.height / 2)- 55, 0x000); super.drawScreen(mouseX, mouseY, partialTicks); } }
Mes différents packets :
package fr.fifou.economy.packets; import java.util.UUID; import fr.fifou.economy.items.ItemCreditcard; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.ByteBufUtils; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class PacketCardChangeSeller implements IMessage { private static int cost; public PacketCardChangeSeller() { } public PacketCardChangeSeller(int cost) { this.cost = cost; } public void fromBytes(ByteBuf buf) { this.cost = buf.readInt(); } public void toBytes(ByteBuf buf) { buf.writeInt(this.cost); } public static class Handler implements IMessageHandler <packetcardchangeseller, imessage="">{ public IMessage onMessage(PacketCardChangeSeller message, MessageContext ctx) { EntityPlayer player = ctx.getServerHandler().player; // GET PLAYER World worldIn = player.world; // GET WORLD for(int i = 0; i < player.inventory.getSizeInventory(); i++) // CHECK IN PLAYER'S INVENTORY { if(player.inventory.getStackInSlot(i) != null) // IF SLOT "i" IS NOT NULL { if(player.inventory.getStackInSlot(i).getItem() instanceof ItemCreditcard) // IF SLOT "i" IS ITEM CREDIT CARD { ItemStack hasCardIS = player.inventory.getStackInSlot(i); // NEW ITEM STACK FROM "i" SLOT int fundsPrev = hasCardIS.getTagCompound().getInteger("Funds"); // GET PERVIOUS FUNDS int costBuy = message.cost; // GET COST BY WHAT WE SENT hasCardIS.getTagCompound().setInteger("Funds",(fundsPrev - costBuy)); //SERVER SET FUNDS OF CARD BY FUNDSPREV - TILE ENTITY COST } } } return null; } } }
package fr.fifou.economy.packets; import fr.fifou.economy.blocks.tileentity.TileEntityBlockSeller; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.ByteBufUtils; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class PacketSellerCreated implements IMessage { private static boolean created; private int cost; private int x; private int y; private int z; private String name = ""; private int amount = 0; public PacketSellerCreated() { } public PacketSellerCreated(boolean createdS,int costS, String nameS, int amountS, int xS, int yS, int zS) { this.created = createdS; // WE TAKE BACK THE INFOS FROM GuiSeller this.x = xS; this.y = yS; this.z = zS; this.name = nameS; this.amount = amountS; this.cost = costS; } public void fromBytes(ByteBuf buf) { this.created = buf.readBoolean(); this.cost = buf.readInt(); this.x = buf.readInt(); this.y = buf.readInt(); this.z = buf.readInt(); this.name = ByteBufUtils.readUTF8String(buf); this.amount = buf.readInt(); } public void toBytes(ByteBuf buf) { buf.writeBoolean(this.created); buf.writeInt(this.cost); buf.writeInt(this.x); buf.writeInt(this.y); buf.writeInt(this.z); ByteBufUtils.writeUTF8String(buf, this.name); buf.writeInt(this.amount); } public static class Handler implements IMessageHandler <packetsellercreated, imessage="">{ public IMessage onMessage(PacketSellerCreated message, MessageContext ctx) { EntityPlayer player = ctx.getServerHandler().player; World world = player.world; BlockPos pos = new BlockPos(message.x, message.y, message.z); TileEntityBlockSeller te = (TileEntityBlockSeller)world.getTileEntity(pos); //WE TAKE THE POSITION OF THE TILE ENTITY TO ADD INFO if(te != null) // CHECK IF PLAYER HAS NOT DESTROYED TILE ENTITY IN THE SHORT TIME OF SENDING PACKET { te.setCreated(message.created); // SERVER ADD CREATED TO TILE ENTITY te.setCost(message.cost); // SERVER ADD COST TO TILE ENTITY te.setItem(message.name); // SERVER ADD NAME TO TILE ENTITY te.setAmount(message.amount); // SERVER ADD AMOUNT TO TILE ENTITY } else { System.out.println("null tile entity"); } return null; } } }
package fr.fifou.economy.packets; import fr.fifou.economy.blocks.tileentity.TileEntityBlockSeller; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class PacketSellerFundsTotal implements IMessage { private int fundstotal; private int x; private int y; private int z; private int amount; public PacketSellerFundsTotal() { } public PacketSellerFundsTotal(int fundstotalS, int xS, int yS, int zS, int amountS) { this.fundstotal = fundstotalS; this.x = xS; this.y = yS; this.z = zS; this.amount = amountS; } public void fromBytes(ByteBuf buf) { this.fundstotal = buf.readInt(); this.x = buf.readInt(); this.y = buf.readInt(); this.z = buf.readInt(); this.amount = buf.readInt(); } public void toBytes(ByteBuf buf) { buf.writeInt(this.fundstotal); buf.writeInt(this.x); buf.writeInt(this.y); buf.writeInt(this.z); buf.writeInt(this.amount); } public static class Handler implements IMessageHandler <packetsellerfundstotal, imessage="">{ public IMessage onMessage(PacketSellerFundsTotal message, MessageContext ctx) { EntityPlayer player = ctx.getServerHandler().player; // GET PLAYER World world = player.world; // GET WORLD FROM PLAYER BlockPos pos = new BlockPos(message.x, message.y, message.z); // NEW BLOCK POS FOR TILE ENTITY COORDINATES TileEntityBlockSeller te = (TileEntityBlockSeller)world.getTileEntity(pos); // GET THE TILE ENTITY IN WORLD THANKS TO COORDINATES if(te != null) // IF TILE ENTITY EXIST { if(!te.getStackInSlot(0).isEmpty()) // IF THE SLOT IS NOT EMPTY { te.setFundsTotal(message.fundstotal); // SERVER SET THE FUNDS TOTAL FROM WHAT WE SENT te.getStackInSlot(0); player.addItemStackToInventory(te.getStackInSlot(0).splitStack(1)); // SERVER DIMINUE STACK IN SLOT BY 1 int messAmount = message.amount; // INT THE AMOUNT OF STACK IN SLOT int newAmount = messAmount - 1; // INT THE NEW AMOUNT OF STACK IN SLOT te.setAmount(newAmount); // SERVER SET THE NEW AMOUNT OF STACK IN SLOT } } return null; } } }
Voilà j’espère qu’avec l’explication et les commentaires vous aurez compris sinon je suis dispo grâce au discord de MFF
Merci à ceux qui m’aideront ^^
EDIT : Voici le débug que j’ai client et serveur du coup :
[18:03:58] [main/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:95]: –---------------------BEGIN------------------------ 64 [18:03:58] [main/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:98]: 1180fda0-fbd6-4eaa-a861-61fd3910f6fc 5 true [18:03:58] [main/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:101]: Gear 0 [18:03:58] [main/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:103]: –---------------------END------------------------ [18:03:58] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:95]: –---------------------BEGIN------------------------ 64 [18:03:58] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:98]: 1180fda0-fbd6-4eaa-a861-61fd3910f6fc 5 true [18:03:58] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:101]: Gear 0 [18:03:58] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:103]: –---------------------END------------------------
Et si je restart le jeu :
[18:04:35] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:95]: –---------------------BEGIN------------------------ 64 [18:04:35] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:98]: 1180fda0-fbd6-4eaa-a861-61fd3910f6fc 5 true [18:04:35] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:101]: Gear 0 [18:04:35] [Server thread/INFO]: [fr.fifou.economy.blocks.BlockSeller:onBlockActivated:103]: –---------------------END------------------------
Le server à toujours les infos, donc je ne comprend pas pourquoi ca reviens à 0 …
Je vous ajoutes une vidéo car je suis gentil et que c’est ptetre compliquer à comprendre avec un texte ^^
https://www.youtube.com/watch?v=92ZkvkYSPFk&feature=youtu.be</packetsellerfundstotal,></packetsellercreated,></packetcardchangeseller,>
-
Bonsoir,
Je pense qu’avoir la classe de ta TE et de ton GuiHandler nous aiderait.
Par ailleurs, je ne comprends pas pourquoi tu te sers de packet, j’étais convaincu qu’il en existait déjà des préconçus pour la synchro de n’importelle TE. -
Pas de soucis
package fr.fifou.economy.blocks.tileentity; import javax.annotation.Nullable; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.ItemStackHandler; public class TileEntityBlockSeller extends TileEntity { ItemStackHandler inventory_seller = new ItemStackHandler(1); //STACK HANDLER FOR ONE SLOT = 0 private String owner = ""; private int funds_total; private int cost; private boolean created; private int amount; private String item = ""; public TileEntityBlockSeller() { } public TileEntityBlockSeller(String ownerData, int costS, int amountS, String itemS ,int fundsTotalS, boolean createdS) { this.owner = ownerData; //STORE OWNER this.cost = costS; //STORE COST this.amount = amountS; //STORE AMOUNT this.item = itemS; //STORE ITEM NAME this.funds_total = fundsTotalS; //STORE FUNDS TOTAL this.created = createdS; //STORE CREATED BOOLEAN } public ItemStack getStackInSlot(int slot) { return inventory_seller.getStackInSlot(slot); } public ItemStack removeStackInSlot(int slot) { return inventory_seller.getStackInSlot(slot).splitStack(1); } public void setOwner(String string) { this.owner = string; } public String getOwner() { return this.owner; } public void setCost(int costS) { this.cost = costS; } public int getCost() { return this.cost; } public void setFundsTotal(int fundsS) { this.funds_total = fundsS; } public int getFundsTotal() { return this.funds_total; } public void setCreated(boolean createdS) { this.created = createdS; } public boolean getCreated() { return this.created; } public void setItem(String itemS) { this.item = itemS; } public String getItem() { return this.item; } public void setAmount(int amountS) { this.amount = amountS; } public int getAmount() { return this.amount; } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { compound.setTag("inventory", inventory_seller.serializeNBT()); compound.setString("ownerS", this.owner); compound.setInteger("cost", this.cost); compound.setInteger("amount", this.amount); compound.setString("item", this.item); compound.setInteger("funds_total", this.funds_total); compound.setBoolean("created", this.created); return super.writeToNBT(compound); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); inventory_seller.deserializeNBT(compound.getCompoundTag("inventory")); this.owner = compound.getString("ownerS"); this.cost = compound.getInteger("cost"); this.amount = compound.getInteger("amount"); this.item = compound.getString("item"); this.funds_total = compound.getInteger("funds_total"); this.created = compound.getBoolean("created"); } @Override public boolean hasCapability(Capability capability,@Nullable EnumFacing facing) { return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing); } @Override public <t>T getCapability(Capability <t>capability, @Nullable EnumFacing facing) { return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)inventory_seller : super.getCapability(capability, facing); } }
package fr.fifou.economy.gui; import javax.annotation.Nullable; import org.lwjgl.opengl.GL11; import fr.fifou.economy.ModEconomy; import fr.fifou.economy.blocks.tileentity.TileEntityBlockSeller; import fr.fifou.economy.blocks.tileentity.TileEntityBlockVault; import fr.fifou.economy.containers.ContainerSeller; import fr.fifou.economy.containers.ContainerVault; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.IGuiHandler; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class GuiHandler implements IGuiHandler { public static final int ITEM_CARD_GUI = 0; public static final int ITEM_CARD_DELETE = 1; public static final int BLOCK_VAULT_NEW = 2; public static final int BLOCK_SELLER = 3; public static final int BLOCK_SELLER_BUY = 4; public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { BlockPos pos = new BlockPos(x,y,z); TileEntity te = world.getTileEntity(pos); if(ID == BLOCK_VAULT_NEW) { return new ContainerVault(player.inventory, (TileEntityBlockVault)te); } else if(ID == BLOCK_SELLER) { return new ContainerSeller(player.inventory, (TileEntityBlockSeller)te); } return null; } public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { BlockPos pos = new BlockPos(x,y,z); TileEntity te = world.getTileEntity(pos); if(ID == ITEM_CARD_GUI) { return new GuiItem(); } else if(ID == ITEM_CARD_DELETE) { return new GuiDelete(); } else if(ID == BLOCK_VAULT_NEW) { return new GuiVaultNew(player.inventory, (TileEntityBlockVault)te); } else if(ID == BLOCK_SELLER) { return new GuiSeller(player.inventory, (TileEntityBlockSeller)te); } else if(ID == BLOCK_SELLER_BUY) { return new GuiSellerBuy((TileEntityBlockSeller)te); } return null; } }
Ah je ne sais pas, je n’en ai pas vu dans mes recherches, mais que ce soit les miens ou ceux là ici il marche ^^
Merci de ton aide, tu n’as pas été repoussé par la longueur du thread ^^</t></t>
-
Salut
Pour la synchro des tileEntity en 1.11 et +, il te suffis de modifier les fonctions getUpdateTag et handleUpdateTag de ton tileEntity de la même manière que writeToNBT et readFromNBT. -
J’ai pas compris à quoi ils servaient …
-
Non, si je me souviens bien, quand tu modifie les tags de ton TileEntity, il faut appeler this.markDirty(); et pour que toutes les informations soit transmises, il fait réécrire les fonctions getUpdateTag et handleUpdateTag. Tu n’as pas besoin d’appeler ces deux fonctions, minecraft va le faire tout seul.
EDIT : je te donne les liens vers deux topics de minecraftforge.net (en Anglais), c’est en 1.10, mais ça peux peut-être t’aider.
http://www.minecraftforge.net/forum/topic/40530-1102-solved-saving-tileentity-data/
http://www.minecraftforge.net/forum/topic/41122-110solved-tile-entity-synchronization/ -
@‘LeBossMax2’:
Non, si je me souviens bien, quand tu modifie les tags de ton TileEntity, il faut appeler this.markDirty(); et pour que toutes les informations soit transmises, il fait réécrire les fonctions getUpdateTag et handleUpdateTag. Tu n’as pas besoin d’appeler ces deux fonctions, minecraft va le faire tout seul.
Je vais voir avec ce que tu m’as dis
Du coup j’ai ajouter des tile.markDirty(); à chaque fois que je modifie des données du tile entity, et j’ai rajouter du coup ça :
@Override public NBTTagCompound getUpdateTag() { return super.getUpdateTag(); } @Override public void handleUpdateTag(NBTTagCompound compound) { super.handleUpdateTag(compound); }
Mais dans les fonctions je dois mettre quoi genre pour le getUpdateTag un this.variable = variable ?
EDIT : Désolé j’ai mal regarder ::( Ma faute du coup j’ai rajouter
@Nullable public SPacketUpdateTileEntity getUpdatePacket() { return new SPacketUpdateTileEntity(this.pos, 1, this.getUpdateTag()); } public NBTTagCompound getUpdateTag() { return this.writeToNBT(new NBTTagCompound()); }
Et ca semble marcher