Non résolu Spam packet avec ExtendedProperties
-
Bonjour j’ai suivie le tuto sur le forum concernant les extended properties
lors de la connexion au serveur je récupère des données associés au joueur et ensuite je synchronise les données sur la partie serveur et client cependant je viens de constater que ça envoie des packets infiniment et ça ralentie énormément le jeu savez-vous d’ou cela peut venir ?http://image.noelshack.com/fichiers/2018/43/4/1540472326-screenshot-1.png
Voici ma classe extended player
package com.flansmod.common; import java.util.ArrayList; import com.flansmod.common.driveables.EntityVehicle; import com.flansmod.common.driveables.VehicleType; import com.flansmod.common.network.PacketSync; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; public class ExtendedPlayer implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "CYLRP"; private final EntityPlayer player; public ArrayList<EntityVehicle> placedVehicle = new ArrayList<EntityVehicle>(); public ArrayList<ItemStack> ownedVehicle = new ArrayList<ItemStack>(); private int vCoins = 0; private final int LimitVehicle = 11; private final int startOwnedSlot = 3; private final int slotPriceFactor = 2; private int ownedSlot; public ExtendedPlayer(EntityPlayer player) { this.player = player; } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setInteger("Vcoins", this.vCoins); properties.setInteger("OwnedSlots", this.ownedSlot); /*for(int i = 0; i < placedVehicle.size(); i++) { NBTTagCompound vehicleTag = new NBTTagCompound(); NBTTagCompound itemTag = new NBTTagCompound(); String vehicleTagName = "Vehicle-" + i; String itemTagName = "Item-" + i; EntityVehicle vehicle = this.placedVehicle.get(i); VehicleType type = vehicle.getVehicleType(); ItemStack item = new ItemStack(type.item, 1, vehicle.driveableData.paintjobID); item.stackTagCompound = new NBTTagCompound(); vehicle.driveableData.writeToNBT(item.stackTagCompound); item.writeToNBT(itemTag); vehicleTag.setInteger("ItemID", Item.getIdFromItem(type.item)); compound.setTag(itemTagName, itemTag); compound.setTag(vehicleTagName, vehicleTag); } */ compound.setTag(this.EXT_PROP_NAME, properties); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(this.EXT_PROP_NAME); this.vCoins = properties.getInteger("Vcoins"); this.ownedSlot = properties.getInteger("OwnedSlots"); if(this.ownedSlot == 0) this.ownedSlot = 3; /*for(int i = 0; i < this.ownedSlot-1; i++) { String vehicleTagName = "Vehicle-" + i; String itemTagName ="Item-" + i; NBTTagCompound vehicleTag = (NBTTagCompound) compound.getTag(vehicleTagName); NBTTagCompound itemTag = (NBTTagCompound) compound.getCompoundTag(itemTagName); ItemStack vehicleItem = ItemStack.loadItemStackFromNBT(itemTag); }*/ } public void sync() { if (!player.worldObj.isRemote) { EntityPlayerMP player1 = (EntityPlayerMP) player; FlansMod.getPacketHandler().sendTo(new PacketSync(this.vCoins, this.ownedSlot), player1); } else { FlansMod.getPacketHandler().sendToServer(new PacketSync(this.vCoins, this.ownedSlot)); } } 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(); } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } @Override public void init(Entity entity, World world) { } 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); } public EntityPlayer getPlayer() { return this.player; } public void setVcoins(int value) { this.vCoins = value; this.sync(); } public int getVcoins() { return this.vCoins; } public void setOwnedSlots(int value) { this.ownedSlot = value; this.sync(); } public int getOwnedSlots() { return this.ownedSlot; } }
le code lors de la connexion du joueur :
@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 pData = ExtendedPlayer.get((EntityPlayer) event.entity); pData.setVcoins(10); System.out.println(pData.getVcoins()); ((ExtendedPlayer) (event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME))).sync(); } }
merci d’avance de votre aide
-
Salut,
Vu ton code ce n’est pas étonnant puisque dans chaque setter de ton EEP tu appels la fonction Sync qui renvoie un paquet. Or dans la réponse du paquet tu utilises les setter en question, ce qui renvoie a nouveau un paquet …En général la synchro on ne la fait que dans le sens serveur -> client (sinon cela permet de faire des hack client très facilement)