Créer une arme custom simple
-
T’as essayé le code au moins ?
Je suis quasi-sûr que c’est dans l’autre sens
Dès que je peux je testeEt puis la méthode onUsingTick et onPlayerStoppedUsing ne marchent pas (je n’arrive pas à les appeler), je te conseille d’utiliser ces 2 là respectivement équivalentes

public void onUpdate(ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) {}
public ItemStack onItemRightClick(ItemStack p_77659_1_, World p_77659_2_, EntityPlayer p_77659_3_) {}
-
Non je n’ai pas testé encore car dans l’histoire j’ai deux armes et la seconde a un fonctionnement plus complexe avec 3 variables. Je voulais déjà savoir si ma façon de faire concernant les nbt était convenable pour une.
:::
public class ItemPlasmaGun extends Item { private int timerTir = 0; private int timerCold = 100; private boolean onUse; public void onPlayerStoppedUsing(ItemStack stack, World world, EntityPlayer player, int useTime) { this.onUse = false; } public void onUsingTick(ItemStack stack, EntityPlayer player, int count) { if(this.onUse == false) this.onUse = true; if (timerTir <= 150) { this.timerTir++; } if (timerTir % 5 == 0 && timerCold != 0 && timerTir <= 150) { EntityBullet entityBullet = new EntityBullet(player.worldObj, player); player.worldObj.playSoundAtEntity(player, "note.bd", 0.3F, 1.0F); if (!player.worldObj.isRemote) { player.worldObj.spawnEntityInWorld(entityBullet); } } } public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean b) { if(timerTir > 0 && timerTir != 151 && this.onUse == false) { this.timerTir–; } if (timerTir > 150 && timerCold != 0) { this.timerCold--; } if (timerCold == 0) { this.timerTir = 0; this.timerCold = 100; } // System.out.println(timerTir + "tir"); // System.out.println(timerCold + "cold"); } public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) { return stack; } public int getMaxItemUseDuration(ItemStack stack) { return 3000; } public EnumAction getItemUseAction(ItemStack stack) { return EnumAction.bow; } public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) { return true; } public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if (player.capabilities.isCreativeMode || player.inventory.hasItem(ModPg2.itemPlasmaGun)) { player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); } return stack; } public void writeToNBT(ItemStack stack) { if (!stack.hasTagCompound()) stack.setTagCompound(new NBTTagCompound()); stack.writeToNBT(stack.getTagCompound()); } }:::
Donc en gros quand on tire on fait monter une valeur, quand on ne tire plus, elle descend (sauf si elle atteint un maximum). Une valeur booléenne est utilisée pour savoir si l’item est en train d’être utilisé ou non (y’a peut-être plus simple mais j’ai pas voulu demander).
Si la première valeur atteint son maximum, elle ne peut plus redescendre quand on ne tire plus, une seconde valeur diminue alors (un cooldown) jusqu’à remettre à leurs valeurs initiales les deux valeurs.C’est une arme qui doit mimer une arme futuriste type Halo, avec surchauffe.
-
Bon en fait je me suis trompé

L’histoire du get et set n’a apparemment pas vraiment d’importante
Voici un code qui semble pour le moment fonctionnel, après t’as juste à faire tes conditions pour la valeur de timer. Sa je pense que tu en seras capable
public void onUpdate(ItemStack stack, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) { super.onUpdate(stack, p_77663_2_, p_77663_3_, p_77663_4_, p_77663_5_); if(!stack.hasTagCompound()) stack.setTagCompound(new NBTTagCompound()); int timer = stack.getTagCompound().getInteger("timer"); timer++; stack.getTagCompound().setInteger("timer", timer); System.out.println(timer); } -
Est-ce que je suis obligé de mettre ça dans la méthode onUpdate?
-
Tu voulais le mettre autre part ?
-
Eh bien c’est simplement le “this.timer++;” dans le onUpdate qui me gène. Il faut qu’il soit dans la méthode onUsingTick.
Si la place de cette ligne n’influence pas les nbt alors aucune autre question, je vais essayer de mettre les nbt pour la seconde arme suivant ce modèle. Comme ça on pourra me dire si ça semble correspondre à du java fonctionnel ou non.
-
Je t’ai dit avoir testé le code. Il fonctionne très bien, je sais ce que je fais

Au cas où tu n’aurais pas débuggé, la méthode onUsingTick semble ne pas marcher et puis elle sert à la même chose que onUpdate() : être appelé à chaque tick. -
onUsingTick est appelé quand on fait un clic droit, à chaque tick.
-
Il y a une grande différence entre les deux méthodes, l’une n’est appelée que lorsque l’on est en train d’utiliser l’arme.
Je ne sais pas utiliser les nbt, par contre si j’incrémente* mon “timer” dans onUpdate, je suis sûr de foutre en l’air le fonctionnement de l’arme.
-
OK

Merci autant pour moi, bah alors autant la garder pour incrémenter timer et se servir de onUpdate pour la décrémenter comme Toutoune souhaitait
En fait timer est obligé d’être local à la méthode où les NBT s’en servent. Teste en la mettant en dehors de onUpdate() ou en dehors de onPlayerStoppedUsing() et tu verras que les NBT ne se sauvegardent pas

En fait il faudrait enregistrer la variable timer dans les NBT dans la méthode onUpdate comme ça on aurait juste à la get dans onPlayerStoppedUsing() -
:::
public class ItemRailGun extends Item { private int timer = 0; public void onPlayerStoppedUsing(ItemStack stack, World world, EntityPlayer player, int useTime) { if (this.timer < 60) { this.timer = 0; } if (this.timer >= 60) { EntityBullet entityBullet = new EntityBullet(world, player); world.playSoundAtEntity(player, "note.bd", 0.3F, 1.0F); if (!world.isRemote) { player.worldObj.spawnEntityInWorld(entityBullet); this.timer = 0; } } } public void onUsingTick(ItemStack stack, EntityPlayer player, int count) { this.timer++; } public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean b) { if(!stack.hasTagCompound()) { stack.setTagCompound(new NBTTagCompound()); } int timer = stack.getTagCompound().getInteger("timer"); stack.getTagCompound().setInteger("timer", timer); } public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) { return stack; } public int getMaxItemUseDuration(ItemStack stack) { return 300; } public EnumAction getItemUseAction(ItemStack stack) { return EnumAction.bow; } public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) { return true; } public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if (player.capabilities.isCreativeMode || player.inventory.hasItem(ModPg2.itemRailGun)) { player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); } return stack; } }:::
En faisant comme ça, la valeur du timer est toujours la bonne, celle enregistrée dans les nbt?
-
Je redis la même chose qu’avant, tu ne dois pas avoir de variable dans ta classe, les nbt tu les lis au moment où tu en as besoin
-
J’veux bien mais je ne comprend pas ce que ça veut dire. (en java)
Le “private int timer” vire je suppose et je fais comment pour récupérer la valeur du nbt tag voulu, svp?
-
Quand tu veux récupéré le timer présent dans le NBTTagCompound tu fait :
int timer = stack.getTagCompound().getInteger("timer"); -
D’acc, est-ce que j’en ai trop mis ou pas assez?
:::
public class ItemRailGun extends Item { public void onPlayerStoppedUsing(ItemStack stack, World world, EntityPlayer player, int useTime) { int timer = stack.getTagCompound().getInteger("timer"); if (timer < 60) { timer = 0; stack.getTagCompound().setInteger("timer", timer); } if (timer >= 60) { EntityBullet entityBullet = new EntityBullet(world, player); world.playSoundAtEntity(player, "note.bd", 0.3F, 1.0F); if (!world.isRemote) { player.worldObj.spawnEntityInWorld(entityBullet); timer = 0; stack.getTagCompound().setInteger("timer", timer); } } } public void onUsingTick(ItemStack stack, EntityPlayer player, int count) { int timer = stack.getTagCompound().getInteger("timer"); timer++; stack.getTagCompound().setInteger("timer", timer); } public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean b) { if(!stack.hasTagCompound()) { stack.setTagCompound(new NBTTagCompound()); } int timer = stack.getTagCompound().getInteger("timer"); stack.getTagCompound().setInteger("timer", timer); } public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) { return stack; } public int getMaxItemUseDuration(ItemStack stack) { return 300; } public EnumAction getItemUseAction(ItemStack stack) { return EnumAction.bow; } public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) { return true; } public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if (player.capabilities.isCreativeMode || player.inventory.hasItem(ModPg2.itemRailGun)) { player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); } return stack; } }:::
J’ai supposé qu’il fallait set à chaque fois que ma valeur change.
De ce que j’ai compris " stack.getTagCompound().setInteger(“timer”, timer);" permet d’enregistrer la valeur. -
Tout est bon sauf dans le onUpdate où tout ce que tu fais c’est récupérer la variable puis “set” la variable sans aucune action entre
-
Ouki, j’ai viré cette méthode du coup.
Par contre j’ai un problème avec l’arme à cause du onUsingTick. Si je set la valeur après le timer++; l’arme “pète un plomb” (fais l’action de l’arc comme voulu mais fonctionne comme un clignotant) et la valeur monte toute seule même sans utiliser l’arme.
Si je retire le set, évidemment la valeur reste à 1 mais l’action “arc” se déroule normalement.
:::
public class ItemRailGun extends Item { public void onPlayerStoppedUsing(ItemStack stack, World world, EntityPlayer player, int useTime) { int timer = stack.getTagCompound().getInteger("timer"); if (timer < 60) { timer = 0; stack.getTagCompound().setInteger("timer", timer); } if (timer >= 60) { EntityBullet entityBullet = new EntityBullet(world, player); world.playSoundAtEntity(player, "note.bd", 0.3F, 1.0F); if (!world.isRemote) { player.worldObj.spawnEntityInWorld(entityBullet); timer = 0; stack.getTagCompound().setInteger("timer", timer); } } } public void onUsingTick(ItemStack stack, EntityPlayer player, int count) { if(!stack.hasTagCompound()) { stack.setTagCompound(new NBTTagCompound()); } int timer = stack.getTagCompound().getInteger("timer"); timer++; stack.getTagCompound().setInteger("timer", timer); System.out.println(timer); } public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) { return stack; } public int getMaxItemUseDuration(ItemStack stack) { return 300; } public EnumAction getItemUseAction(ItemStack stack) { return EnumAction.bow; } public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) { return true; } public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if (player.capabilities.isCreativeMode || player.inventory.hasItem(ModPg2.itemRailGun)) { player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); } return stack; } }:::
-
Regarde si la méthode est appelée en permanence, si oui rajoute une condition pour vérifier que le joueur est en train de l’utiliser.
Au passage : tu as déjà un timer incrémenté tous les ticks, regarde le dernier paramètre de la fonction onPlayerStoppedUsing.
-
Oui j’avais vu ce paramètre (après) mais le fonctionnement de ma deuxième arme nécessite tout de même d’enregistrer une valeur dans les nbt (en gros pour faire diminuer le count quand le joueur n’utilise pas l’arme).
Par contre je me sers d’une valeur booléenne pour savoir si l’item est utilisé, y a-t-il plus simple?
En gros l’arme réagit comme si on était en train de l’utiliser alors que non.Voici la classe de ma deuxième arme (modifiée selon les derniers bons conseils et possédant déjà une valeur booléenne pour tenter de résoudre le problème), mais le problème persiste
:::
public class ItemPlasmaGun extends Item { public void onPlayerStoppedUsing(ItemStack stack, World world, EntityPlayer player, int useTime) { boolean onUse = stack.getTagCompound().getBoolean("onUse"); onUse = false; stack.getTagCompound().setBoolean("onUse", onUse); } public void onUsingTick(ItemStack stack, EntityPlayer player, int count) { int timerTir = stack.getTagCompound().getInteger("timerTir"); int timerCold = stack.getTagCompound().getInteger("timerCold"); boolean onUse = stack.getTagCompound().getBoolean("onUse"); if(onUse == false) { onUse = true; stack.getTagCompound().setBoolean("onUse", onUse); } if (timerTir <= 150 && onUse == true) { timerTir++; stack.getTagCompound().setInteger("timerTir", timerTir); } if (timerTir % 5 == 0 && timerCold != 0 && timerTir <= 150 && onUse == true) { EntityBullet entityBullet = new EntityBullet(player.worldObj, player); player.worldObj.playSoundAtEntity(player, "note.bd", 0.3F, 1.0F); if (!player.worldObj.isRemote) { player.worldObj.spawnEntityInWorld(entityBullet); } } } public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean b) { if(!stack.hasTagCompound()) { stack.setTagCompound(new NBTTagCompound()); } int timerTir = stack.getTagCompound().getInteger("timerTir"); int timerCold = stack.getTagCompound().getInteger("timerCold"); boolean onUse = stack.getTagCompound().getBoolean("onUse"); if(timerTir > 0 && timerTir != 151 && onUse == false) { timerTir–; stack.getTagCompound().setInteger("timerTir", timerTir); } if (timerTir > 150 && timerCold != 0) { timerCold--; stack.getTagCompound().setInteger("timerCold", timerCold); } if (timerCold == 0) { timerTir = 0; timerCold = 100; stack.getTagCompound().setInteger("timerTir", timerTir); stack.getTagCompound().setInteger("timerCold", timerCold); } System.out.println(timerTir + "tir"); System.out.println(timerCold + "cold"); } public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) { return stack; } public int getMaxItemUseDuration(ItemStack stack) { return 3000; } public EnumAction getItemUseAction(ItemStack stack) { return EnumAction.bow; } public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) { return true; } public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if (player.capabilities.isCreativeMode || player.inventory.hasItem(ModPg2.itemPlasmaGun)) { player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); } return stack; } public void writeToNBT(ItemStack stack) { if (!stack.hasTagCompound()) stack.setTagCompound(new NBTTagCompound()); stack.writeToNBT(stack.getTagCompound()); } }:::
-
Dans ton code il y a beaucoup de choses inutiles et il y a des choses que je ne comprends pas :
- Pourquoi faire ceci :
boolean onUse = stack.getTagCompound().getBoolean("onUse"); onUse = false; stack.getTagCompound().setBoolean("onUse", onUse);au lieu de ceci :
stack.getTagCompound().setBoolean("onUse", false);- A quoi ça sert de faire ça :
public void writeToNBT(ItemStack stack) { if (!stack.hasTagCompound()) stack.setTagCompound(new NBTTagCompound()); stack.writeToNBT(stack.getTagCompound()); }