MFF

    Minecraft Forge France
    • Récent
    • Mots-clés
    • Populaire
    • Utilisateurs
    • Groupes
    • Forge Events
      • Automatique
      • Foncé
      • Clair
    • S'inscrire
    • Se connecter

    NBT Tags, ces trucs que je ne comprends pas.

    Planifier Épinglé Verrouillé Déplacé Sans suite
    1.11.x
    17 Messages 3 Publieurs 3.7k Vues 1 Watching
    Charger plus de messages
    • Du plus ancien au plus récent
    • Du plus récent au plus ancien
    • Les plus votés
    Répondre
    • Répondre à l'aide d'un nouveau sujet
    Se connecter pour répondre
    Ce sujet a été supprimé. Seuls les utilisateurs avec les droits d'administration peuvent le voir.
    • FlowF Hors-ligne
      Flow
      dernière édition par

      @robin4002:

      Salut,
      Le problème vient de cette variable :
      public boolean owned = false;
      Ça valeur est partagé entre tous les itemCreditcard du monde comme l’instance itemCreditcard est la même pour tous ces items.
      Tu ne dois donc pas avoir de variable de classe ici, il faut uniquement des variables locales dans les fonctions, et tu dois toujours dépendre de tagnbt pour get ou set les valeurs (le tabnbt étant lié à l’itemstack qui lui a une instance par itemstack dans le monde, donc pas de partage des valeurs).

      package fr.fifou.economy.items;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import org.fusesource.jansi.Ansi.Color;
      
      import com.mojang.realmsclient.gui.ChatFormatting;
      
      import net.java.games.input.Keyboard;
      import net.minecraft.block.Block;
      import net.minecraft.block.state.IBlockState;
      import net.minecraft.client.Minecraft;
      import net.minecraft.client.audio.ISound;
      import net.minecraft.client.audio.PositionedSoundRecord;
      import net.minecraft.creativetab.CreativeTabs;
      import net.minecraft.enchantment.Enchantment;
      import net.minecraft.entity.player.EntityPlayer;
      import net.minecraft.init.Items;
      import net.minecraft.init.SoundEvents;
      import net.minecraft.item.Item;
      import net.minecraft.item.ItemStack;
      import net.minecraft.nbt.NBTTagCompound;
      import net.minecraft.nbt.NBTTagList;
      import net.minecraft.util.ActionResult;
      import net.minecraft.util.EnumActionResult;
      import net.minecraft.util.EnumHand;
      import net.minecraft.util.ResourceLocation;
      import net.minecraft.util.SoundCategory;
      import net.minecraft.util.math.BlockPos;
      import net.minecraft.util.math.RayTraceResult;
      import net.minecraft.util.text.ITextComponent;
      import net.minecraft.util.text.Style;
      import net.minecraft.util.text.TextComponentString;
      import net.minecraft.world.World;
      import net.minecraftforge.event.entity.player.PlayerInteractEvent;
      import net.minecraftforge.event.world.BlockEvent;
      import net.minecraftforge.fml.relauncher.Side;
      import net.minecraftforge.fml.relauncher.SideOnly;
      
      public class itemCreditcard extends Item
      {
      
      public itemCreditcard()
      {
      this.maxStackSize = 1;
      this.setCreativeTab(CreativeTabs.MATERIALS);
      this.setUnlocalizedName("item_creditcard");
      this.setRegistryName("item_creditcard");
      this.setDamage(new ItemStack(ItemsRegistery.itemCreditcard), 6);
      
      }
      
      @Override
      public ActionResult <itemstack>onItemRightClick(World world, EntityPlayer player, EnumHand hand) //Right click action.
      {
      if (player.isSneaking()) //If player is sneaking.
      {
      if (!world.isRemote) //Server side.
      {
      boolean owned = false;
      ItemStack card = player.getHeldItem(hand);//Creating an ItemStack for the item held in hand.
      NBTTagCompound nbt = card.getTagCompound();
      if(nbt == null){ nbt = new NBTTagCompound(); }
      nbt.setBoolean("owned", owned);
      
      if(nbt.getBoolean("owned") == true)//If Owned is on True, it means it has already been right clicked.
      {
      player.sendMessage(new TextComponentString("You can't create an account on an already owned card."));
      }
      else
      {
      owned = true; //We pass owned at true
      int deposited = 0; //We create new account with 0 as deposited since it's new.
      String owner = player.getName().toString(); //Take player name has owner variable.
      world.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F)); //Play a sound that alert everybody that a credit card was created.
      nbt.setString("owner", owner);
      nbt.setBoolean("owned", owned);
      System.out.println(nbt.getString("owner")); //Print owner name for debug
      System.out.println(nbt.getBoolean("owned")); //Print owned name for debug
      
      return new ActionResult(EnumActionResult.SUCCESS, new ItemStack(this)); //Return a success.
      }
      }
      }
      return new ActionResult(EnumActionResult.PASS, new ItemStack(this)); //Return a pass.
      }
      
      /*@Override
      @SideOnly(Side.CLIENT)
      public void addInformation(ItemStack stack, EntityPlayer par2player, List list, boolean par4)
      {
      
      ItemStack card = par2player.getHeldItemMainhand();
      if(card.getTagCompound() == null){ stack.setTagCompound(new NBTTagCompound()); }
      String loreCreativeOnly = ChatFormatting.RED + stack.getTagCompound().getString("owner");
      list.add(loreCreativeOnly);
      }*/
      
      }
      
      

      Comme ceci ?</itemstack>

      Oui ce gif est drôle.

      1 réponse Dernière réponse Répondre Citer 0
      • DeletedD Hors-ligne
        Deleted
        dernière édition par

        Je ne pense pas, car à chaque clic droit, tu remets la variable à false, du coup si on l’avait à true, au coup d’avant, cela ne sera pas sauvegardé.

        1 réponse Dernière réponse Répondre Citer 0
        • FlowF Hors-ligne
          Flow
          dernière édition par

          @‘Plaigon’:

          Je ne pense pas, car à chaque clic droit, tu remets la variable à false, du coup si on l’avait à true, au coup d’avant, cela ne sera pas sauvegardé.

          Oui effectivement xD Mais du coup si il doit être dans la fonction comment je suis censé faire ? Car si il est dans la fonction comme tu dis il sera directement remis à false à chaque click droit ? Il faut que je vérifie si j’ai un nbt tag à null et du coup le mettre à false ? Et si il est sur true on change rien ? Fin je vais essayer et j’édit 🙂

          EDIT : Je ne peut même pas faire comme je voulais faire car la variable n’est pas déclarée avant xD Si vous avez une idée je suis preneur sur le coup 🙂

          Oui ce gif est drôle.

          1 réponse Dernière réponse Répondre Citer 0
          • robin4002R Hors-ligne
            robin4002 Moddeurs confirmés Rédacteurs Administrateurs
            dernière édition par

            nbt.getBoolean(“xxx”) renvoies false s’il n’a jamais été défini.

            1 réponse Dernière réponse Répondre Citer 0
            • FlowF Hors-ligne
              Flow
              dernière édition par

              @‘robin4002’:

              nbt.getBoolean(“xxx”) renvoies false s’il n’a jamais été défini.

              Yep en effet, mais je dois être bête car si je fais comme tu me dis ca remet toujours à false Oo

              @Override
              public ActionResult <itemstack>onItemRightClick(World world, EntityPlayer player, EnumHand hand) //Right click action.
              {
              if (player.isSneaking()) //If player is sneaking.
              {
              if (!world.isRemote) //Server side.
              {
              
              ItemStack card = player.getHeldItem(hand);//Creating an ItemStack for the item held in hand.
              NBTTagCompound nbt = card.getTagCompound();
              if(nbt == null){ nbt = new NBTTagCompound(); }
              nbt.getBoolean("owned");
              if(nbt.getBoolean("owned") == true)//If Owned is on True, it means it has already been right clicked.
              {
              player.sendMessage(new TextComponentString("You can't create an account on an already owned card."));
              }
              else
              {
              boolean owned = true; //We pass owned at true
              int deposited = 0; //We create new account with 0 as deposited since it's new.
              String owner = player.getName().toString(); //Take player name has owner variable.
              world.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F)); //Play a sound that alert everybody that a credit card was created.
              nbt.setString("owner", owner);
              nbt.setBoolean("owned", owned);
              System.out.println(nbt.getString("owner")); //Print owner name for debug
              System.out.println(nbt.getBoolean("owned")); //Print owned name for debug
              
              return new ActionResult(EnumActionResult.SUCCESS, new ItemStack(this)); //Return a success.
              }
              }
              }
              return new ActionResult(EnumActionResult.PASS, new ItemStack(this)); //Return a pass.
              }
              
              ```</itemstack>

              Oui ce gif est drôle.

              1 réponse Dernière réponse Répondre Citer 0
              • DeletedD Hors-ligne
                Deleted
                dernière édition par

                Tu n’as plus le setTagCompound du début. Je pense que ça vient de là.

                1 réponse Dernière réponse Répondre Citer 0
                • FlowF Hors-ligne
                  Flow
                  dernière édition par

                  @Plaigon:

                  Tu n’as plus le setTagCompound du début. Je pense que ça vient de là.

                  Tu veut dire ça ? Car j’ai mis et toujours rien 😕

                  card.setTagCompound(nbt);
                  

                  Oui ce gif est drôle.

                  1 réponse Dernière réponse Répondre Citer 0
                  • robin4002R Hors-ligne
                    robin4002 Moddeurs confirmés Rédacteurs Administrateurs
                    dernière édition par

                    Tu l’as mis où ?

                    1 réponse Dernière réponse Répondre Citer 0
                    • FlowF Hors-ligne
                      Flow
                      dernière édition par

                      @‘robin4002’:

                      Tu l’as mis où ?

                      Je l’ai mis dans le else.

                      else
                      {
                      boolean owned = true; //We pass owned at true
                      int deposited = 0; //We create new account with 0 as deposited since it's new.
                      String owner = player.getName().toString(); //Take player name has owner variable.
                      world.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F)); //Play a sound that alert everybody that a credit card was created.
                      nbt.setString("owner", owner); // Set variable at owner.
                      nbt.setBoolean("owned", owned); // Set variable at owned.
                      
                      card.setTagCompound(nbt); // Set TagCompound of card item stack.
                      return new ActionResult(EnumActionResult.SUCCESS, new ItemStack(this)); //Return a success.
                      }
                      ``` Mais j'ai aussi essayer de le mettre dans le ```java
                      if (!world.isRemote) //Server side.
                      {}
                      

                      EDIT : Voici un débug avec

                      [13:09:31] [Server thread/INFO]: [STDOUT]: null
                      [13:09:31] [Server thread/INFO]: [STDOUT]: {}
                      [13:09:31] [Server thread/INFO]: [STDOUT]: {}
                      [13:09:31] [Server thread/INFO]: [STDOUT]: {owner:"Legrandfifou",owned:1b}
                      
                      if (!world.isRemote) //Server side.
                      {
                      
                      ItemStack card = player.getHeldItem(hand);//Creating an ItemStack for the item held in hand.
                      NBTTagCompound nbt = card.getTagCompound();
                      **System.out.println(nbt);**
                      if(nbt == null){ nbt = new NBTTagCompound(); }
                      **System.out.println(nbt);**
                      nbt.getBoolean("owned");
                      card.setTagCompound(nbt);
                      **System.out.println(nbt);**
                      if(nbt.getBoolean("owned") == true)//If Owned is on True, it means it has already been right clicked.
                      {
                      player.sendMessage(new TextComponentString("You can't create an account on an already owned card."));
                      System.out.println(nbt.getBoolean("owned"));
                      }
                      else
                      {
                      boolean owned = true; //We pass owned at true
                      int deposited = 0; //We create new account with 0 as deposited since it's new.
                      String owner = player.getName().toString(); //Take player name has owner variable.
                      world.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F)); //Play a sound that alert everybody that a credit card was created.
                      nbt.setString("owner", owner); // Set variable at owner.
                      nbt.setBoolean("owned", owned); // Set variable at owned.
                      System.out.println(nbt);
                      return new ActionResult(EnumActionResult.SUCCESS, new ItemStack(this)); //Return a success.
                      }
                      **System.out.println(nbt);**
                      card.setTagCompound(nbt);
                      **System.out.println(nbt);**
                      }
                      

                      On peut voir que après le nbt.getBoolean(“owned”) il n’affiche rien pour lui il n’y a pas de nbt tag …

                      Oui ce gif est drôle.

                      1 réponse Dernière réponse Répondre Citer 0
                      • FlowF Hors-ligne
                        Flow
                        dernière édition par

                        UP, du coup la il me faut de l’aide car je ne comprend pas trop pourquoi ça ne marche pas 😕

                        Oui ce gif est drôle.

                        1 réponse Dernière réponse Répondre Citer 0
                        • DeletedD Hors-ligne
                          Deleted
                          dernière édition par

                          Premièrement, tu dois finir ta fonction avec un return, or à la fin, on observe quelque println.
                          Deuxièmement, à chaque clic droit tu reset le tag compound de ton itemstack avec une nouvelle variable vide. Le setTagCompound(nbt), doit être placé uniquement dans le cas où ton itemstack n’en possède pas, donc dans le if(itemstack.getTagCompound() == null).
                          Ensuite ceci : nbt.getBoolean(“owned”); sert strictement à rien (ligne 10). Ensuite ta variable owned, comme dit précédemment ne doit pas toujours donner true, mais soit donner la valeur précédente (lors du dernier clic droit, logique), ou true si il s’agit du premier clic droit. Donc avec une condition ternaire, cela devrait ressebler à ceci :
                          boolean owned = itemstack.getTagCompound().hasKey(“owned”) ? itemstack.getTagCompound.getBoolean(“owned”) : true;

                          Sinon concernant le reste, ça m’a l’air bon, auquel cas robin, ou un autre membre, revérifiera !

                          1 réponse Dernière réponse Répondre Citer 0
                          • robin4002R Hors-ligne
                            robin4002 Moddeurs confirmés Rédacteurs Administrateurs
                            dernière édition par

                            Non, ça variable owned doit bien être sur false par défaut.
                            Donc un simple boolean owned = nbt.getBoolean(“owned”) est ok.
                            Par contre le code qui remet le tag doit être avant le return.

                            1 réponse Dernière réponse Répondre Citer 0
                            • FlowF Hors-ligne
                              Flow
                              dernière édition par

                              Du coup je dois avouer que je suis un peu perdu j’ai compris la condition ternaire mais je ne vois toujours aucun changement, le boolean est bien a false au départ, mais c’est cette histoire de card.setTagCompound que je ne comprend pas en fait.

                              @Override
                              public ActionResult <itemstack>onItemRightClick(World world, EntityPlayer player, EnumHand hand) //Right click action.
                              {
                              if (player.isSneaking()) //If player is sneaking.
                              {
                              if (!world.isRemote) //Server side.
                              {
                              
                              ItemStack card = player.getHeldItem(hand); //Creating an ItemStack for the item held in hand.
                              NBTTagCompound nbt = card.getTagCompound();
                              if(nbt == null)
                              {
                              nbt = new NBTTagCompound();
                              card.setTagCompound(nbt);
                              }
                              
                              nbt.getBoolean("owned");
                              System.out.println(nbt.getBoolean("owned"));
                              if(nbt.getBoolean("owned") == true)//If Owned is on True, it means it has already been right clicked.
                              {
                              player.sendMessage(new TextComponentString("You can't create an account on an already owned card."));
                              }
                              else
                              {
                              boolean owned = card.getTagCompound().hasKey("owned") ? card.getTagCompound().getBoolean("owned") : true; //We check if card hasKey("owned"), if yes we just take the value of the "owned" if not, we pass it at True.
                              int deposited = 0; //We create new account with 0 as deposited since it's new.
                              String owner = player.getName().toString(); //Take player name has owner variable.
                              world.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F)); //Play a sound that alert everybody that a credit card was created.
                              nbt.setString("owner", owner); // Set variable at owner.
                              nbt.setBoolean("owned", owned); // Set variable at owned.
                              return new ActionResult(EnumActionResult.SUCCESS, new ItemStack(this)); //Return a success.
                              }
                              }
                              return new ActionResult(EnumActionResult.FAIL, new ItemStack(this)); //Return a pass.
                              }
                              
                              return new ActionResult(EnumActionResult.PASS, new ItemStack(this)); //Return a pass.
                              }
                              
                              

                              Merci de votre aide en tout cas :)</itemstack>

                              Oui ce gif est drôle.

                              1 réponse Dernière réponse Répondre Citer 0
                              • 1 / 1
                              • Premier message
                                Dernier message
                              Design by Woryk
                              ContactMentions Légales

                              MINECRAFT FORGE FRANCE © 2024

                              Powered by NodeBB