Jump to content

[1.7.10] Trouble with sending packet to client


NeoSup2130

Recommended Posts

Is this a joke? Are you trolling?

 

Seriously, you give us nothing to work with. It's like phoning the doctor and screaming "Ouch it hurts!" without any further explanation.

because I don' t know what to show?

CommonProxy/ The Packet / PacketPipeLine / The ExtendedPlayer / MainClass

VampZ modder

Link to comment
Share on other sites

Well, maybe you should first clarify what your "trouble" is...

I've trouble with sending a packet from server side to client side

It's my first time with packet sending

 

Have you searched for a tutorial? I believe diesieben07 has one on this forum.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Well, maybe you should first clarify what your "trouble" is...

I've trouble with sending a packet from server side to client side

It's my first time with packet sending

 

Have you searched for a tutorial? I believe diesieben07 has one on this forum.

 

Yeah I've searched some tutorials: CoolAlias's one but I find it still complex because it's my first time with the packethandler and sending packets

VampZ modder

Link to comment
Share on other sites

PacketPipeLine

 

 

@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;

 

    public boolean registerPacket(Class<? extends AbstractPacket> 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;

    }

   

    public void initialise() {

   

    this.channels = NetworkRegistry.INSTANCE.newChannel("VampZ", this); 

    registerPackets();

    }

   

    public void registerPackets() {

    registerPacket(SyncPlayerPropsPacket.class);

    }

    @Override

    protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception {

        ByteBuf buffer = Unpooled.buffer();

        Class<? extends AbstractPacket> 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);

    }

 

    @Override

    protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {

        ByteBuf payload = msg.payload();

        byte discriminator = payload.readByte();

        Class<? extends AbstractPacket> 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);

    }

   

    @SideOnly(Side.CLIENT)

    private EntityPlayer getClientPlayer() {

        return Minecraft.getMinecraft().thePlayer;

    }

   

    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);

    }

 

    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);

    }

 

    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);

    }

 

    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);

    }

 

    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);

    }

 

 

}

 

 

 

 

SyncPlayerPropsPacket

 

 

public class SyncPlayerPropsPacket extends AbstractPacket

{

 

private NBTTagCompound data;

 

public SyncPlayerPropsPacket() {}

 

public SyncPlayerPropsPacket(EntityPlayer player) {

data = new NBTTagCompound();

ExtendedPlayer.get(player).saveNBTData(data);

}

 

@Override

public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {

ByteBufUtils.writeTag(buffer, data);

}

 

@Override

public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {

data = ByteBufUtils.readTag(buffer);

}

 

@Override

public void handleClientSide(EntityPlayer player) {

ExtendedPlayer.get(player).loadNBTData(data);

}

 

@Override

public void handleServerSide(EntityPlayer player) {

}

}

 

 

 

ExtendedPlayer

 

 

package com.neosup.VampZ;

 

import ibxm.Player;

 

import java.io.ByteArrayOutputStream;

import java.io.DataOutputStream;

 

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;

 

import com.neosup.VampZ.network.SyncPlayerPropsPacket;

import com.neosup.VampZ.proxy.CommonProxy;

 

public class ExtendedPlayer implements IExtendedEntityProperties {

 

public final static String EXT_PROP_VZPLAYER = "ExtendedPlayer";

private final EntityPlayer player;

 

private int MaxLevel, CurrentVampLevel, CurrentWereLevel, CurrentPlayerLevel;

 

public ExtendedPlayer(EntityPlayer player){

this.player = player;

this.MaxLevel = 15;

}

 

public static final void register(EntityPlayer player){

player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_VZPLAYER, new ExtendedPlayer(player));

}

 

public static final ExtendedPlayer get(EntityPlayer player){

return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_VZPLAYER);

}

 

public static void saveProxyData(EntityPlayer player) {

ExtendedPlayer playerData = ExtendedPlayer.get(player);

NBTTagCompound savedData = new NBTTagCompound();

}

 

