Posted July 26, 20223 yr 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); } }
July 26, 20223 yr Please show where you call ModMessages.register Edited July 26, 20223 yr by Luis_ST
July 26, 20223 yr 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(); } }
July 26, 20223 yr Are the packets sent on client and received on server, is handle called? Use debugger to check. Edited July 26, 20223 yr by Luis_ST
July 26, 20223 yr Author How would I check in debugger? and would I do that on server side or client side Edited July 26, 20223 yr by EthTDP
July 26, 20223 yr 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.
July 26, 20223 yr 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
July 26, 20223 yr 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.
July 26, 20223 yr Author the code in the handle method does work and there is a closestEntity. Edited July 26, 20223 yr by EthTDP
July 26, 20223 yr Then everything should work fine. Edit: Did you check this in Singleplayer or and server? Edited July 26, 20223 yr by Luis_ST
July 26, 20223 yr 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 July 26, 20223 yr 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.