Sauvegarde NBTTagList Player Capabilities
-
Mais c’est quand même bizarre l’inventaire est géré dans une liste pour les slots non ?
Sinon, il faudrait que je passe par une boucle pour lire la liste des spells et les ajouter en tant que tags indépendant les uns des autres ? Oo
-
Pourtant sur nhg on a ça :
@Override public void readNBT(Capability <inhgcapability>capability, INHGCapability instance, EnumFacing side, NBTBase nbt) { NBTTagCompound tag = ((NBTTagCompound)nbt); instance.setKitId(tag.getInteger("KitId")); ….Et le cast ne plante pas.
Je ne comprend pas pourquoi ça ne passe pas chez toi.</inhgcapability> -
La liste n’a pas l’air de pouvoir être cast on dirait
net.minecraft.nbt.NBTTagList cannot be cast to net.minecraft.nbt.NBTTagCompound -
Ce n’est pas la liste qu’il faut cast à NBTTagCompound, mais le truc “nbt” de type NBTBase.
-
Hein ? Tu fais comment ?
-
Bah ça :
@Override public void readNBT(Capability <ispells>capability, ISpells instance, EnumFacing side, NBTBase nbt) { final NBTTagCompound comp = (NBTTagCompound) nbt; //ICI final NBTTagList tagList = comp.getTagList("Spells", Constants.NBT.TAG_COMPOUND); for (int i = 0; i < tagList.tagCount(); i++) { final NBTTagCompound tag = tagList.getCompoundTagAt(i); final String s = tag.getString("Spells" + i); instance.getSpell().add(i, s); } }Logiquement c’est ça.</ispells>
-
Même crash à la ligne que tu m’a proposé
net.minecraft.nbt.NBTTagList cannot be cast to net.minecraft.nbt.NBTTagCompound -
System.out.println(nbt); affiche quoi dans la console ?
Ou alors tu peux mettre un point d’arrêt et regarder en débug ce que contient nbt ? -
Je pense que l’ancien Tag enregistré était un NBTTagList donc il faut mettre un if instanceof NBTTaCompound.
-
[19:12:52] [Server thread/INFO] [STDOUT]: [fr.caminelot.common.capability.SpellsStorage:readNBT:37]: [] -
Hum ça semble être vide.
Il y a un soucis
Mais je ne vois pas d’où ça peut venir :s
-
Bon j’ai un peu avancé, enfin je crois….
Maintenant j’ai une boucle infinie lel
Code actuel:
:::
public class SpellsStorage implements IStorage <ispells>{ @Override public NBTBase writeNBT(Capability <ispells>capability, ISpells instance, EnumFacing side) { final NBTTagCompound tag = new NBTTagCompound(); final NBTTagList tagList = new NBTTagList(); for (int i = 0; i < instance.getSpell().size(); i++) { if (instance.getSpell().get(i) != null) { final String s = instance.getSpell().get(i); if (s != null) { tag.setString("Spells" + i, s); tagList.appendTag(tag); } } } tag.setTag("Spells", tagList); return tag; } @Override public void readNBT(Capability <ispells>capability, ISpells instance, EnumFacing side, NBTBase nbt) { final NBTTagList tagList = ((NBTTagCompound) nbt).getTagList("Spells", Constants.NBT.TAG_COMPOUND); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.get(i); final String s = tag.getString("Spells" + i); instance.getSpell().add(i, s); } }:::
Crash Report:
https://gist.github.com/ZeAmateis/739b960ca18e24a5f23dbe69f58bab29</ispells></ispells></ispells>
-
Hum !
Ce que tu as fait est très bizarre :
Créer TagCompound dans lequel tu enrigistre des strings et un TagList que contient plusieurs fois le TagCompound … o_o
En soit, il y a deux manières de faire : -soit tu passe que par un TagCompound où tu enregistre les string,
-soit tu passe que par un TagList où tu enregistre directement les string (plus logique à mon goût ) (et tu peut si tu veux enregistrer ce TagList dans un TagCompund mais dans ton cas, ça me parais moins utile)EDIT :
Voila ce que j’aurais fait :
@Override public NBTBase writeNBT(Capability <ispells>capability, ISpells instance, EnumFacing side) { final NBTTagList tagList = new NBTTagList(); for (int i = 0; i < instance.getSpell().size(); i++) { if (instance.getSpell().get(i) != null) { final String s = instance.getSpell().get(i); if (s != null) { tagList.appendTag(new NBTTagString(s)); } } } return tagList; } @Override public void readNBT(Capability <ispells>capability, ISpells instance, EnumFacing side, NBTBase nbt) { final NBTTagList tagList = (NBTTagList) nbt; for (int i = 0; i < tagList.tagCount(); i++) { final String s = tagList.getStringTagAt(i); instance.getSpell().add(i, s); } } ```</ispells></ispells> -
Tu cherches à stocker quoi exactement ?
-
Je cherche à stocker une liste de sorts de magie que le joueur peux débloquer au fur et à mesure de son aventure dans le jeu.
Une fois qu’il a débloquer le sort il à accès au sort “enfant” à la manière des achievement
-
et les sorts de magie sont quoi ?
Juste des string ou il y aura autre chose ? -
Juste des strings
-
As-tu testé le code que je t’ai envoyé plus haut ? Si ça ne fonctionne pas, je pence que tu peut aussi faire comme se que tu faisais au tout début mais en suppriment ce qui a rapport au TagList et sans oublié, dans le readNBT, de remplacer “String s = tag.getString(“Spells” + i)” par “String s = ((NBTTagCompound)nbt).getString(“Spells” + i)”.
-
Ton code marche niquel, j’arrive juste pas à récupérer ma liste en faisant .getCapabilities().getSpells(), il me retourne une ArrayList vide, je suppose que je dois passer par des packets pour récupérer la liste mais je galère depuis toujours avec ces packets….
-
En effet inutile de faire une liste de TagNbtCompound qui eux même contiennent un string.
On peut directement faire une liste de TagNbtText.Cette exemple :
package com.example.examplemod; import java.util.ArrayList; import java.util.List; import net.minecraft.init.Blocks; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLInitializationEvent; @Mod(modid = ExampleMod.MODID, version = ExampleMod.VERSION) public class ExampleMod { public static final String MODID = "examplemod"; public static final String VERSION = "1.0"; private List <string>strings = new ArrayList<string>(); @EventHandler public void init(FMLInitializationEvent event) { // some example code System.out.println("DIRT BLOCK >> "+Blocks.DIRT.getUnlocalizedName()); strings.add("blabla"); strings.add("mff"); strings.add("fun"); strings.add("minecraft"); strings.add("forge"); NBTBase nbt = writeListToTag(); strings.clear(); readListFromTag(nbt); System.out.println(); } public NBTBase writeListToTag() { NBTTagList tagList = new NBTTagList(); for(String s : this.strings) { tagList.appendTag(new NBTTagString(s)); } return tagList; } public void readListFromTag(NBTBase tag) { NBTTagList tagList = (NBTTagList)tag; for(int i = 0; i < tagList.tagCount(); i++) { this.strings.add(tagList.getStringTagAt(i)); } } }Fonctionne comme il faut (la liste est bien reconstitué à la fin) :
</string></string>