Navigation

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    SOLVED Synchronisation ItemStack Server -> Client

    1.7.x
    1.7.10
    1
    2
    547
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Joelchasseu
      Joelchasseu last edited by

      Bonjour à tous,

      J’explique mon idée, l’idée c’est de faire de la nourriture qui, lorsqu’elle est craftée va recevoir une qualité donnée par un jet de dés.
      Pour cela j’ai crée un random qui va être intégré à l’itemStack en question grâce au NBTTagCompound. Seulement voilà je n’arrive pas à faire en sorte que le client puisse accéder au Compound pour afficher une String dans la description de l’item. Tout du moins pas sur le coup, la modification prend effet quand le serveur et/ou la partie solo s’arrête.

      Mes classes :

      public class ItemsFoodHeimnor extends ItemFood {
      
      public ItemsFoodHeimnor(int gigot, float saturation, boolean wolf) {
      super(gigot, saturation, wolf);
      }
      
      @Override
      public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) {
      
      stack.stackSize–;
      
      return stack;
      
      }
      
      @Override
      public void onCreated(ItemStack stack, World world, EntityPlayer player) {
      
      if (!world.isRemote) {
      
      int resultat = FichesFonc.throwOneComp(player, "Cuisine", FichesFonc.getCuisine(player), world) + 1;
      int i = 5;
      if (resultat == 1) {
      i = 0;
      } else if (resultat != 1 && resultat < 10) {
      i = 1;
      } else if (resultat >= 10 && resultat < 20) {
      i = 2;
      } else if (resultat >= 20) {
      i = 3;
      }
      System.out.println("Resultat dés : " + resultat);
      String[] tableau = { "Même un lainard n'en voudrait pas ...", "Ça se mange je suppose ...",
      "Ça ne devrait pas être mauvais !", "Parfait !" };
      
      if (!stack.hasTagCompound()) {
      stack.setTagCompound(new NBTTagCompound());
      }
      NBTTagCompound compound = stack.getTagCompound();
      
      System.out.println(compound);
      System.out.println(tableau*);
      String lore = tableau*;
      compound.setString("lore", lore);
      System.out.println(compound);
      stack.setTagCompound(compound);
      
      }
      
      }
      
      @SideOnly(Side.CLIENT)
      public void addInformation(ItemStack stack, EntityPlayer player, List lores, boolean bool) {
      
      if (stack.hasTagCompound()) {
      
      if (stack.getTagCompound().hasKey("lore")) {
      
      String loreStack = stack.getTagCompound().getString("lore");
      lores.add(EnumChatFormatting.GREEN + loreStack);
      }
      }
      
      super.addInformation(stack, player, lores, bool);
      }
      }
      ``` 
      
      ```java
      public static int getCuisine(EntityPlayer player) {
      
      if (NbtCsFile.getData(new File("Heimnor/Joueurs.dat")).getCompoundTag("index").getCompoundTag(player.getDisplayName())
      .hasKey("perso")) {
      
      String persoName = NbtCsFile.getData(new File("Heimnor/Joueurs.dat")).getCompoundTag("index")
      .getCompoundTag(player.getDisplayName()).getString("perso");
      int cuisine = NbtCsFile.getData(new File("Heimnor/Fiches.dat")).getCompoundTag("index").getCompoundTag(persoName)
      .getInteger("cuisine");
      System.out.println("Cuisine : " + cuisine);
      return cuisine;
      } else {
      return 0;
      }
      
      }
      
      public static int throwOneComp(EntityPlayer player, String comp1Name, int comp1, World world) {
      
      if (!world.isRemote) {
      Random rand = new Random();
      int jet = rand.nextInt(20);
      int resultat = jet + comp1;
      
      String formula = "(" + jet + "+" + comp1 + "=" + resultat + ")";
      ChatStyle style = new ChatStyle().setColor(EnumChatFormatting.GRAY);
      IChatComponent chatMsg = new ChatComponentText(
      "[" + comp1Name + "]" + formula + player.getDisplayName() + " obtient " + resultat + ".")
      .setChatStyle(style);
      player.addChatMessage(chatMsg);
      
      return resultat;
      }
      return 0;
      }
      

      Merci d’avance pour votre aide 😉

      1 Reply Last reply Reply Quote 0
      • Joelchasseu
        Joelchasseu last edited by

        Bon j’ai du bidouiller mais problème résolu je vous poste quand même le code si ça intéresse quelqu’un :

        public class SyncFood implements IMessage{
        
        ItemStack stack;
        int slot;
        
        public SyncFood() {}
        
        public SyncFood(ItemStack stack, int slot) {
        this.stack = stack;
        this.slot = slot;
        }
        
        @Override
        public void fromBytes(ByteBuf buf) {
        
        this.stack = ByteBufUtils.readItemStack(buf);
        this.slot = ByteBufUtils.readVarInt(buf, 5);
        }
        
        @Override
        public void toBytes(ByteBuf buf) {
        
        ByteBufUtils.writeItemStack(buf, this.stack);
        ByteBufUtils.writeVarInt(buf, this.slot, 5);
        
        }
        
        public static class Handler implements IMessageHandler<syncfood, imessage="">{
        
        @Override
        public IMessage onMessage(SyncFood message, MessageContext ctx) {
        
        Minecraft.getMinecraft().thePlayer.inventory.setInventorySlotContents(message.slot, message.stack);
        return null;
        }
        
        }
        }
        
        public class ItemsFoodHeimnor extends ItemFood {
        
        public ItemsFoodHeimnor(int gigot, float saturation, boolean wolf) {
        super(gigot, saturation, wolf);
        }
        
        @Override
        public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) {
        
        stack.stackSize–;
        
        return stack;
        }
        
        @Override
        public void onUpdate(ItemStack stack, World world, Entity player, int slotID, boolean p_77663_5_) {
        
        if (player instanceof EntityPlayer) {
        if (!stack.hasTagCompound()) {
        stack.setTagCompound(new NBTTagCompound());
        }
        if (!world.isRemote) {
        
        if (!stack.getTagCompound().hasKey("quality")) {
        int result = FichesFonc.throwOneComp((EntityPlayer) player, "Cuisine",
        FichesFonc.getCuisine((EntityPlayer) player), world);
        
        System.out.println("Jet de dés :" + result);
        int i = 5;
        if (result == 1) {
        i = 0;
        } else if (result > 1 && result <= 10) {
        i = 1;
        } else if (result > 10 && result < 20) {
        i = 2;
        } else if (result == 20) {
        i = 3;
        }
        String[] table = { "Même un lainard n'oserait pas y toucher ...", "Mouais ... ça se mange.",
        "Bien ! Ça ne doit pas être mauvais.", "Parfait !" };
        String stringNBT = table*;
        
        if (stack.hasTagCompound()) {
        stack.getTagCompound().setString("quality", stringNBT);
        System.out.println("stack server : " + stack);
        Heimnor.network.sendTo(new SyncFood(stack, slotID), (EntityPlayerMP) player);
        System.out.println(stack.getTagCompound().getString("quality"));
        }
        ((EntityPlayer) player).inventory.setInventorySlotContents(slotID, stack);
        }
        }
        }
        }
        
        }
        ```</syncfood,>
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Design by Woryk
        Contact / Mentions Légales / Faire un don

        MINECRAFT FORGE FRANCE © 2018

        Powered by NodeBB