Jump to content

SimpleChannel sends packet to client and not to the server.


Recommended Posts

Posted

I have a laser block that can shoot lasers using energy. The laser has the option to be always active or only active if a redstone signal is sent (like the command block). If the player clicks in the GUI on the button it should update it on the server. So I read https://mcforge.readthedocs.io/en/latest/networking/simpleimpl/ and tried to implement it for my case. To send the data I use SimpleChanel.sendToServer. But somehow it sends the data to the client. My code:

PacketHandler:

    private static final String PROTOCOL_VERSION = "1";
    public static final SimpleChannel LASER_CHANNEL = NetworkRegistry.newSimpleChannel(
            new ModResourceLocation("laser_transmitter"),
            () -> PROTOCOL_VERSION,
            PROTOCOL_VERSION::equals,
            PROTOCOL_VERSION::equals);

    private static int id = 0;

    public static class LaserPacket {

        BlockPos pos;
        boolean alwaysActive;

        public LaserPacket(PacketBuffer buffer) {
            this(buffer.readBlockPos(), buffer.readBoolean());
        }

        public LaserPacket(BlockPos pos, boolean alwaysActive) {
            this.pos = pos;
            this.alwaysActive = alwaysActive;
        }

        public void encode(PacketBuffer packetBuffer) {
            packetBuffer.writeBlockPos(pos);
            packetBuffer.writeBoolean(alwaysActive);
        }

        public void handle(Supplier<NetworkEvent.Context> ctx) {
            ctx.get().enqueueWork(() -> {
                World world = Minecraft.getInstance().world;
                if (!world.isRemote()) {
                    LaserTransmitterTileEntity tileEntity = (LaserTransmitterTileEntity)world.getTileEntity(pos);
                    tileEntity.setAlwaysActive(alwaysActive);
                }
            });
            ctx.get().setPacketHandled(true);
        }
    }

    public static void register() {
        LASER_CHANNEL.registerMessage(id++, LaserPacket.class, LaserPacket::encode, LaserPacket::new, LaserPacket::handle);
    }

TileEntity:

    public void setAlwaysActive(boolean alwaysActive) {
        this.alwaysActive = alwaysActive;

        if (world.isRemote()) {
            PacketHandler.LASER_CHANNEL.sendToServer(new TatPacketHandler.LaserPacket(pos, alwaysActive));
        } else {
            markDirty();
        }
    }

 

The methode PacketHandler.register is called in the constructor of my Main class.

But when i debug in LaserPacket.handle the world is always a ClientWorld. What have I done wrong?

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.