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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Another Minecraft Launcher https://multimc.org/#Download or https://atlauncher.com/downloads
    • Remove these mods and combine OptiFine HD U I6 with Forge 47.2.18 Start with Optifine - without any other mods - if this works, add the mods one by one or in groups until you find the mod, conflicting with Optifine
    • Ok yeah I think this might be where the problem starts. For starters, I think you can get rid of your RecipeType impl and make it a static constant instead. I'm not 100% sure this will fix it but it's posssible. RuneInscribingRecipe public class RuneInscribingRecipe implements Recipe<SimpleContainer> { public static final RecipeType<RuneInscribingRecipe> RECIPE_TYPE = new RecipeType<>(){}; // .... @Override public RecipeType<?> getType() { return RECIPE_TYPE; } // .... } RuneInscriberMenu private Optional<RecipeHolder<RuneInscribingRecipe>> getCurrentRecipe() { SimpleContainer inventory = new SimpleContainer(2); inventory.setItem(BASE_INPUT_SLOT, this.inventory.getStackInSlot(BASE_INPUT_SLOT)); inventory.setItem(TEMPLATE_INPUT_SLOT, this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); List<RecipeHolder<RuneInscribingRecipe>> list = this.blockEntity.getLevel().getRecipeManager().getRecipesFor(RuneInscribingRecipe.RECIPE_TYPE, inventory, this.blockEntity.getLevel()); if (list.isEmpty()) { return Optional.empty(); } return Optional.of(list.get(0)); }   Also, this here is a little suspicious where you're getting the `this.inventory.getStackInSlot` calls. I'd try and make sure those are what you expect they are with either a Debug line in IDE or print statement SimpleContainer inventory = new SimpleContainer(2); inventory.setItem(BASE_INPUT_SLOT, this.inventory.getStackInSlot(BASE_INPUT_SLOT)); System.out.printf("CHECKING BASE INPUT OF RECIPE: %s\n", this.inventory.getStackInSlot(BASE_INPUT_SLOT)); inventory.setItem(TEMPLATE_INPUT_SLOT, this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); System.out.printf("CHECKING TEMPLATE INPUT OF RECIPE: %s\n", this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); ^ See what the result of those print statements are, make sure they're right
    • Upon testing again after adding them, the game still doesn't crash however the resource packs are still unfortunately broken.
    • Hi! Can someone please point me to the correct FAQ or place? I'm not able do DuckDuckGo myself out of this one. As you can hopefully see, chunks are not rendered. I run a forge server, fresh install on a linux VM. Curseforge on the Win11 client with Embeddium, Oculus and Unbound shader. No mods. Forge 47.2.23 on server & client.  No errors, no crashes, no apparent CPU or RAM issues with the VM. This screenshot was captured while creative, but the same happens when flying in survival. Waiting (floating) for a few minutes sooner or (often) later, lets the missing chunks appear. Usually takes 5-10 minutes.  When on a local game, the chunks appear slowly, but they do appear without pause.   This must be something simple, obvious, but I can't find the correct "search words" to find my way out. "Chunks not loading / rendering" didn't help. Browsing several forums neither. 
  • Topics

×
×
  • Create New...

Important Information

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