Extended Entity Properties
-
cela ma l’air correct et ta méthode “sync”, peut tu l’a donner ?
-
@‘mindany2’:
public class ExtendedEntityPropFr implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "ExtPropFr"; private final EntityPlayer player; public boolean DoubleJump; public ExtendedEntityPropFr(EntityPlayer player) { this.player = player; this.DoubleJump = false; } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setBoolean("DoubleJump", this.DoubleJump); compound.setTag(EXT_PROP_NAME, properties); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); this.DoubleJump = properties.getBoolean("DoubleJump"); } @Override public void init(Entity entity, World world) { // TODO Auto-generated method stub } public static final void register(EntityPlayer player) { player.registerExtendedProperties(ExtendedEntityPropFr.EXT_PROP_NAME, new ExtendedEntityPropFr(player)); } public static final ExtendedEntityPropFr get(EntityPlayer player) { return (ExtendedEntityPropFr) player.getExtendedProperties(EXT_PROP_NAME); } public final void sync() { PacketFr packetFr = new PacketFr(this.DoubleJump); Fr_mod.rcModPacketHandler.sendToServer(packetFr); if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; //Ici, même chose que précédemment, sauf que le packet est envoyé au player. Fr_mod.rcModPacketHandler.sendTo(packetFr, player1); } } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } public static void saveProxyData(EntityPlayer player) { ExtendedEntityPropFr playerData = ExtendedEntityPropFr.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); CommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { ExtendedEntityPropFr playerData = ExtendedEntityPropFr .get(player); NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } public boolean getDoubleJump() { return this.DoubleJump; } public void setDoubleJump(boolean newDoubleJump) { this.DoubleJump = newDoubleJump; this.sync(); } }
Voigna ^^
-
Je ne vois rien d’étrange tout a l’air correct dans le codage.
Après je pense qu’il faut attendre des codeurs un peu plus expérimenter dans le codage minecraft, car moi actuellement je suis bloquer dans le “onPlayerTick” il veut pas me le prendre en compte …
donc je n’en suis pas a ton niveau ou tu cherche à enregistrer les valeurs mais ton problème viens d’ici, je pense au sync() principalement, du au test que l’on a fait avec les string out mais c’est peut être pas ta faute c’est peut être un problème dans le tuto.
Je n’est jamais utiliser cette méthode encore… je découvre
-
Ok j’attendrais que quelqu’un d’autre passe
Niveau onPlayerTick t’a bien mis
" @SideOnly(Side.CLIENT)
@SubscribeEvent" -
ouep je suis en train de crée une réponse sur mon poste et je te donne le lien sa sera mieux pour éviter de mélanger les problèmes
Voilà si tu veux jeter un œil au codage où tenter de comprendre mon problème je te donne “le lien” ou j’ai poster mon problème
-
Bonjour, j’ai suivi le tutoriel a la lettre, et pourtant j’ai une erreur dans la classe du common proxy
public class CommonProxy { private static final Map <string, nbttagcompound="">extendedEntityData = new HashMap<string, nbttagcompound="">(); public void registerRender () { System.out.println("Méthode du serveur"); } public static void storeEntityData(String saveKey, NBTTagCompound savedData) { extendedEntityData.put(name, compound); } public static NBTTagCompound getEntityData(String saveKey) { return extendedEntityData.remove(name); } }
Chez moi j’ai une erreur sur “name” et “compound”. Si je les déclare dans la classe du CommonProxy, le jeu plante quand je fais clic droit avec l’item.
Quelqu’un aurait-il une idée ?</string,></string,> -
public static void storeEntityData(String name, NBTTagCompound compound) { extendedEntityData.put(name, compound); }
-
Merci beaucoup ! C’était surement une erreur de recopiage (je retape tout a la main pour mémorisé ).
Mais même si j’arrive maintenant a compilé, j’ai un autre crash, lorsque je fait le clic droit avec l’item pour testé.
Voici les logs,mais je ne sais pas quel partie du code il faudrait que je poste.
A priori, ça va vient plutôt de l’item ou de l’extented entity ?net.minecraft.util.ReportedException: Ticking memory connection
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:198) ~[NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:720) ~[MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:608) ~[MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) ~[IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:746) [MinecraftServer$2.class:?]
Caused by: java.lang.NullPointerException
at com.minefus.principal.ItemExtentedEntity.onItemRightClick(ItemExtentedEntity.java:25) ~[ItemExtentedEntity.class:?]
at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:165) ~[ItemStack.class:?]
at net.minecraft.server.management.ItemInWorldManager.tryUseItem(ItemInWorldManager.java:341) ~[ItemInWorldManager.class:?]
at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:572) ~[NetHandlerPlayServer.class:?]
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:74) ~[C08PacketPlayerBlockPlacement.class:?]
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:122) ~[C08PacketPlayerBlockPlacement.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:232) ~[NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) ~[NetworkSystem.class:?]
… 5 more
[16:19:29] [Server thread/ERROR]: This crash report has been saved to: D:\Minefus Projet\MCP 1.7.2\eclipse.\crash-reports\crash-2014-07-10_16.19.29-server.txt
[16:19:29] [Server thread/INFO]: Stopping server
[16:19:29] [Server thread/INFO]: Saving players
[16:19:29] [Server thread/INFO]: Saving worlds
[16:19:29] [Server thread/INFO]: Saving chunks for level ‘New World’/Overworld
–-- Minecraft Crash Report ----
// My bad.Time: 10/07/14 16:19
Description: Ticking memory connectionjava.lang.NullPointerException: Ticking memory connection
at com.minefus.principal.ItemExtentedEntity.onItemRightClick(ItemExtentedEntity.java:25)
at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:165)
at net.minecraft.server.management.ItemInWorldManager.tryUseItem(ItemInWorldManager.java:341)
at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:572)
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:74)
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:122)
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:232)
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:720)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:608)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:746)A detailed walkthrough of the error, its code path and all known details is as follows:
– Head –
Stacktrace:
at com.minefus.principal.ItemExtentedEntity.onItemRightClick(ItemExtentedEntity.java:25)
at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:165)
at net.minecraft.server.management.ItemInWorldManager.tryUseItem(ItemInWorldManager.java:341)
at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:572)
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:74)
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:122)
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:232)– Ticking connection –
Details:
Connection: net.minecraft.network.NetworkManager@2b4beaa5
Stacktrace:
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:720)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:608)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:746) -
at com.minefus.principal.ItemExtentedEntity.onItemRightClick(ItemExtentedEntity.jav?a:25)
Ligne 25 de ton item ? (+ toutes la classes). -
Voici les 3 classes en rapport avec l’item (l’extented entity, l’item et les paquets)
Merci de m’accorder du temps !package com.minefus.principal; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.world.World; public class ItemExtentedEntity extends Item { public ItemExtentedEntity() { this.setCreativeTab(CreativeTabs.tabTools); } @Override public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player) { if (!world.isRemote) { PlayerExtendedEntity props = PlayerExtendedEntity.get(player); if (props.att(6)) { System.out .println("Squalala, nous sommes partis!"); } else { System.out .println("Pas d'argent ! Je suis triste !"); } } return itemstack; } }
package com.minefus.principal; import com.minefus.commonproxy.CommonProxy; 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.network.Packet; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; public class PlayerExtendedEntity implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "ExtendedPlayer" ; private final EntityPlayer player; public long experience ; public short classe ; public short pointsort ; public short pointniveau ; public short niveau; public PlayerExtendedEntity (EntityPlayer player) { this.player = player; this.experience = 0 ; this.classe = 10 ; this.pointsort = 0 ; this.pointniveau = 0 ; this.niveau = 1 ; } public static final void register(EntityPlayer player) { player.registerExtendedProperties(PlayerExtendedEntity.EXT_PROP_NAME, new PlayerExtendedEntity(player)); } public static final PlayerExtendedEntity get (EntityPlayer player) { return (PlayerExtendedEntity) player.getExtendedProperties(EXT_PROP_NAME);} public final void sync() { PaquetPlayer paquetPlayer = new PaquetPlayer ( this.experience,this.classe,this.pointsort,this.pointniveau,this.niveau ); if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; } } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setLong("Experience",this.experience) ; properties.setShort("Classe",this.classe) ; properties.setShort("PointSort",this.pointsort) ; properties.setShort("PointNiveau",this.pointniveau) ; properties.setShort("Niveau",this.niveau) ; } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound .getTag(EXT_PROP_NAME); this.experience = properties.getLong("Experience") ; this.classe = properties.getShort("Classe") ; this.pointsort = properties.getShort("PointSort") ; this.pointniveau = properties.getShort("PointNiveau") ; this.niveau = properties.getShort("Niveau") ; } public static void saveProxyData(EntityPlayer player) { PlayerExtendedEntity playerData = PlayerExtendedEntity.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); CommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { PlayerExtendedEntity playerData = PlayerExtendedEntity.get(player); NBTTagCompound savedData = CommonProxy .getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } @Override public void init(Entity entity, World world) { // TODO Auto-generated method stub } public boolean att( long amount) { boolean sufficient = amount <= this.classe; if (sufficient) { this.classe-= amount; this.sync(); } else { return false; } return sufficient; } }
package com.minefus.principal; import io.netty.channel.ChannelHandlerContext; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; import fr.minecraftforgefrance.ffmtlibs.network.AbstractPacket; public class PaquetPlayer extends AbstractPacket { private short classe, pointsort, pointniveau, niveau ; private long experience ; public PaquetPlayer () {} public PaquetPlayer(long experience ,short classe ,short pointsort ,short pointniveau,short niveau) { this.experience = experience ; this.classe = classe ; this.pointsort = pointsort ; this.pointniveau = pointniveau ; this.niveau = niveau ; } @Override public void encodeInto(ChannelHandlerContext ctx, PacketBuffer buffer) throws IOException { buffer.writeLong(experience); buffer.writeShort(classe); buffer.writeShort(pointsort); buffer.writeShort(pointniveau); buffer.writeShort(niveau); } @Override public void decodeInto(ChannelHandlerContext ctx, PacketBuffer buffer) throws IOException { this.experience = buffer.readLong(); this.classe= buffer.readShort(); this.pointsort = buffer.readShort(); this.pointniveau = buffer.readShort(); this.niveau = buffer.readShort(); } @Override public void handleClientSide(EntityPlayer player) { PlayerExtendedEntity props = PlayerExtendedEntity .get(player); props.experience = this.experience; props.classe = this.classe; props.pointsort = this.pointsort; props.pointniveau = this.pointniveau; props.niveau = this.niveau; } @Override public void handleServerSide(EntityPlayer player) { PlayerExtendedEntity props = PlayerExtendedEntity .get(player); props.experience = this.experience; props.classe = this.classe; props.pointsort = this.pointsort; props.pointniveau = this.pointniveau; props.niveau = this.niveau; } }
-
props semble être null, tu n’as pas enregistrés ton custom entity properties ou les event qui le fond.
-
A oui, enfaite j’avais oublier de créer la classe EventHanddler… ^^ Forcement ça marche un peu moins bien sans.
Par contre, j’ai exactement les même erreurs dans les logs, même ligne, même classe, donc je pence que ça doit venir de autre part. Vu que j’ai fait quelques corrections de méthodes qui mettaient des erreurs partout a cause de la classe Event qui manquait et que j’ai ajouté une jauge de mana, je te repost tousa :
Donc l’Extented Enity Properties :
package com.minefus.principal; import com.minefus.commonproxy.CommonProxy; 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.network.Packet; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; public class PlayerExtendedEntity implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "ExtendedPlayer" ; private final EntityPlayer player; public short manaMax ; public short mana ; public long experience ; public short classe ; public short pointsort ; public short pointniveau ; public short niveau; public PlayerExtendedEntity (EntityPlayer player) { this.player = player; this.mana = 12; this.manaMax = 12; this.experience = 0 ; this.classe = 0 ; this.pointsort = 0 ; this.pointniveau = 0 ; this.niveau = 1 ; } public static final void register(EntityPlayer player) { player.registerExtendedProperties(PlayerExtendedEntity.EXT_PROP_NAME, new PlayerExtendedEntity(player)); } public static final PlayerExtendedEntity get (EntityPlayer player) { return (PlayerExtendedEntity) player.getExtendedProperties(EXT_PROP_NAME);} public final void sync() { PaquetPlayer paquetPlayer = new PaquetPlayer (this.mana, this.manaMax, this.experience,this.classe,this.pointsort,this.pointniveau,this.niveau ); if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; } } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setShort("Mana",this.mana) ; properties.setShort("ManaMax",this.manaMax) ; properties.setLong("Experience",this.experience) ; properties.setShort("Classe",this.classe) ; properties.setShort("PointSort",this.pointsort) ; properties.setShort("PointNiveau",this.pointniveau) ; properties.setShort("Niveau",this.niveau) ; } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound .getTag(EXT_PROP_NAME); this.mana = properties.getShort("Mana") ; this.manaMax = properties.getShort("ManaMax") ; this.experience = properties.getLong("Experience") ; this.classe = properties.getShort("Classe") ; this.pointsort = properties.getShort("PointSort") ; this.pointniveau = properties.getShort("PointNiveau") ; this.niveau = properties.getShort("Niveau") ; } public static void saveProxyData(EntityPlayer player) { PlayerExtendedEntity playerData = PlayerExtendedEntity.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); CommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { PlayerExtendedEntity playerData = PlayerExtendedEntity.get(player); NBTTagCompound savedData = CommonProxy .getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } @Override public void init(Entity entity, World world) { // TODO Auto-generated method stub } public boolean att( long amount) { boolean sufficient = amount <= this.mana; if (sufficient) { this.mana -= amount; this.sync(); } else { return false; } return sufficient; } }
L’Item :
package com.minefus.principal; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.world.World; public class ItemExtentedEntity extends Item { public ItemExtentedEntity() { this.setCreativeTab(CreativeTabs.tabTools); } @Override public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player) { if (!world.isRemote) { PlayerExtendedEntity props = PlayerExtendedEntity.get(player); if (props.att(6)) { System.out .println("Squalala, nous sommes partis!"); } else { System.out .println("Pas d'argent ! Je suis triste !"); } } return itemstack; } }
Les Events pour l’entity:
package com.minefus.principal; import com.minefus.commonproxy.CommonProxy; 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 cpw.mods.fml.common.eventhandler.SubscribeEvent; public class EntityEventHandler { @SubscribeEvent public void onEntityConstructing(EntityConstructing event) { if (event.entity instanceof EntityPlayer && PlayerExtendedEntity.get((EntityPlayer) event.entity) == null) PlayerExtendedEntity.register((EntityPlayer) event.entity); System.out.println("une entité a rejoint le monde"); } @SubscribeEvent public void onLivingDeathEvent(LivingDeathEvent event) { if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) { NBTTagCompound playerData = new NBTTagCompound(); ((PlayerExtendedEntity) (event.entity.getExtendedProperties(PlayerExtendedEntity.EXT_PROP_NAME))).saveNBTData(playerData); CommonProxy.storeEntityData(((EntityPlayer) event.entity).getDisplayName(), playerData); PlayerExtendedEntity.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) { ((PlayerExtendedEntity) (event.entity.getExtendedProperties(PlayerExtendedEntity.EXT_PROP_NAME))).loadNBTData(playerData); } ((PlayerExtendedEntity) (event.entity.getExtendedProperties(PlayerExtendedEntity.EXT_PROP_NAME))).sync(); } } }
PaquetPlayer :
package com.minefus.principal; import io.netty.channel.ChannelHandlerContext; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; import fr.minecraftforgefrance.ffmtlibs.network.AbstractPacket; public class PaquetPlayer extends AbstractPacket { private short mana, manaMax, classe, pointsort, pointniveau, niveau ; private long experience ; public PaquetPlayer () {} public PaquetPlayer(short mana, short manaMax ,long experience ,short classe ,short pointsort ,short pointniveau,short niveau) { this.mana = mana; this.manaMax = manaMax; this.experience = experience ; this.classe = classe ; this.pointsort = pointsort ; this.pointniveau = pointniveau ; this.niveau = niveau ; } @Override public void encodeInto(ChannelHandlerContext ctx, PacketBuffer buffer) throws IOException { buffer.writeLong(experience); buffer.writeShort(mana); buffer.writeShort(manaMax); buffer.writeShort(classe); buffer.writeShort(pointsort); buffer.writeShort(pointniveau); buffer.writeShort(niveau); } @Override public void decodeInto(ChannelHandlerContext ctx, PacketBuffer buffer) throws IOException { this.mana = buffer.readShort(); this.manaMax = buffer.readShort(); this.experience = buffer.readLong(); this.classe= buffer.readShort(); this.pointsort = buffer.readShort(); this.pointniveau = buffer.readShort(); this.niveau = buffer.readShort(); } @Override public void handleClientSide(EntityPlayer player) { PlayerExtendedEntity props = PlayerExtendedEntity .get(player); props.mana = this.mana; props.manaMax = this.manaMax; props.experience = this.experience; props.classe = this.classe; props.pointsort = this.pointsort; props.pointniveau = this.pointniveau; props.niveau = this.niveau; } @Override public void handleServerSide(EntityPlayer player) { PlayerExtendedEntity props = PlayerExtendedEntity .get(player); props.mana = this.mana; props.manaMax = this.manaMax; props.experience = this.experience; props.classe = this.classe; props.pointsort = this.pointsort; props.pointniveau = this.pointniveau; props.niveau = this.niveau; } }
Au passage, comment tu fait pour savoir que props est nul ?
-
Car l’erreur est un NullPointerException, qu’il te donne la ligne et qu’il n’y a pas d’autre objet qui peut être null sur cette ligne.
Tu as enregistrés la classe d’event avec MinecraftForge.EVENT_BUS.register(new EntityEventHandler()) dans ta classe principale ? -
-
FMLCommonHandler.instance().bus().register(new EntityEventHandler()); n’est pas nécessaire ici, c’est pour les event de fml, or tu n’as pas d’event fml dans la classe
-
A ok, j’avais inversé les 2, je ne savais pas lequel enlevé, voila c’est dit .
-
Salut ,
J’ai toujours un problème avec les Extended Entity Properties, j’ai revu plusieurs fois mon code mais les packets n’ont pas l’air de se transmettre peut importe ce que je fais les données restent toujours à 0
Si vous avez une petite idée ^^ :
PacketSonic :public class PacketSonic extends FFMTPacket { public int Descendre; public boolean DoubleJump; public long Ring; public PacketSonic(){ } public PacketSonic(int Descendre, boolean DoubleJump, long Ring){ this.Descendre = Descendre; this.DoubleJump = DoubleJump; this.Ring = Ring; } @Override public void writeData(ByteBuf buffer) throws IOException { buffer.writeInt(Descendre); buffer.writeBoolean(DoubleJump); buffer.writeLong(Ring); } @Override public void readData(ByteBuf buffer) { this.Descendre = buffer.readInt(); this.DoubleJump = buffer.readBoolean(); this.Ring = buffer.readLong(); } @Override public void handleClientSide(EntityPlayer player) { ExtendedEntityPropSonic props = ExtendedEntityPropSonic .get(player); props.Descendre = this.Descendre; props.DoubleJump = this.DoubleJump; props.Ring = this.Ring; } @Override public void handleServerSide(EntityPlayer player) { ExtendedEntityPropSonic props = ExtendedEntityPropSonic .get(player); props.Descendre = this.Descendre; props.DoubleJump = this.DoubleJump; props.Ring = this.Ring; } }
ExtendedEntityPropSonic :
public final static String EXT_PROP_NAME = "ExtPropSonic"; private final EntityPlayer player; public int Descendre; public boolean DoubleJump; public long Ring; public ExtendedEntityPropSonic(EntityPlayer player) { this.player = player; this.Descendre = 0; this.DoubleJump = false; this.Ring = 0; } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setInteger("Descendre", this.Descendre); properties.setBoolean("DoubleJump", this.DoubleJump); properties.setLong("Ring", this.Ring); compound.setTag(EXT_PROP_NAME, properties); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); this.Descendre = properties.getInteger("Descendre"); this.DoubleJump = properties.getBoolean("DoubleJump"); this.Ring = properties.getLong("Ring"); } @Override public void init(Entity entity, World world) { // TODO Auto-generated method stub } public static final void register(EntityPlayer player) { player.registerExtendedProperties(ExtendedEntityPropSonic.EXT_PROP_NAME, new ExtendedEntityPropSonic(player)); } public static final ExtendedEntityPropSonic get(EntityPlayer player) { return (ExtendedEntityPropSonic) player.getExtendedProperties(EXT_PROP_NAME); } public final void sync() { PacketSonic packetSonic = new PacketSonic(this.Descendre, this.DoubleJump, this.Ring); Sonic_mod.rcModPacketHandler.sendToServer(packetSonic); if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; Sonic_mod.rcModPacketHandler.sendTo(packetSonic, player1); } } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } public static void saveProxyData(EntityPlayer player) { ExtendedEntityPropSonic playerData = ExtendedEntityPropSonic.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); CommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { ExtendedEntityPropSonic playerData = ExtendedEntityPropSonic.get(player); NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } public long getRing() { return this.Ring; } public void setRing(long newRing) { this.Ring = newRing; this.sync(); } public int getDescendre() { return this.Descendre; } public void setDescendre(int newDescendre) { this.Descendre = newDescendre; this.sync(); } public boolean getDoubleJump() { return this.DoubleJump; } public void setDoubleJump(boolean newDoubleJump) { this.DoubleJump = newDoubleJump; this.sync(); }
Une partie de ma classe event :
/** Register data*/ @SubscribeEvent public void onEntityConstructing(EntityConstructing event) { if (event.entity instanceof EntityPlayer && ExtendedEntityPropSonic.get((EntityPlayer) event.entity) == null) { ExtendedEntityPropSonic.register((EntityPlayer) event.entity); System.out.println("une entité a rejoint le monde"); } } @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) { ((ExtendedEntityPropSonic) (event.entity .getExtendedProperties(ExtendedEntityPropSonic.EXT_PROP_NAME))) .loadNBTData(playerData); } ((ExtendedEntityPropSonic) (event.entity .getExtendedProperties(ExtendedEntityPropSonic.EXT_PROP_NAME))) .sync(); } } }
Je pense avoir tout mis
Merci d’avance ^^’ -
Bonjour,
Alors voilà mon problème : je n’arrive pas à synchroniser le server et le client pour mon ExtendedProperties : sur le client la propriété se remet à 0 dès qu’on charge le monde, et par contre sur le server pas de soucis ça sauvegarde bien :
:::
Voilà ce que j’obtiens quand je charge un monde et que je demande la valeur de stat :http://www.casimages.com/i/141029015100755129.png
:::Je vous mets les classes de mon mod :
:::Classe principale :
package mc_plus.common; import mc_plus.network.PacketPipeline; import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraftforge.common.MinecraftForge; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.registry.GameRegistry; @Mod(modid = "mc_plus", name = "Mc Plus", version = "1.0.0") public class Mc_Plus { @Instance("mc_plus") public static Mc_Plus instance; @SidedProxy(clientSide = "mc_plus.client.ClientProxy", serverSide = "mc_plus.common.CommonProxy") public static CommonProxy proxy; public static final PacketPipeline pipe = new PacketPipeline(); public static int stat = 0; public static Block test; public static Block reset; @EventHandler public void preInit(FMLPreInitializationEvent event) { test = new Test(true).setBlockName("test"); GameRegistry.registerBlock(test, "placer"); reset = new Test(false).setBlockName("reset"); GameRegistry.registerBlock(reset, "reset"); } @EventHandler public void init(FMLInitializationEvent event) { proxy.registerRender(); MinecraftForge.EVENT_BUS.register(new StatEventHandler()); pipe.initialise(); } @EventHandler public void postInit(FMLPostInitializationEvent event) { pipe.postInitialise(); } }
Classe de l’Extended Properties :
package mc_plus.common; import mc_plus.network.PacketStat; 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 ExtendedPlayer implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "ExtendedPlayer"; private EntityPlayer player; private int stat; public ExtendedPlayer(EntityPlayer player) { this.player = player; this.stat = 0; } public static final void register(EntityPlayer player) { player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player)); } public static final ExtendedPlayer get(EntityPlayer player) { return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME); } @Override public void saveNBTData(NBTTagCompound compound) { // We need to create a new tag compound that will save everything for our Extended Properties NBTTagCompound properties = new NBTTagCompound(); properties.setInteger("stat", this.stat); /* Now add our custom tag to the player's tag with a unique name (our property's name). This will allow you to save multiple types of properties and distinguish between them. If you only have one type, it isn't as important, but it will still avoid conflicts between your tag names and vanilla tag names. For instance, if you add some "Items" tag, that will conflict with vanilla. Not good. So just use a unique tag name. */ compound.setTag(EXT_PROP_NAME, properties); } // Load whatever data you saved @Override public void loadNBTData(NBTTagCompound compound) { // Here we fetch the unique tag compound we set for this class of Extended Properties NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); // Get our data from the custom tag compound this.stat = properties.getInteger("stat"); // Just so you know it's working, add this line: System.out.println("[TUT PROPS] Stat from NBT: " + this.stat); } @Override public void init(Entity entity, World world) { // TODO Auto-generated method stub } public void sync() { { PacketStat paquetPlayer = new PacketStat (this.stat); Mc_Plus.pipe.sendToServer(paquetPlayer); if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; Mc_Plus.pipe.sendTo(paquetPlayer, player1); } } } public static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } public static void saveProxyData(EntityPlayer player) { ExtendedPlayer playerData = ExtendedPlayer.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); CommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { ExtendedPlayer playerData = ExtendedPlayer.get(player); NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } public int getStat() { return this.stat; } public void setStat(int i) { this.stat = i; this.sync(); } public void incrStat() { this.stat++; this.sync(); } public void decrStat() { if (this.stat > 0) this.stat–; this.sync(); } }
Classe des events :
package mc_plus.common; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ChatComponentTranslation; 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.LivingEvent.LivingUpdateEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class StatEventHandler { @SubscribeEvent public void onEntityConstructing(EntityConstructing event) { if (event.entity instanceof EntityPlayer && ExtendedPlayer.get((EntityPlayer) event.entity) == null) ExtendedPlayer.register((EntityPlayer) event.entity); } @SubscribeEvent public void onLivingDeathEvent(LivingDeathEvent event) { if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) { NBTTagCompound playerData = new NBTTagCompound(); ((ExtendedPlayer) (event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME))).saveNBTData(playerData); CommonProxy.storeEntityData(((EntityPlayer) event.entity).getDisplayName(), playerData); ExtendedPlayer.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) { ((ExtendedPlayer) (event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME))).loadNBTData(playerData); } ((ExtendedPlayer) (event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME))).sync(); } } @SubscribeEvent public void onUpdate(LivingUpdateEvent event) { if(event.entity instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.entity; ItemStack heldItem = player.getHeldItem(); ExtendedPlayer props = ExtendedPlayer.get(player); // if(!player.worldObj.isRemote) int stat = props.getStat(); if(heldItem != null && heldItem.getItem() == Items.gold_ingot) { if(!player.worldObj.isRemote) { player.addChatMessage(new ChatComponentTranslation("[SERVER] : Stat : " + stat)); } else { player.addChatMessage(new ChatComponentTranslation("[CLIENT] : Stat : " + stat)); } } if(heldItem != null && heldItem.getItem() == Items.diamond) { props.incrStat(); } if(stat > 10) { player.capabilities.allowFlying = true; if(heldItem != null && heldItem.getItem() == Items.iron_ingot) { player.addChatMessage(new ChatComponentTranslation("Je peux voler !")); } } else { player.capabilities.allowFlying = false; if(heldItem != null && heldItem.getItem() == Items.iron_ingot) { player.addChatMessage(new ChatComponentTranslation("Je peux pas voler !")); } } } } }
Je pense que c’est un problème de paquets, je vous mets donc les classes concernant les paquets :
J’ai pris ces classes sur le wiki de forge :http://www.minecraftforge.net/wiki/Netty_Packet_Handling
Classe de AbstractPacket :package mc_plus.network; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import net.minecraft.entity.player.EntityPlayer; public abstract class AbstractPacket { public abstract void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer); /** * Decode the packet data from the ByteBuf stream. Complex data sets may need specific data handlers (See @link{cpw.mods.fml.common.network.ByteBuffUtils}) * * @param ctx channel context * @param buffer the buffer to decode from */ public abstract void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer); /** * Handle a packet on the client side. Note this occurs after decoding has completed. * * @param player the player reference */ public abstract void handleClientSide(EntityPlayer player); /** * Handle a packet on the server side. Note this occurs after decoding has completed. * * @param player the player reference */ public abstract void handleServerSide(EntityPlayer player); }
Classe de PacketPipeline :
package mc_plus.network; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageCodec; import java.util.Collections; import java.util.Comparator; import java.util.EnumMap; import java.util.LinkedList; import java.util.List; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.network.INetHandler; import net.minecraft.network.NetHandlerPlayServer; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.network.FMLEmbeddedChannel; import cpw.mods.fml.common.network.FMLOutboundHandler; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.internal.FMLProxyPacket; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; /** * Packet pipeline class. Directs all registered packet data to be handled by the packets themselves. * @author sirgingalot * some code from: cpw */ @ChannelHandler.Sharable public class PacketPipeline extends MessageToMessageCodec <fmlproxypacket, abstractpacket="">{ private EnumMap <side, fmlembeddedchannel="">channels; private LinkedList<class<? extends="" abstractpacket="">> packets = new LinkedList<class<? extends="" abstractpacket="">>(); private boolean isPostInitialised = false; /** * Register your packet with the pipeline. Discriminators are automatically set. * * @param clazz the class to register * * @return whether registration was successful. Failure may occur if 256 packets have been registered or if the registry already contains this packet */ public boolean registerPacket(Class clazz) { if (this.packets.size() > 256) { // You should log here!! return false; } if (this.packets.contains(clazz)) { // You should log here!! return false; } if (this.isPostInitialised) { // You should log here!! return false; } this.packets.add(clazz); return true; } // In line encoding of the packet, including discriminator setting @Override protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List <object>out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class clazz = msg.getClass(); if (!this.packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); } byte discriminator = (byte) this.packets.indexOf(clazz); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); } // In line decoding and handling of the packet @Override protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List <object>out) throws Exception { ByteBuf payload = msg.payload(); byte discriminator = payload.readByte(); Class clazz = this.packets.get(discriminator); if (clazz == null) { throw new NullPointerException("No packet registered for discriminator: " + discriminator); } AbstractPacket pkt = clazz.newInstance(); pkt.decodeInto(ctx, payload.slice()); EntityPlayer player; switch (FMLCommonHandler.instance().getEffectiveSide()) { case CLIENT: player = this.getClientPlayer(); pkt.handleClientSide(player); break; case SERVER: INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); player = ((NetHandlerPlayServer) netHandler).playerEntity; pkt.handleServerSide(player); break; default: } out.add(pkt); } // Method to call from FMLInitializationEvent public void initialise() { this.channels = NetworkRegistry.INSTANCE.newChannel("TUT", this); } // Method to call from FMLPostInitializationEvent // Ensures that packet discriminators are common between server and client by using logical sorting public void postInitialise() { if (this.isPostInitialised) { return; } this.isPostInitialised = true; Collections.sort(this.packets, new Comparator<class<? extends="" abstractpacket="">>() { @Override public int compare(Class clazz1, Class clazz2) { int com = String.CASE_INSENSITIVE_ORDER.compare(clazz1.getCanonicalName(), clazz2.getCanonicalName()); if (com == 0) { com = clazz1.getCanonicalName().compareTo(clazz2.getCanonicalName()); } return com; } }); } @SideOnly(Side.CLIENT) private EntityPlayer getClientPlayer() { return Minecraft.getMinecraft().thePlayer; } /** * Send this message to everyone. * * Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper * * @param message The message to send */ public void sendToAll(AbstractPacket message) { this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL); this.channels.get(Side.SERVER).writeAndFlush(message); } /** * Send this message to the specified player. * * Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper * * @param message The message to send * @param player The player to send it to */ public void sendTo(AbstractPacket message, EntityPlayerMP player) { this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER); this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player); this.channels.get(Side.SERVER).writeAndFlush(message); } /** * Send this message to everyone within a certain range of a point. * * Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper * * @param message The message to send * @param point The {@link cpw.mods.fml.common.network.NetworkRegistry.TargetPoint} around which to send */ public void sendToAllAround(AbstractPacket message, NetworkRegistry.TargetPoint point) { this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT); this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point); this.channels.get(Side.SERVER).writeAndFlush(message); } /** * Send this message to everyone within the supplied dimension. * * Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper * * @param message The message to send * @param dimensionId The dimension id to target */ public void sendToDimension(AbstractPacket message, int dimensionId) { this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION); this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId); this.channels.get(Side.SERVER).writeAndFlush(message); } /** * Send this message to the server. * * Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper * * @param message The message to send */ public void sendToServer(AbstractPacket message) { this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER); this.channels.get(Side.CLIENT).writeAndFlush(message); } }
Classe de PacketStat :
package mc_plus.network; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import mc_plus.common.ExtendedPlayer; import net.minecraft.entity.player.EntityPlayer; public class PacketStat extends AbstractPacket { private int stat; public PacketStat(){} public PacketStat(int stat) { this.stat = stat; } @Override public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) { buffer.writeInt(stat); } @Override public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) { this.stat = buffer.readInt(); } @Override public void handleClientSide(EntityPlayer player) { ExtendedPlayer props = ExtendedPlayer.get(player); props.setStat(this.stat); } @Override public void handleServerSide(EntityPlayer player) { ExtendedPlayer props = ExtendedPlayer.get(player); props.setStat(this.stat); } }
:::
Voilà, j’espère que vous pourrez me dire où est mon erreur, sachant que le jeu se lance !
En tout cas, merci d’avoir pris le temps de lire ! :)</class<?></object></object></class<?></class<?></side,></fmlproxypacket,> -
A quoi sert la fonction init(Entity entity, World world) ??
-
Comment passer par le système pour envoyer un packet via le FFMT api?