Enregistrer les NBT Tags et Individualisé les items?
-
Tu essaies d’accéder a une valeur d’un tag NBT null.
Avant chaque utilisation de stack.getTagCompound() il faut que tu t’assures que le tag n’est pas null, en vérifiant avecstack.hasTagCompound() -
@robin4002 Merci beaucoup , c’était la première fois que j’utilisais les nbt tag et je suis encore pas très bon en dev en java.
-
De rien ! Le problème est résolu ?
-
@robin4002 enfaite je viens de remarquer un autre problème , c’est que ça enregistre les nbt tag sur l’item qui se trouve dans l’inventaire , le plus à gauche
public class ItemWeapon extends Item { public int maxAmmo; public int cooldown; public int timer ; public Item bullet; Minecraft mc = Minecraft.getMinecraft(); public ItemWeapon(String name, int munition,Item itembullet,int cooldown){ this.setUnlocalizedName(name); this.setRegistryName(name); this.setCreativeTab(Main.SpaceTab); Items.ITEMS.add(this); this.cooldown = cooldown*20; this.maxStackSize = 1; bullet = itembullet; this.maxAmmo = munition; } private void setmun(ItemStack stack,int mun){ if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { stack.getTagCompound().setInteger("mun", mun); } } private int getmun(ItemStack stack){ if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { int getmun = stack.getTagCompound().getInteger("mun"); return getmun; } } private boolean setCooldown(int x){ return timer>= x * cooldown; } private void Cooldown(){ mc.player.getCooldownTracker().setCooldown(this, cooldown); } @Override public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { timer++; KeyBinding[] keyBindings = ClientProxy.keyBindings; if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { if (keyBindings[0].isPressed()) { findammo(stack); } } super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected); } private void findammo(ItemStack stack){ if(!stack.hasTagCompound()) { stack.setTagCompound(new NBTTagCompound()); }else { NBTTagCompound nbt = stack.getTagCompound(); if(setCooldown(2) && mc.player.getHeldItem(EnumHand.MAIN_HAND).getItem() == this) { timer = 0; if (mc.player.inventory.hasItemStack(new ItemStack(Items.Bullet)) && nbt.getInteger("mun") <= this.maxAmmo) { if (nbt.hasKey("mun")) { setmun(stack, getmun(stack) + 1); } else { setmun(stack, 0); } stack.setTagCompound(nbt); mc.player.inventory.clearMatchingItems(this.bullet, 0, 1, null); Cooldown(); System.out.println(nbt.getInteger("mun") + "tir"); } } } } @Override public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack stack) { Minecraft mc = Minecraft.getMinecraft(); if(entityLiving instanceof EntityPlayer) { if(!entityLiving.world.isRemote) { if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { if (getmun(stack) >= 1) { if (setCooldown(2)) { EntityBullet entitysnowball = new EntityBullet(entityLiving.world, mc.player); entitysnowball.shoot(mc.player, mc.player.rotationPitch + 5.0f, mc.player.rotationYaw + 5.0f, 0.0F, 1.5F, 1.0F); entityLiving.world.spawnEntity(entitysnowball); Cooldown(); setmun(stack, getmun(stack) - 1); } } else { findammo(stack); } } } } return super.onEntitySwing(entityLiving, stack); } -
Étrange, je ne vois pas ce qui peut expliquer ce comportement

-
@robin4002 j’ai finalement réussi en utilisant la fonction :
findammo(mc.player.inventory.getCurrentItem());
-
@robin4002 Enfaite je viens de regarder j’ai rien changer à mon code , mais le problème c’est que je ne sais pas pourquoi mais quand je relance le monde il y a 1 arme qui est rechargé a 1/6 et les autres à 0/6 à chaque fois que je reviens dans le monde e je ne sais pas d’ou ça viens
Le problème avait été résolu mais je ne sais pas pourquoi il est revenue :public class ItemWeapon extends Item { public int maxAmmo; public int cooldown; public int timer ; public Item bullet; Minecraft mc = Minecraft.getMinecraft(); public ItemWeapon(String name, int munition,Item itembullet,int cooldown){ this.setUnlocalizedName(name); this.setRegistryName(name); this.setCreativeTab(Main.SpaceTab); Items.ITEMS.add(this); this.cooldown = cooldown*20; this.maxStackSize = 1; bullet = itembullet; this.maxAmmo = munition; } private void setmun(ItemStack stack,int mun){ if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { stack.getTagCompound().setInteger("mun", mun); } } private int getmun(ItemStack stack){ if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { int getmun = stack.getTagCompound().getInteger("mun"); return getmun; } } private boolean setCooldown(int x){ return timer>= x * cooldown; } private void Cooldown(){ mc.player.getCooldownTracker().setCooldown(this, cooldown); } @Override public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { timer++; KeyBinding[] keyBindings = ClientProxy.keyBindings; if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { if (keyBindings[0].isPressed()) { findammo(mc.player.inventory.getCurrentItem()); } } super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected); } private void findammo(ItemStack stack){ if(!stack.hasTagCompound()) { stack.setTagCompound(new NBTTagCompound()); }else { NBTTagCompound nbt = stack.getTagCompound(); if(setCooldown(2) && mc.player.getHeldItem(EnumHand.MAIN_HAND).getItem() == this) { timer = 0; if (mc.player.inventory.hasItemStack(new ItemStack(Items.Bullet)) && nbt.getInteger("mun") <= this.maxAmmo) { if (nbt.hasKey("mun")) { setmun(stack, getmun(stack) + 1); } else { setmun(stack, 0); } stack.setTagCompound(nbt); mc.player.inventory.clearMatchingItems(this.bullet, 0, 1, null); Cooldown(); System.out.println(nbt.getInteger("mun") + "tir"); } } } } @Override public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack stack) { Minecraft mc = Minecraft.getMinecraft(); if(entityLiving instanceof EntityPlayer) { if(!entityLiving.world.isRemote) { if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { if (getmun(stack) >= 1) { if (setCooldown(2)) { EntitySnowball entitysnowball = new EntitySnowball(entityLiving.world, mc.player); entitysnowball.shoot(mc.player, mc.player.rotationPitch + 5.0f, mc.player.rotationYaw + 5.0f, 0.0F, 1.5F, 1.0F); entityLiving.world.spawnEntity(entitysnowball); Cooldown(); setmun(stack, getmun(stack) - 1); } } else { findammo(mc.player.inventory.getCurrentItem()); } } } } return super.onEntitySwing(entityLiving, stack); } @Override public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) { if(!stack.hasTagCompound()){ stack.setTagCompound(new NBTTagCompound()); } { tooltip.add(getmun(stack) + "/6 munitions"); } super.addInformation(stack, worldIn, tooltip, flagIn); } public void registerModels() { Main.proxy.registerItemRenderer(this, 0, "inventory"); }}
-
@robin4002 tu en penses quoi robin ?
-
Je ne sais pas
-
@robin4002 car j’ai essayé de voir de regarder mais a chaque fois ça se remet à 0 mais je ne comprend pas pourquoi
-
R robin4002 a déplacé ce sujet de Support pour les moddeurs sur