Sauvegarde NBTTagList Player Capabilities
-
final NBTTagList tagList = ((NBTTagCompound)nbt).getTagList(“Spells”, Constants.NBT.TAG_COMPOUND);
Comme ça ? -
Non malheureusement:
java.lang.ClassCastException: net.minecraft.nbt.NBTTagList cannot be cast to net.minecraft.nbt.NBTTagCompound -
Le problème c’est que dans writeNBT, tu retourne comp.getTag(“Spells”); ce qui correspond à un NBTTagList alors que dans readNBT, tu fait comme si tu avais un NBTTaCompound. Il faut donc que tu mette “return comp;” à la place de “return comp.getTag(“Spells”);” (et faire ce que Robin à dit).
-
Vu l’exception NBTBase est une taglist et non un NBTTagCompound.
Donc il faut faire comme ça :
final NBTTagList tagList = ((NBTTagList )nbt).getTagList(“Spells”, Constants.NBT.TAG_COMPOUND); -
The method getTagList(String, int) is undefined for the type NBTTagListJe comprends pas, je vous donne mes sources actuelles:
public class SpellsStorage implements IStorage <ispells>{ @Override public NBTBase writeNBT(Capability <ispells>capability, ISpells instance, EnumFacing side) { final NBTTagCompound comp = 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) { final NBTTagCompound tag = new NBTTagCompound(); tag.setString("Spells" + i, s); tagList.appendTag(tag); } } } comp.setTag("Spells", tagList); return comp; } @Override public void readNBT(Capability <ispells>capability, ISpells instance, EnumFacing side, NBTBase nbt) { final NBTTagList tagList = ((NBTTagList) nbt).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); } } } ```</ispells></ispells></ispells> -
Donc il n’est visiblement pas possible de mettre une tab list dans une tag list.
Il faudrait faire un tag supplémentaire.
-
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 ?