• S'inscrire
    • Se connecter
    • Recherche
    • Récent
    • Mots-clés
    • Populaire
    • Utilisateurs
    • Groupes

    Résolu NBTTagCompound qui ne s'enregistre pas dans un ItemStack

    1.7.x
    1.7.10
    2
    7
    1541
    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.
    • Deleted
      Deleted dernière édition par

      Bonjour tout le monde
      Hé oui, il est vrai que désormais 99 % de mes post concernent les NBTTagCompound ^^
      Aujourd’hui j’ai tenté de créer un pistolet qui doit se recharger au bout d’un certain nombre de munitions tirée (ici 3). Une fois cette limite atteinte, je passe un boolean à false enregistré dans les tags de l’ItemStack, le seul problème c’est que si vous prenez après cela un autre même item, et que vous faites clic droit (juste une fois), ce-dernier aura aussi le même tag passé aussi à false.
      Je ne sais pas trop si j’ai été précis ^^’
      Le code en tout cas lui, devrait l’être

      Le code de mon item

      :::

      
      public class Gun extends Item 
      {
      
      private short fireRate;
      private short reloadingTime;
      private byte reloadingCounter;
      private Item itemUsedToShoot;
      
      private byte munitionsUsedCount;
      private boolean isReloading;
      
      public Gun(short fireRate, short reloadingTime, short maxusescount, byte reloadingcounter, Item itemusedtoshoot)
      {
      this.fireRate = fireRate;
      this.reloadingTime = reloadingTime;
      this.reloadingCounter = reloadingcounter;
      this.itemUsedToShoot = itemusedtoshoot;
      
      munitionsUsedCount = 0;
      isReloading = false;
      
      this.setMaxStackSize(1);
      this.setMaxDamage(maxusescount);
      
      }
      
      @Override
      public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
      {
      NBTTagCompound compound = new NBTTagCompound();
      if(!isReloading)
      {
      compound.setBoolean("isReloading", true);
      stack.setTagCompound(compound);
      if(player.inventory.hasItem(itemUsedToShoot))
      {
      if(player.inventory.consumeInventoryItem(itemUsedToShoot))
      {
      if(munitionsUsedCount == reloadingCounter)
      {
      isReloading = true;
      }
      else
      {
      munitionsUsedCount++;
      compound.setByte("munitionsUsedCount", munitionsUsedCount);
      stack.setTagCompound(compound);
      if(world.isRemote)
      {
      for (int i = 0; i < 4; ++i)
      {
      world.spawnParticle("explode", player.posX + (world.rand.nextDouble() - 0.5D) * (double)player.width, player.posY + world.rand.nextDouble() * (double)player.height - 0.25D, player.posZ + (world.rand.nextDouble() - 0.5D) * (double)player.width, (world.rand.nextDouble() - 0.5D) * 2.0D, -world.rand.nextDouble(), (world.rand.nextDouble() - 0.5D) * 2.0D);
      }
      world.playSoundAtEntity(player, "random.explode", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
      }
      else
      {
      EntitySheep sheeptest = new EntitySheep(world);
      sheeptest.setLocationAndAngles(player.posX, player.posY, player.posZ, player.rotationYaw, player.rotationPitch);
      world.spawnEntityInWorld(sheeptest);
      stack.damageItem(1, player);
      }
      }
      }
      }
      else
      {
      compound.setBoolean("isReloading", false);
      stack.setTagCompound(compound);
      }
      }
      return stack;
      }
          public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) 
          {
          if(stack.stackTagCompound != null)
          {
          if(stack.stackTagCompound.hasKey("isReloading"))
          {
          String information = "Est en train d'être rechargée";
          list.add(information);
          }
          }
          }    
      }
      
      

      :::

      Merci d’avance =D

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

        Car tu utilises private boolean isReloading; comme variable local à la classe. Cette variable ne devrait pas exister.
        if(!isReloading) -> tu remplaces par !stack.hasTagCompound() || !stack.getTagCompound().getBoolean(“isReloading”)

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

          J’ai remplacé toutes mes variables isReloading par ce que tu m’as dit dans la méthode. Voici d’ailleurs ce à quoi elle ressemble désormais ^^

          :::

          
          @Override
          public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
          {
          NBTTagCompound compound = new NBTTagCompound();
          if(player.capabilities.isCreativeMode)
          { 
          if(world.isRemote)
          {
          player.addChatComponentMessage(new ChatComponentTranslation("item.gun.inGodMode"));
          }
          //Code pour tirer illimité
          }
          else
          {
          if(!stack.hasTagCompound() || !stack.getTagCompound().getBoolean("isReloading"))
          {
          compound.setBoolean("isReloading", false);
          stack.setTagCompound(compound);
          if(player.inventory.hasItem(itemUsedToShoot))
          {
          if(player.inventory.consumeInventoryItem(itemUsedToShoot))
          {
          compound.setByte("munitionsUsedCount", munitionsUsedCount);
          compound.setShort("fireRate", fireRate);
          stack.setTagCompound(compound);
          if(stack.getTagCompound().getByte("munitionsUsedCount") == reloadingCounter)
          {
          //rechargemnt
          System.out.println("rechargement");
          compound.setBoolean("isReloading", true);
          }
          else
          {
          stack.getTagCompound().setByte("munitionsUsedCount", munitionsUsedCount++);
          stack.setTagCompound(compound);
          if(world.isRemote)
          {
          for (int i = 0; i < 4; ++i)
          {
          world.spawnParticle("explode", player.posX + (world.rand.nextDouble() - 0.5D) * (double)player.width, player.posY + world.rand.nextDouble() * (double)player.height - 0.25D, player.posZ + (world.rand.nextDouble() - 0.5D) * (double)player.width, (world.rand.nextDouble() - 0.5D) * 2.0D, -world.rand.nextDouble(), (world.rand.nextDouble() - 0.5D) * 2.0D);
          }
          world.playSoundAtEntity(player, "random.explode", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
          }
          else
          {
          EntitySheep sheeptest = new EntitySheep(world);
          sheeptest.setLocationAndAngles(player.posX, player.posY, player.posZ, player.rotationYaw, player.rotationPitch);
          world.spawnEntityInWorld(sheeptest);
          stack.damageItem(1, player);
          }
          }
          }
          }
          }
          else if(stack.getTagCompound().getBoolean("isReloading"))
          {
          compound.setBoolean("isReloading", true);
          stack.setTagCompound(compound);
          }
          }
          System.out.println(stack.getTagCompound().getBoolean("isReloading"));
          return stack;
          }
          
          

          :::

          Mais le problème persiste…

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

            Pourtant ça ne devrait pas. Le second item contient-il le tag isReloading avec la valeur true ?

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

              Je t’explique la scène :

              Je prend 3 pistolets après avoir généré une nouvelle map.
              Ensuite je clique droit 3 fois il me fait bien spawn 3 moutons, le 4ème clique ne fait rien car c’est le rechargement.
              Je m’empare ensuite de mon deuxième item qui est toujours sans le moindre tag, seulement je fais juste 1 seule fois clic droit il me consomme juste 1 munition, me fait rien spawn et puis à ce moment là son tag devient true, et ainsi de suite pour le troisième item….

              Tiens au cas où je te refile la classe de l’item 🙂
              :::

              
              public class Gun extends DyingCraftItem 
              {
              
              private short fireRate;
              private short reloadingTime;
              private byte reloadingCounter;
              private Item itemUsedToShoot;
              
              private byte munitionsUsedCount;
              private boolean isReloading;
              
              public Gun(short fireRate, short reloadingTime, short maxusescount, byte reloadingcounter, Item itemusedtoshoot)
              {
              this.fireRate = fireRate;
              this.reloadingTime = reloadingTime;
              this.reloadingCounter = reloadingcounter;
              this.itemUsedToShoot = itemusedtoshoot;
              
              munitionsUsedCount = 0;
              isReloading = false;
              
              this.setMaxStackSize(1);
              this.setMaxDamage(maxusescount);
              
              }
              
              @Override
              public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
              {
              NBTTagCompound compound = new NBTTagCompound();
              if(player.capabilities.isCreativeMode)
              { 
              if(world.isRemote)
              {
              player.addChatComponentMessage(new ChatComponentTranslation("item.gun.inGodMode"));
              }
              //Code pour tirer illimité
              }
              else
              {
              if(!stack.hasTagCompound() || !stack.getTagCompound().getBoolean("isReloading"))
              {
              compound.setBoolean("isReloading", false);
              stack.setTagCompound(compound);
              if(player.inventory.hasItem(itemUsedToShoot))
              {
              if(player.inventory.consumeInventoryItem(itemUsedToShoot))
              {
              compound.setByte("munitionsUsedCount", munitionsUsedCount);
              compound.setShort("fireRate", fireRate);
              stack.setTagCompound(compound);
              if(stack.getTagCompound().getByte("munitionsUsedCount") == reloadingCounter)
              {
              //rechargemnt
              System.out.println("rechargement");
              compound.setBoolean("isReloading", true);
              }
              else
              {
              stack.getTagCompound().setByte("munitionsUsedCount", munitionsUsedCount++);
              stack.setTagCompound(compound);
              if(world.isRemote)
              {
              for (int i = 0; i < 4; ++i)
              {
              world.spawnParticle("explode", player.posX + (world.rand.nextDouble() - 0.5D) * (double)player.width, player.posY + world.rand.nextDouble() * (double)player.height - 0.25D, player.posZ + (world.rand.nextDouble() - 0.5D) * (double)player.width, (world.rand.nextDouble() - 0.5D) * 2.0D, -world.rand.nextDouble(), (world.rand.nextDouble() - 0.5D) * 2.0D);
              }
              }
              else
              {
              EntitySheep sheeptest = new EntitySheep(world);
              sheeptest.setLocationAndAngles(player.posX, player.posY, player.posZ, player.rotationYaw, player.rotationPitch);
              world.spawnEntityInWorld(sheeptest);
              stack.damageItem(1, player);
              world.playSoundAtEntity(player, "random.explode", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
              }
              }
              }
              }
              }
              else if(stack.getTagCompound().getBoolean("isReloading"))
              {
              compound.setBoolean("isReloading", true);
              stack.setTagCompound(compound);
              }
              }
              System.out.println(stack.getTagCompound().getBoolean("isReloading"));
              return stack;
              }
                  public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) 
                  {
                  if(stack.stackTagCompound != null)
                  {
                  if(stack.stackTagCompound.hasKey("isReloading"))
                  {
                  String information = "Est en train d'être rechargée";
                  list.add(information);
                  }
                  }
                  }    
              }
              
              

              :::

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

                Faut ajouter des System.out.println(différent truc ici, mets les aussi à différent endroit, print la valeur du tag aussi …);

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

                  Ok je vais faire ça
                  Mais c’est tout de même bizarre que ça marche nikel pour le premier item mais pas pour les autres.
                  Mon code ne semblait pas faux pourtant o_O

                  Voici ma version avec plus de logs
                  :::

                  
                  @Override
                  public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
                  {
                  NBTTagCompound compound = new NBTTagCompound();
                  if(player.capabilities.isCreativeMode)
                  { 
                  if(world.isRemote)
                  {
                  player.addChatComponentMessage(new ChatComponentTranslation("item.gun.inGodMode"));
                  }
                  //Code pour tirer illimité
                  }
                  else
                  {
                  if(!stack.hasTagCompound() || !stack.getTagCompound().getBoolean("isReloading"))
                  {
                  System.out.println("tag null ? " + stack.hasTagCompound() == null);
                  compound.setBoolean("isReloading", false);
                  stack.setTagCompound(compound);
                  if(player.inventory.hasItem(itemUsedToShoot))
                  {
                  if(player.inventory.consumeInventoryItem(itemUsedToShoot))
                  {
                  compound.setByte("munitionsUsedCount", munitionsUsedCount);
                  compound.setShort("fireRate", fireRate);
                  stack.setTagCompound(compound);
                  if(stack.getTagCompound().getByte("munitionsUsedCount") == reloadingCounter)
                  {
                  //rechargemnt
                  System.out.println("rechargement");
                  compound.setBoolean("isReloading", true);
                  }
                  else
                  {
                  stack.getTagCompound().setByte("munitionsUsedCount", munitionsUsedCount++);
                  stack.setTagCompound(compound);
                  System.out.println("Avant dernier else " + stack.getTagCompound().getByte("munitionsUsedCount"));
                  if(world.isRemote)
                  {
                  for (int i = 0; i < 4; ++i)
                  {
                  world.spawnParticle("explode", player.posX + (world.rand.nextDouble() - 0.5D) * (double)player.width, player.posY + world.rand.nextDouble() * (double)player.height - 0.25D, player.posZ + (world.rand.nextDouble() - 0.5D) * (double)player.width, (world.rand.nextDouble() - 0.5D) * 2.0D, -world.rand.nextDouble(), (world.rand.nextDouble() - 0.5D) * 2.0D);
                  }
                  }
                  else
                  {
                  EntitySheep sheeptest = new EntitySheep(world);
                  sheeptest.setLocationAndAngles(player.posX, player.posY, player.posZ, player.rotationYaw, player.rotationPitch);
                  world.spawnEntityInWorld(sheeptest);
                  stack.damageItem(1, player);
                  world.playSoundAtEntity(player, "random.explode", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
                  }
                  }
                  }
                  }
                  }
                  else if(stack.getTagCompound().getBoolean("isReloading"))
                  {
                  compound.setBoolean("isReloading", true);
                  stack.setTagCompound(compound);
                  }
                  }
                  System.out.println(stack.getTagCompound().getBoolean("isReloading"));
                  return stack;
                  }
                  
                  

                  :::

                  Par-contre ça ne m’a pas du tout + aidé. Je ne comprends rien du tout c’est à croire que mon ItemStack ne retient pas la variable compound. Pourtant je la set bien , à plusieurs reprises même. Tu as sûrement déjà des items avec des NBTTagCompound en +.
                  Y’en aurait-il sur le github de MFF ?

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

                  MINECRAFT FORGE FRANCE © 2018

                  Powered by NodeBB