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 two classes for packets. One that makes an explosion happen when a button is pressed, and one that kills nearby entities when a button is pressed. Whenever I am in a singleplayer world, the packets work, it spawns explosions and it kills nearby entities, but whenever I start a server, it stops working. I am not sure what I am doing wrong, since I am new to the whole packets thing, and was wondering if anyone could help. 

Here is my explosion class:

public class CreeperExplosionC2SPacket {

    public CreeperExplosionC2SPacket() {

    }

    public CreeperExplosionC2SPacket(FriendlyByteBuf buf) {

    }

    public void toBytes(FriendlyByteBuf buf) {

    }

    public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            //HERE WE ARE ON THE SERVER
            ServerPlayer player = context.getSender();
            ServerLevel level = player.getLevel();

            HitResult block = player.pick(20.0D, 0.0F, false);
            HitResult fluid = player.pick(20.0D, 0.0F, true);

            BlockPos blockpos = ((BlockHitResult) block).getBlockPos();
            BlockPos fluidpos = ((BlockHitResult) fluid).getBlockPos();

                if (block.getType() == HitResult.Type.BLOCK) {
                    level.explode(player, blockpos.getX(), blockpos.getY(), blockpos.getZ(), 4.0f, Explosion.BlockInteraction.NONE);
                }

                if (fluid.getType() == HitResult.Type.BLOCK) {
                    level.explode(player, fluidpos.getX(), fluidpos.getY(), fluidpos.getZ(), 4.0f, Explosion.BlockInteraction.NONE);
                }
            });
        return true;
    }
}

Here is my "kill nearby entities" class:

public class WardenShootingC2SPacket {

    public WardenShootingC2SPacket() {

    }

    public WardenShootingC2SPacket(FriendlyByteBuf buf) {

    }

    public void toBytes(FriendlyByteBuf buf) {

    }

    public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            //HERE WE ARE ON THE SERVER
            ServerPlayer player = context.getSender();
            ServerLevel level = player.getLevel();

            List<LivingEntity> entities = level.getNearbyEntities(LivingEntity.class, TargetingConditions.DEFAULT, player, player.getBoundingBox().inflate(5.0D, 5.0D, 5.0D));

            double lowestDistanceSoFar = Double.MAX_VALUE;
            Entity closestEntity = null;

            for (LivingEntity entity : entities) {
                double distance = entity.getPosition(0).distanceTo(player.getPosition(0));
                if (distance < lowestDistanceSoFar) {
                    lowestDistanceSoFar = distance;
                    closestEntity = entity;
                }
            }

            Vec3 vec3 = player.position().add(0.0D, 1.65D, 0.0D);

            if (closestEntity != null) {

                Vec3 vec31 = closestEntity.getEyePosition().subtract(vec3);
                Vec3 vec32 = vec31.normalize();
                for (int i = 1; i < Mth.floor(vec31.length()) + 7; ++i) {
                    Vec3 vec33 = vec3.add(vec32.scale((double) i));
                    level.sendParticles(ParticleTypes.SONIC_BOOM, vec33.x, vec33.y, vec33.z,  1, 0.0D, 0.0D, 0.0D, 0.0D  );
                    level.playSound(player, player.blockPosition(), SoundEvents.WARDEN_SONIC_BOOM, SoundSource.PLAYERS, 1.0f, 1.0f);
                }

                closestEntity.hurt(DamageSource.sonicBoom(closestEntity), 20.0F);
            }
        });
        return true;
    }
}

Here is the class to register the packets:

public class ModMessages {
    private static SimpleChannel INSTANCE;

    private static int packetId = 0;
    private static int id() {
        return packetId++;
    }

    public static void register() {
        SimpleChannel net = NetworkRegistry.ChannelBuilder
                .named(new ResourceLocation(CustomItems.MODID, "messages"))
                .networkProtocolVersion(() -> "1.0")
                .clientAcceptedVersions(s -> true)
                .serverAcceptedVersions(s -> true)
                .simpleChannel();

        INSTANCE = net;

        net.messageBuilder(WardenShootingC2SPacket.class, id(), NetworkDirection.PLAY_TO_SERVER)
                .decoder(WardenShootingC2SPacket::new)
                .encoder(WardenShootingC2SPacket::toBytes)
                .consumerMainThread(WardenShootingC2SPacket::handle)
                .add();

        net.messageBuilder(CreeperExplosionC2SPacket.class, id(), NetworkDirection.PLAY_TO_SERVER)
                .decoder(CreeperExplosionC2SPacket::new)
                .encoder(CreeperExplosionC2SPacket::toBytes)
                .consumerMainThread(CreeperExplosionC2SPacket::handle)
                .add();
    }

    public static <MSG> void sendToServer(MSG message) {
        INSTANCE.sendToServer(message);
    }

    public static <MSG> void sendToPlayer(MSG message, ServerPlayer player) {
        INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), message);
    }
}

 

  • Author

My ModMessages.register is in my commonsetup:

@Mod.EventBusSubscriber(modid = CustomItems.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class CommonModEvents {

    @SubscribeEvent
    public static void commonSetup(final FMLCommonSetupEvent event) {
        event.enqueueWork(() -> {
            ModVillagers.registerPOIs();
        });

        ModMessages.register();
    }
}

 

Are the packets sent on client and received on server, is handle called?

Use debugger to check.

Edited by Luis_ST

  • Author

How would I check in debugger? 

and would I do that on server side or client side

Edited by EthTDP

Use the debugger of your IDE and set at method start a breakpoint.

If your not familiar with the debugger of your IDE, check out the website of your IDE in the most cases there is a doc or a wiki about it.

No the game starts normal but if a code part with a breakpoint is execute the, it will be stopped at this breakpoint (Game freezed).

You then can continue and you know the code is called, if you repeat this for the way the Packet is called you can find the code part with the issue.
In your case you need to set a breakpoint at: Packet send -> Packet encode -> Packet decode -> Packet handle

  • Author

What exactly am I looking for? So far, all four of the breakpoints have been stopped my game and all methods have been called.

  • Author

the code in the handle method does work and there is a closestEntity.

Edited by EthTDP

Then everything should work fine.

Edit: Did you check this in Singleplayer or and server?

Edited by Luis_ST

  • Author

yeah I just noticed my problem. I checked it in a singleplayer and not a server. On the server, no breakpoints have worked, so I think the problem is that the packet is never sent.

Edited by EthTDP

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.