Résolu Crash Entity Extended Properties
-
Salut tout le monde
J’ai crée un player extended entity props mais au moment de get un byte depuis un block via un system.out.println, mon jeu crash :
voici le crash reportFMLIndexedMessageCodec exception caught io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?] at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?] at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?] at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?] at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?] at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?] at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?] at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?] at net.minecraft.client.Minecraft.runTick(Minecraft.java:2141) [Minecraft.class:?] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1028) [Minecraft.class:?] at net.minecraft.client.Minecraft.run(Minecraft.java:951) [Minecraft.class:?] at net.minecraft.client.main.Main.main(Main.java:164) [Main.class:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_71] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_71] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_71] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_71] at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.11.jar:?] at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.11.jar:?] at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:78) [start/:?] at GradleStart.main(GradleStart.java:45) [start/:?] Caused by: java.lang.IndexOutOfBoundsException at io.netty.buffer.EmptyByteBuf.readByte(EmptyByteBuf.java:439) ~[EmptyByteBuf.class:?] at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:69) ~[FMLIndexedMessageToMessageCodec.class:?] at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?] at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?] at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?] … 19 more
Et voici mes class
Mon entity extended props
package mrplaigon.zombarcraftserver.zombarcraftmod.common.extendedentityproperties; import mrplaigon.zombarcraftserver.zombarcraftmod.common.ZombarCraftCommonProxy; import mrplaigon.zombarcraftserver.zombarcraftmod.common.ZombarCraftMod; import mrplaigon.zombarcraftserver.zombarcraftmod.common.packet.PacketKitSecours; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; public class ExtendedEntityPropertiesPlayer implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "ExtPropPlayer"; private final EntityPlayer player; public byte kitSecoursNumber; public ExtendedEntityPropertiesPlayer(EntityPlayer player) { this.player = player; this.kitSecoursNumber = 0; } @Override public void init(Entity entity, World world) { } public static final void register(EntityPlayer player) { player.registerExtendedProperties(ExtendedEntityPropertiesPlayer.EXT_PROP_NAME, new ExtendedEntityPropertiesPlayer(player)); } public static final ExtendedEntityPropertiesPlayer get(EntityPlayer player) { return (ExtendedEntityPropertiesPlayer) player.getExtendedProperties(EXT_PROP_NAME); } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); compound.setTag(EXT_PROP_NAME, properties); properties.setByte("kitSecoursNumber", this.kitSecoursNumber); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); this.kitSecoursNumber = properties.getByte("kitSecoursNumber"); } public final void sync() { PacketKitSecours packetkitsecours = new PacketKitSecours(this.kitSecoursNumber); ZombarCraftMod.network.sendToServer(packetkitsecours); if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; ZombarCraftMod.network.sendTo(packetkitsecours, player1); } } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } public static void saveProxyData(EntityPlayer player) { ExtendedEntityPropertiesPlayer playerData = ExtendedEntityPropertiesPlayer.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); ZombarCraftCommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { ExtendedEntityPropertiesPlayer playerData = ExtendedEntityPropertiesPlayer.get(player); NBTTagCompound savedData = ZombarCraftCommonProxy.getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } public boolean pay(byte amount) { boolean sufficient = amount <= this.kitSecoursNumber; if (sufficient) { this.kitSecoursNumber -= amount; this.sync(); } else { return false; } return sufficient; } public void addkitSecoursNumber(byte amount) { this.kitSecoursNumber += amount; this.sync(); } public byte getkitSecoursNumber() { return this.kitSecoursNumber; } public void setkitSecoursNumber(byte newkitSecoursNumber) { this.kitSecoursNumber = newkitSecoursNumber; this.sync(); } }
La méthode avec le println dedans
public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) { this.breakBlock(world, x, y, z, this, 0); ExtendedEntityPropertiesPlayer props = ExtendedEntityPropertiesPlayer.get(player); if (props.getkitSecoursNumber() == 0) { System.out.println("Squalala, nous sommes partis!"); } else { System.out.println("Pas d'argent ! Je suis triste !"); } }
Et enfin, mes events
package mrplaigon.zombarcraftserver.zombarcraftmod.common.handler; import java.util.Random; import mrplaigon.zombarcraftserver.zombarcraftmod.common.ZombarCraftCommonProxy; import mrplaigon.zombarcraftserver.zombarcraftmod.common.entity.EntityBasicZombar; import mrplaigon.zombarcraftserver.zombarcraftmod.common.entity.EntityStrongZombar; import mrplaigon.zombarcraftserver.zombarcraftmod.common.extendedentityproperties.ExtendedEntityPropertiesPlayer; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class ZombarCraftEventHandlerCommon { Random rand = new Random(); public static ZombarCraftCommonProxy commonProxy; @SubscribeEvent public void onSpawn(EntityJoinWorldEvent event) { if(event.entity instanceof EntityBasicZombar) { int i = rand.nextInt(2); EntityBasicZombar basiczombar = ((EntityBasicZombar)event.entity); if(i == 0) { basiczombar.setHasHeldItem(false); } else { basiczombar.setHasHeldItem(true); } } if(event.entity instanceof EntityStrongZombar) { int i = rand.nextInt(2); EntityStrongZombar strongzombar = ((EntityStrongZombar)event.entity); if(i == 0) { strongzombar.setHasHeldItem(false); } else { strongzombar.setHasHeldItem(true); } } } @SubscribeEvent public void onHitEntity(LivingHurtEvent event) { if(event.entity != null && event.entity instanceof EntityLiving) { EntityLiving entityliving = ((EntityLiving)event.entity); System.out.println("Sa vie est egale a " + entityliving.getHealth()); } } @SubscribeEvent public void onEntityConstructing(EntityConstructing event) { if (event.entity instanceof EntityPlayer && ExtendedEntityPropertiesPlayer.get((EntityPlayer) event.entity) == null) ExtendedEntityPropertiesPlayer.register((EntityPlayer) event.entity); } @SubscribeEvent public void onLivingDeathEvent(LivingDeathEvent event) { if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) { NBTTagCompound playerData = new NBTTagCompound(); ((ExtendedEntityPropertiesPlayer) (event.entity.getExtendedProperties(ExtendedEntityPropertiesPlayer.EXT_PROP_NAME))).saveNBTData(playerData); commonProxy.storeEntityData(((EntityPlayer) event.entity).getDisplayName(), playerData); ExtendedEntityPropertiesPlayer.saveProxyData((EntityPlayer) event.entity); } else { } } @SubscribeEvent public void onEntityJoinWorld(EntityJoinWorldEvent event) { if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) { NBTTagCompound playerData = commonProxy.getEntityData(((EntityPlayer) event.entity).getDisplayName()); if (playerData != null) { ((ExtendedEntityPropertiesPlayer) (event.entity.getExtendedProperties(ExtendedEntityPropertiesPlayer.EXT_PROP_NAME))).loadNBTData(playerData); } ((ExtendedEntityPropertiesPlayer) (event.entity.getExtendedProperties(ExtendedEntityPropertiesPlayer.EXT_PROP_NAME))).sync(); } } }
Merci d’avance
-
Quel est la classe de ton paquet que tu envois ?
Je me trompe peut être mais je pense qu’il y a une erreur ici :
PacketKitSecours packetkitsecours = new PacketKitSecours(this.kitSecoursNumber); ZombarCraftMod.network.sendToServer(packetkitsecours); if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; ZombarCraftMod.network.sendTo(packetkitsecours, player1); }
Tu envois le paquet au serveur à chaque fois, donc si tu lances ton mod côté serveur, il va s’envoyer un paquet à lui-même.
-
Voici la class du packet
package mrplaigon.zombarcraftserver.zombarcraftmod.common.packet; import io.netty.buffer.ByteBuf; import mrplaigon.zombarcraftserver.zombarcraftmod.common.extendedentityproperties.ExtendedEntityPropertiesPlayer; import net.minecraft.entity.player.EntityPlayerMP; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; public class PacketKitSecours implements IMessage { private static byte kitSecoursNumber; public PacketKitSecours() { } public PacketKitSecours(byte kitSecoursNumber) { this.kitSecoursNumber = kitSecoursNumber; } @Override public void fromBytes(ByteBuf buf) { buf.writeByte(kitSecoursNumber); } @Override public void toBytes(ByteBuf buf) { this.kitSecoursNumber = buf.readByte(); } public static class Handler implements IMessageHandler <packetkitsecours, imessage="">{ @Override public IMessage onMessage(PacketKitSecours message, MessageContext ctx) { EntityPlayerMP player = ctx.getServerHandler().playerEntity; ExtendedEntityPropertiesPlayer props = ExtendedEntityPropertiesPlayer.get(player); props.kitSecoursNumber = kitSecoursNumber; return null; } } }
Et pour ce que tu m’as proposé, quel code aurais-tu mis à la place ? Car pour l’instant je ne lançais mon mod qu’en solo pas en serveur.
Mon packet est bien déclaré dans ma main class il a pour SIDE server, et un bon discriminator, et pour ceux qui penserait que cela viendrait du SIDE j’ai mis client pour voir toujours pareil.</packetkitsecours,> -
fromBytes = depuis le bytes -> read
toBytes = vers les bytes -> writeTu as inversé les deux.
-
Ok merci tout marche