Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.