Résolu WorldSavedData crash
-
Bonsoir,
Quand je save avec m’a classe WorldSavedData j’ai un crash dans mes logs:
Crash “création de la clé”
[20:44:27] [Server thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.entity.player.PlayerInteractEvent@cd2de0: java.lang.NullPointerException at com.CSC.net.Events.Events.onInteract(Events.java:91) ~[Events.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_9_Events_onInteract_PlayerInteractEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?] at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEventFactory.java:119) [ForgeEventFactory.class:?] at net.minecraft.server.management.ItemInWorldManager.activateBlockOrUseItem(ItemInWorldManager.java:452) [ItemInWorldManager.class:?] at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:624) [NetHandlerPlayServer.class:?] at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:67) [C08PacketPlayerBlockPlacement.class:?] at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:114) [C08PacketPlayerBlockPlacement.class:?] at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:24) [PacketThreadUtil$1.class:?] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45] at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:709) [FMLCommonHandler.class:?] at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:669) [MinecraftServer.class:?] at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:171) [IntegratedServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:540) [MinecraftServer.class:?] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45] [20:44:27] [Server thread/ERROR] [FML]: Index: 1 Listeners: [20:44:27] [Server thread/ERROR] [FML]: 0: NORMAL [20:44:27] [Server thread/ERROR] [FML]: 1: ASM: com.CSC.net.Events.Events@6d8b3b onInteract(Lnet/minecraftforge/event/entity/player/PlayerInteractEvent;)V [20:44:27] [Server thread/ERROR] [FML]: 2: ASM: com.CSC.net.Events.Events@6d8b3b onInteract2(Lnet/minecraftforge/event/entity/player/PlayerInteractEvent;)V [20:44:27] [Server thread/FATAL] [FML]: Exception caught executing FutureTask: java.util.concurrent.ExecutionException: java.lang.NullPointerException java.util.concurrent.ExecutionException: java.lang.NullPointerException at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[?:1.8.0_45] at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[?:1.8.0_45] at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:710) [FMLCommonHandler.class:?] at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:669) [MinecraftServer.class:?] at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:171) [IntegratedServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:540) [MinecraftServer.class:?] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45] Caused by: java.lang.NullPointerException at com.CSC.net.Events.Events.onInteract(Events.java:91) ~[Events.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_9_Events_onInteract_PlayerInteractEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) ~[EventBus.class:?] at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEventFactory.java:119) ~[ForgeEventFactory.class:?] at net.minecraft.server.management.ItemInWorldManager.activateBlockOrUseItem(ItemInWorldManager.java:452) ~[ItemInWorldManager.class:?] at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:624) ~[NetHandlerPlayServer.class:?] at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:67) ~[C08PacketPlayerBlockPlacement.class:?] at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:114) ~[C08PacketPlayerBlockPlacement.class:?] at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:24) ~[PacketThreadUtil$1.class:?] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_45] at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_45] at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:709) ~[FMLCommonHandler.class:?] … 5 more [20:44:27] [Client thread/INFO]: [CHAT] §aTu as créé une clé
Event “créé une clé”:
public static String Key = "key"; public static ItemStack items; public static Position p1; public static final List <position>doorsPositions = Lists.newArrayList(); @SubscribeEvent public void onInteract(PlayerInteractEvent e) { if(!e.world.isRemote) { ItemStack stack = e.entityPlayer.getHeldItem(); if(stack != null && stack.getItem() == CSCItems.clef && e.action == Action.RIGHT_CLICK_BLOCK) { BlockPos pos = e.pos; IBlockState state = e.world.getBlockState(pos); items = stack; if(state.getBlock() == Blocks.iron_door) { /** * On se base sur la partie haute de la porte, en effet c'est cette partie qui contient * l'information pour savoir si la porte est ouverte ou non. * Ici si c'est la partie basse on ajoute 1 à la coordonnée Y et on récupère donc l'état * de la partie haute. */ if(state.getValue(BlockDoor.HALF) == BlockDoor.EnumDoorHalf.LOWER) { pos = pos.up(); state = e.world.getBlockState(pos); } /** * Si c'est une nouvelle clé on écrit les coordonnée de la porte ainsi que l'id * de la dimension dans ses tag NBT. */ if(!stack.hasTagCompound()) { Position p = new Position(pos, e.world.provider.getDimensionId()); p1 = p; if(!doorsPositions.contains(p)) { NBTTagCompound nbt = new NBTTagCompound(); nbt.setIntArray("doorPos", new int[]{e.world.provider.getDimensionId(), pos.getX(), pos.getY(), pos.getZ()}); stack.setTagCompound(nbt); doorsPositions.add(new Position(pos.getX(), pos.getY(), pos.getZ(), e.world.provider.getDimensionId())); e.entityPlayer.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.GREEN + "Tu as créé une clé")); KeySave.instance.markDirty(); // ligne 90 de l'event } else { e.entityPlayer.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.RED + "Une clé existe déjà pour cette porte")); } } /** * Ici tu peut enlever le else ce qui aura pour effet d'ouvrir la porte lors de la création * de la clé car avec le else on doit créer la porte et refaire clique droit pour l'ouvrir. */ else { int[] doorPos = stack.getTagCompound().getIntArray("doorPos"); if(doorPos[0] == e.world.provider.getDimensionId() && doorPos[1] == pos.getX() && doorPos[2] == pos.getY() && doorPos[3] == pos.getZ()) { Boolean opened = (Boolean) (state.getValue(BlockDoor.POWERED)); /** * Il y a d'autres solutions pour garder la porte ouverte, j'avais essayé de changé le state en POWERED = true * à chaque tick mais en changeant l'état ça update le bloc remettant POWERED à false * donc il faut obligatoirement utiliser la redstone, par contre tu peux faire que la * porte doit être posée sur un certain bloc (que tu va créer) et qui alimentera ou pas la * porte en énergie, ça sera plus propre que la torche. C'est à toi de voir. */ if(opened) { e.world.setBlockToAir(pos.down(3)); } else { e.world.setBlockState(pos.down(3), Blocks.redstone_torch.getDefaultState()); } e.entityPlayer.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.GREEN + "Tu as" + (opened ? " fermé " : " ouvert ") + "la porte")); } else { e.entityPlayer.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.RED + "Ceci n'est pas la bonne clé")); } } } } } }
Event “suppression de la clé”
@SubscribeEvent public void onBlockBreak(BreakEvent event) { /** * On supprime la porte de la liste si on la casse. */ if(event.state.getBlock() == Blocks.iron_door) { BlockPos pos = event.pos; if(event.state.getValue(BlockDoor.HALF) == BlockDoor.EnumDoorHalf.LOWER) { pos = pos.up(); } Position p = new Position(pos, event.world.provider.getDimensionId()); doorsPositions.remove(p); KeySave.instance.markDirty();// ligne 138 } }
crash aussi quand je casse la porte " quand la clé est supprimer ":
[20:44:52] [Server thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.world.BlockEvent$BreakEvent@7eccdf: java.lang.NullPointerException at com.CSC.net.Events.Events.onBlockBreak(Events.java:138) ~[Events.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_10_Events_onBlockBreak_BreakEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?] at net.minecraftforge.common.ForgeHooks.onBlockBreakEvent(ForgeHooks.java:532) [ForgeHooks.class:?] at net.minecraft.server.management.ItemInWorldManager.tryHarvestBlock(ItemInWorldManager.java:313) [ItemInWorldManager.class:?] at net.minecraft.server.management.ItemInWorldManager.onBlockClicked(ItemInWorldManager.java:174) [ItemInWorldManager.class:?] at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:552) [NetHandlerPlayServer.class:?] at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:53) [C07PacketPlayerDigging.class:?] at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:76) [C07PacketPlayerDigging.class:?] at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:24) [PacketThreadUtil$1.class:?] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45] at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:709) [FMLCommonHandler.class:?] at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:669) [MinecraftServer.class:?] at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:171) [IntegratedServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:540) [MinecraftServer.class:?] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45] [20:44:52] [Server thread/ERROR] [FML]: Index: 1 Listeners: [20:44:52] [Server thread/ERROR] [FML]: 0: NORMAL [20:44:52] [Server thread/ERROR] [FML]: 1: ASM: com.CSC.net.Events.Events@6d8b3b onBlockBreak(Lnet/minecraftforge/event/world/BlockEvent$BreakEvent;)V [20:44:52] [Server thread/FATAL] [FML]: Exception caught executing FutureTask: java.util.concurrent.ExecutionException: java.lang.NullPointerException java.util.concurrent.ExecutionException: java.lang.NullPointerException at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[?:1.8.0_45] at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[?:1.8.0_45] at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:710) [FMLCommonHandler.class:?] at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:669) [MinecraftServer.class:?] at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:171) [IntegratedServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:540) [MinecraftServer.class:?] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45] Caused by: java.lang.NullPointerException at com.CSC.net.Events.Events.onBlockBreak(Events.java:138) ~[Events.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_10_Events_onBlockBreak_BreakEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) ~[EventBus.class:?] at net.minecraftforge.common.ForgeHooks.onBlockBreakEvent(ForgeHooks.java:532) ~[ForgeHooks.class:?] at net.minecraft.server.management.ItemInWorldManager.tryHarvestBlock(ItemInWorldManager.java:313) ~[ItemInWorldManager.class:?] at net.minecraft.server.management.ItemInWorldManager.onBlockClicked(ItemInWorldManager.java:174) ~[ItemInWorldManager.class:?] at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:552) ~[NetHandlerPlayServer.class:?] at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:53) ~[C07PacketPlayerDigging.class:?] at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:76) ~[C07PacketPlayerDigging.class:?] at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:24) ~[PacketThreadUtil$1.class:?] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_45] at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_45] at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:709) ~[FMLCommonHandler.class:?] … 5 more
Classe WorldSavedData:
package com.CSC.net.save; import java.util.Iterator; import java.util.UUID; import java.util.Map.Entry; import javafx.geometry.Pos; import com.CSC.net.Events.Events; import com.CSC.net.Events.Events.Position; import com.CSC.net.properties.EnumRank; import com.CSC.net.proxy.ServerProxy; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.world.World; import net.minecraft.world.WorldSavedData; import net.minecraft.world.storage.MapStorage; import net.minecraftforge.common.DimensionManager; public class KeySave extends WorldSavedData { public static KeySave instance; /** /* Ce constructeur est obligatoire */ public static KeySave load(World w) { if(DimensionManager.getWorlds().length < 1) return null; MapStorage storage = DimensionManager.getWorlds()[0].getMapStorage(); //On récupère la mapStorage de la dimension final String KEY = Events.Key; // Si vous avez choisis de faire une fonction static String. Quoiqu'il en soit, la variable KEY que nous utiliserons ici représente votre clé. Si vous utilisez un attribut static, utilisez le directement. KeySave result = (KeySave)storage.loadData(KeySave.class, KEY);//On charge nos données if (result == null) { //Si nos données n'existe pas, … result = new KeySave(KEY);// Alors on les crée ... storage.setData(KEY, result);// Et on les ajoutes à la mapStorage. } return result; //On retourne notre objet qui représente les données a sauvegarder } public KeySave(String key) { super(key); instance = this; } @Override public void readFromNBT(NBTTagCompound compound) { NBTTagList keyList= (NBTTagList)compound.getTag("keyList"); for(int i = 0; i < keyList.tagCount(); i++) { NBTTagCompound keyTag = keyList.getCompoundTagAt(i); Events.doorsPositions.add(new Position(keyTag.getInteger("x"), keyTag.getInteger("y"), keyTag.getInteger("z"), keyTag.getInteger("dimension"))); } } @Override public void writeToNBT(NBTTagCompound compound) { NBTTagList keyList = new NBTTagList(); Iterator <position>iterator = Events.doorsPositions.iterator(); Position pos; NBTTagCompound keyTag; while(iterator.hasNext()) { pos = iterator.next(); keyTag = new NBTTagCompound(); keyTag.setInteger("x", pos.getX()); keyTag.setInteger("y", pos.getY()); keyTag.setInteger("z", pos.getZ()); keyTag.setInteger("dimension", pos.dimensionId); keyList.appendTag(keyTag); } compound.setTag("keyList", keyList); } }
@EventHandler public void onServerStarting(FMLServerStartingEvent event) { event.registerServerCommand(new NameCommand()); if(KeySave.instance != null) { KeySave.load(event.getServer().getEntityWorld()); } } ```</position></position>
-
at com.CSC.net.Events.Events.onInteract(Events.java:91) ~[Events.class:?]
à quoi correspond la ligne 91 ?at com.CSC.net.Events.Events.onBlockBreak(Events.java:138) ~[Events.class:?]
idem, quelle est la ligne 138 ? -
@‘robin4002’:
at com.CSC.net.Events.Events.onInteract(Events.java:91) ~[Events.class:?]
à quoi correspond la ligne 91 ?at com.CSC.net.Events.Events.onBlockBreak(Events.java:138) ~[Events.class:?]
idem, quelle est la ligne 138 ?C’est écrit dans les events.
Ligne 91 correspond au markdirty de la création de la clé.
Ligne 138 correspond au markdirty de la suppression de la clé.
-
L’instance de ta classe est donc null, normal si tu ne l’initialise jamais.
-
“KeySave.instance” est null, je ne m’y connais pas en WorldSavedData mais je suppose qu’il faudrait mettre “public static KeySave instance = new KeySave();” au lieu de “public static KeySave instance;” dans la class KeySave.
-
@‘robin4002’:
L’instance de ta classe est donc null, normal si tu ne l’initialise jamais.
Ouais c’est bon j’ai trouver le problème c’est tout con…
j’avais:if(KeySave.instance != null) { KeySave.load(event.getServer().getEntityWorld()); }
au lieux de:
if(KeySave.instance == null) { KeySave.load(event.getServer().getEntityWorld()); }
merci de votre aide
-
Ton code est sujet a erreur.
Ta variable “instance” n’est pas fiable, puisque tu initialise ta map storage en fonction du monde et cette initialisation écrase le contenu de ta variable si c’est la première fois que tu charge ton WorldSavedData ou laisse une mauvaise référence dans ta variable dans le cas contraire.
Supprime ta variable instance, et n’utilise que ta fonction load pour récupérer ton WorldSavedData.