private static final String getSaveKey(EntityPlayer player) {

return player.getCommandSenderName() + ":" + EXT_PROP_VZPLAYER;

}

 

 

@Override

public void saveNBTData(NBTTagCompound compound) {

NBTTagCompound properties = new NBTTagCompound();

properties.setInteger("CurrentPlayerLevel", this.CurrentPlayerLevel);

properties.setInteger("CurrentVampLevel", this.CurrentVampLevel);

properties.setInteger("CurrentWereLevel", this.CurrentWereLevel);

properties.setInteger("MaxLevel", this.MaxLevel);

compound.setTag(EXT_PROP_VZPLAYER, properties);

 

}

 

@Override

public void loadNBTData(NBTTagCompound compound) {

NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_VZPLAYER);

this.CurrentPlayerLevel = properties.getInteger("CurrentPlayerLevel");

this.CurrentVampLevel = properties.getInteger("CurrentVampLevel");

this.CurrentWereLevel = properties.getInteger("CurrentWereLevel");

this.MaxLevel = properties.getInteger("MaxLevel");

 

}

 

@Override

public void init(Entity entity, World world) {

}

 

public int getLevelVampire(){

return this.CurrentVampLevel;

}

 

public int getLevelWerewolf(){

return this.CurrentWereLevel;

}

 

public int getLevelHuman(){

return this.CurrentPlayerLevel;

}

 

public boolean CanPlayerLevel(){

if(this.getLevelHuman() < this.MaxLevel || this.getLevelVampire() < this.MaxLevel || this.getLevelWerewolf() < this.MaxLevel){

return true;

}else{

return false;

}

}

public int AddLevelVampire(int levelup){

return this.CurrentVampLevel += levelup;

}

 

public int AddLevelWerewolf(int levelup){

return this.CurrentWereLevel += levelup;

}

public int AddLevelPlayer(int levelup){

return this.CurrentPlayerLevel += levelup;

}

 

public static final void loadProxyData(EntityPlayer player) {

ExtendedPlayer playerData = ExtendedPlayer.get(player);

NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));

if (savedData != null) { playerData.loadNBTData(savedData); }

MainClass.packetPipeline.sendTo(new SyncPlayerPropsPacket(player), (EntityPlayerMP) player);

}

 

/**

* Sends a packet to the client containing information stored on the server

* for ExtendedPlayer

*/

public final void sync()

{

ByteArrayOutputStream bos = new ByteArrayOutputStream(8);

DataOutputStream outputStream = new DataOutputStream(bos);

 

// We'll write max mana first so when we set current mana client

// side, it doesn't get set to 0 (see methods below)

try {

outputStream.writeInt(this.CurrentPlayerLevel);

outputStream.writeInt(this.CurrentVampLevel);

outputStream.writeInt(this.CurrentWereLevel);

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

 

 

 

 

CommonProxy

 

 

public class CommonProxy implements IGuiHandler {

 

private static final Map<String, NBTTagCompound> extendedEntityData = new HashMap<String, NBTTagCompound>();

 

public void registerRenderThings() {

}

 

public static void storeEntityData(String name, NBTTagCompound compound)

{

extendedEntityData.put(name, compound);

}

 

public static NBTTagCompound getEntityData(String name)

{

return extendedEntityData.remove(name);

}

 

public void registerTileEntitySpecialRenderer() {

 

}

public void registerItemRenderers() {

 

}

public void initialize() {

MainClass.channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(MainClass.networkChannelName);

 

}

 

@Override

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

return null;

}

 

@Override

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

return null;

}

 

 

VampZ modder

Link to comment
Share on other sites

You have no idea what you are doing, right? You are just copy-pasting code from a tutorial, and you don't even care to remove the old comments which have nothing to do with your code. Then, I suggest using the SimpleNetworkWrapper for packet handling, diesieben07 has a tutorial here.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.