Packet | Message vide



  • Salut !

    Petit soucis je ne sais pas pourquoi, j'ai un message vide pour les deux joueurs visés lorsque j'éxécute ceci :

    network.registerMessage(PacketTransfer.Handler.class, PacketTransfer.class, 21, Side.SERVER);
    network.registerMessage(PacketMessage.Handler.class, PacketMessage.class, 12, Side.CLIENT);
    

    (Je souligne qu'il est Side.CLIENT le PacketMessage

    package fr.altiscraft.benjaminloison.packets;
    
    import cpw.mods.fml.common.network.ByteBufUtils;
    import cpw.mods.fml.common.network.simpleimpl.IMessage;
    import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
    import cpw.mods.fml.common.network.simpleimpl.MessageContext;
    import cpw.mods.fml.relauncher.Side;
    import cpw.mods.fml.relauncher.SideOnly;
    import io.netty.buffer.ByteBuf;
    import net.minecraft.client.Minecraft;
    import net.minecraft.client.resources.I18n;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.util.ChatComponentText;
    import net.minecraft.util.EnumChatFormatting;
    
    public class PacketMessage implements IMessage {
    
    private String message;
    private boolean bad;
    private boolean isInt;
    private int i = 0;
    
    public PacketMessage() {
    }
    
    public PacketMessage(String message, boolean bad, int i) {
    this.i = i;
    this.message = message;
    this.bad = bad;
    this.isInt = true;
    }
    
    public PacketMessage(String message, boolean bad) {
    this.message = message;
    this.bad = bad;
    this.isInt = false;
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
    this.message = ByteBufUtils.readUTF8String(buf);
    this.bad = buf.readBoolean();
    this.isInt = buf.readBoolean();
    this.i = buf.readInt();
    }
    
    @Override
    public void toBytes(ByteBuf buf) {
    ByteBufUtils.writeUTF8String(buf, this.message);
    buf.writeBoolean(this.bad);
    buf.writeInt(this.i);
    buf.writeBoolean(this.isInt);
    }
    
    public static class Handler implements IMessageHandler <packetmessage, imessage="">{
    @Override
    @SideOnly(Side.CLIENT)
    public IMessage onMessage(PacketMessage message, MessageContext ctx) {
    EntityPlayer player = Minecraft.getMinecraft().thePlayer;
    if(message.bad)
    {
    ChatComponentText notenoughmoney = new ChatComponentText(EnumChatFormatting.RED + I18n.format(message.message));
    player.addChatComponentMessage(notenoughmoney);
    }
    else
    {
    if(message.isInt){
    ChatComponentText notenoughmoney = new ChatComponentText(EnumChatFormatting.GREEN + I18n.format(message.message) + " " + message.i);
    player.addChatComponentMessage(notenoughmoney);
    }
    else
    {
    ChatComponentText notenoughmoney = new ChatComponentText(EnumChatFormatting.GREEN + I18n.format(message.message));
    player.addChatComponentMessage(notenoughmoney);
    }
    }
    return null;
    
    }
    }
    
    }
    
    

    et le packet Transfer

    package fr.altiscraft.benjaminloison.packets;
    
    import java.util.List;
    
    import cpw.mods.fml.common.network.ByteBufUtils;
    import cpw.mods.fml.common.network.simpleimpl.IMessage;
    import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
    import cpw.mods.fml.common.network.simpleimpl.MessageContext;
    import fr.altiscraft.benjaminloison.common.ModAltisCraft;
    import fr.altiscraft.benjaminloison.entity.EEPRestrained;
    import io.netty.buffer.ByteBuf;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.server.MinecraftServer;
    
    public class PacketTransfer implements IMessage {
    
    private String name;
    private int i;
    private String sender;
    
    public PacketTransfer()
    {
    
    }
    
    public PacketTransfer(int i, String name, String sender)
    {
    this.i = i;
    this.name = name;
    this.sender = sender;
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
    this.i = buf.readInt();
    this.name = ByteBufUtils.readUTF8String(buf);
    this.sender = ByteBufUtils.readUTF8String(buf);
    }
    
    @Override
    public void toBytes(ByteBuf buf) {
    buf.writeInt(this.i);
    ByteBufUtils.writeUTF8String(buf, this.name);
    ByteBufUtils.writeUTF8String(buf, this.sender);
    }
    
    public static class Handler implements IMessageHandler <packettransfer, imessage="">{
    @Override
    public IMessage onMessage(PacketTransfer message, MessageContext ctx) {
    EntityPlayer sender = ctx.getServerHandler().playerEntity;
    List <entityplayermp>allPlayers = MinecraftServer.getServer().getConfigurationManager().playerEntityList;
    for (EntityPlayerMP player : allPlayers) {
    if(player.getDisplayName().equalsIgnoreCase(message.name))
    {
    EEPRestrained emp = EEPRestrained.get(player);
    EEPRestrained emp2 = EEPRestrained.get(sender);
    emp2.delMoney(message.i);
    emp.addMoney(message.i);
    emp.sync();
    emp2.sync();
    ModAltisCraft.instance.network.sendTo(new PacketMessage("t4.transfered", false), (EntityPlayerMP) sender);
    ModAltisCraft.instance.network.sendTo(new PacketMessage("t4.transfering", false), (EntityPlayerMP) player);
    }
    }
    return null;
    }
    }
    
    }
    
    

    Et voici où j'éxécute le code (côté client #Gui)

    if(btn.id == 2)
    {
    if(textfield.getText() != null && textefield.getText() != null)
    {
    if(this.emp != null)
    {
    if(StringUtils.isNumeric(textfield.getText()))
    {
    int i = Integer.parseInt(textfield.getText());
    if(emp.sufficientm(i))
    {
    ModAltisCraft.instance.network.sendToServer(new PacketTransfer(i, textefield.getText(), player.getDisplayName()));
    }
    }
    }
    }
    

    Des idées ? Je ne comprends vraiment pas et le code avant :

    emp2.delMoney(message.i);
    emp.addMoney(message.i);
    emp.sync();
    emp2.sync();
    

    semble inéxécuté ou du moins avec erreur, rien dans les logs et pas d'erreur de personne etc avec les System.out.println :/</entityplayermp></packettransfer,></packetmessage,>



  • J'avais bien vérifier l'ID des packets mais y en avait caché dans le code enfin bon un bordel ^^ le problème venait de deux même ID de packet 😄


  • Administrateurs

    Juste pour informatique, tu va avoir des valeur incohérentes dans PacketMessage car tu as l'ordre de toBytes n'est pas le même que fromBytes.