diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/Galaxies.java b/projects/pswg/src/main/java/com/parzivail/pswg/Galaxies.java index f6f17e390..430e4fcc2 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/Galaxies.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/Galaxies.java @@ -163,7 +163,7 @@ public void onInitialize() ServerPlayNetworking.registerGlobalReceiver(SwgPackets.C2S.ShipFire, ShipEntity::handleFirePacket); ServerPlayNetworking.registerGlobalReceiver(SwgPackets.C2S.ShipRotation, ShipEntity::handleRotationPacket); ServerPlayNetworking.registerGlobalReceiver(SwgPackets.C2S.ShipControls, ShipEntity::handleControlPacket); - ServerPlayNetworking.registerGlobalReceiver(SwgPackets.C2S.JetpackControls, JetpackItem::handeControlPacket); + ServerPlayNetworking.registerGlobalReceiver(SwgPackets.C2S.JetpackControls, JetpackItem::handleControlPacket); ServerPlayNetworking.registerGlobalReceiver(SwgPackets.C2S.TogglePatrolPosture, PlayerPacketHandler::handleTogglePatrolPosture); Galaxies.LOG.info("Loading PSWG addons via pswg-addon"); diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/client/input/JetpackControls.java b/projects/pswg/src/main/java/com/parzivail/pswg/client/input/JetpackControls.java index 3524a5f8f..0b9094c5d 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/client/input/JetpackControls.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/client/input/JetpackControls.java @@ -1,11 +1,17 @@ package com.parzivail.pswg.client.input; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; + import java.util.EnumSet; public enum JetpackControls { NONE(0), FORWARD(0b1), BACKWARD(0b10), LEFT(0b100), RIGHT(0b1000), ASCEND(0b10000), DESCEND(0b100000), MODE(0b1000000); + public static final PacketCodec> PACKET_CODEC = PacketCodecs.SHORT.xmap(JetpackControls::unpack, JetpackControls::pack); + private final short flag; JetpackControls(int flag) diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/client/input/KeyHandler.java b/projects/pswg/src/main/java/com/parzivail/pswg/client/input/KeyHandler.java index e3797810b..ea62f78b5 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/client/input/KeyHandler.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/client/input/KeyHandler.java @@ -1,14 +1,14 @@ package com.parzivail.pswg.client.input; import com.parzivail.pswg.Client; -import com.parzivail.pswg.container.SwgPackets; import com.parzivail.pswg.entity.ship.ShipEntity; import com.parzivail.pswg.item.jetpack.JetpackItem; +import com.parzivail.pswg.network.JetpackControlsC2SPacket; +import com.parzivail.pswg.network.PlayerItemActionC2SPacket; +import com.parzivail.pswg.network.UnitC2SPacket; import com.parzivail.util.item.ItemAction; -import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.client.MinecraftClient; -import net.minecraft.network.PacketByteBuf; import java.util.EnumSet; @@ -20,7 +20,7 @@ public static void tick(MinecraftClient mc) return; if (Client.KEY_SPECIES_SELECT.wasPressed()) - ClientPlayNetworking.send(SwgPackets.C2S.RequestCustomizeSelf, new PacketByteBuf(Unpooled.buffer())); + ClientPlayNetworking.send(UnitC2SPacket.OpenCharacterCustomizer); if (Client.KEY_PRIMARY_ITEM_ACTION.wasPressed()) sendItemAction(ItemAction.PRIMARY); @@ -29,7 +29,7 @@ public static void tick(MinecraftClient mc) sendItemAction(ItemAction.SECONDARY); if (Client.KEY_PATROL_POSTURE.wasPressed()) - ClientPlayNetworking.send(SwgPackets.C2S.TogglePatrolPosture, new PacketByteBuf(Unpooled.buffer())); + ClientPlayNetworking.send(UnitC2SPacket.TogglePatrolPosture); var ship = ShipEntity.getShip(mc.player); @@ -81,9 +81,7 @@ public static void tick(MinecraftClient mc) if (!controls.equals(originalControls)) { - var passedData = new PacketByteBuf(Unpooled.buffer()); - passedData.writeShort(JetpackControls.pack(controls)); - ClientPlayNetworking.send(SwgPackets.C2S.JetpackControls, passedData); + ClientPlayNetworking.send(new JetpackControlsC2SPacket(controls)); } ((IJetpackDataContainer)mc.player).pswg_setJetpackControls(controls); @@ -92,8 +90,6 @@ public static void tick(MinecraftClient mc) private static void sendItemAction(ItemAction action) { - var passedData = new PacketByteBuf(Unpooled.buffer()); - passedData.writeInt(action.ordinal()); - ClientPlayNetworking.send(SwgPackets.C2S.PlayerItemAction, passedData); + ClientPlayNetworking.send(new PlayerItemActionC2SPacket(action)); } } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/client/input/ShipControls.java b/projects/pswg/src/main/java/com/parzivail/pswg/client/input/ShipControls.java index 7b06ad88b..83178ec8c 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/client/input/ShipControls.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/client/input/ShipControls.java @@ -1,11 +1,17 @@ package com.parzivail.pswg.client.input; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; + import java.util.EnumSet; public enum ShipControls { NONE(0), THROTTLE_UP(0b1), THROTTLE_DOWN(0b10), BLASTER(0b100), SPECIAL1(0b1000), SPECIAL2(0b10000); + public static final PacketCodec> PACKET_CODEC = PacketCodecs.INTEGER.xmap(ShipControls::unpack, ShipControls::pack); + private final int flag; ShipControls(int flag) diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/client/screen/CharacterScreen.java b/projects/pswg/src/main/java/com/parzivail/pswg/client/screen/CharacterScreen.java index 037276443..533382ba3 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/client/screen/CharacterScreen.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/client/screen/CharacterScreen.java @@ -332,10 +332,6 @@ public CharacterScreen(Context context, CharacterScreenHandler handler, PlayerIn hoverText.put(CLEAR_BTN, () -> Text.translatable(I18N_CLEAR_SPECIES)); } - private void updateAbility() - { - } - @Override protected void init() { @@ -656,15 +652,8 @@ public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmou private void applySpecies() { - var passedData = new PacketByteBuf(Unpooled.buffer()); - - if (previewSpecies == null) - passedData.writeString(SwgSpeciesRegistry.METASPECIES_NONE.toString()); - else - passedData.writeString(this.previewSpecies.serialize()); - // TODO: verify species variables on server - ClientPlayNetworking.send(SwgPackets.C2S.SetOwnSpecies, passedData); + ClientPlayNetworking.send(new SetOwnSpeciesC2SPacket(previewSpecies == null ? SwgSpeciesRegistry.METASPECIES_NONE.toString() : this.previewSpecies.serialize())); } @Override diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgPackets.java b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgPackets.java index f728e5573..d8307d896 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgPackets.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgPackets.java @@ -1,34 +1,38 @@ package com.parzivail.pswg.container; import com.parzivail.pswg.Resources; +import com.parzivail.pswg.network.*; +import com.parzivail.util.network.PreciseEntitySpawnS2CPacket; +import com.parzivail.util.network.PreciseEntityVelocityUpdateS2CPacket; +import net.minecraft.network.packet.CustomPayload; import net.minecraft.util.Identifier; public class SwgPackets { public static class C2S { - public static final Identifier LightsaberForgeApply = Resources.id("lightsaber_forge_apply"); - public static final Identifier BlasterWorkbenchApply = Resources.id("blaster_workbench_apply"); - public static final Identifier RequestCustomizeSelf = Resources.id("open_character_customizer"); - public static final Identifier SetOwnSpecies = Resources.id("set_own_species"); - public static final Identifier PlayerLeftClickItem = Resources.id("player_use_left"); - public static final Identifier PlayerItemAction = Resources.id("item_action"); - public static final Identifier ShipFire = Resources.id("ship_fire"); - public static final Identifier ShipRotation = Resources.id("srot"); - public static final Identifier ShipControls = Resources.id("sctrl"); - public static final Identifier JetpackControls = Resources.id("jctrl"); - public static final Identifier TogglePatrolPosture = Resources.id("toggle_patrol_posture"); + public static final Identifier LightsaberForgeApply = new CustomPayload.Id<>(Resources.id("lightsaber_forge_apply")); + public static final Identifier BlasterWorkbenchApply = new CustomPayload.Id<>(Resources.id("blaster_workbench_apply")); + public static final CustomPayload.Id RequestCustomizeSelf = new CustomPayload.Id<>(Resources.id("open_character_customizer")); // unit + public static final CustomPayload.Id SetOwnSpecies = new CustomPayload.Id<>(Resources.id("set_own_species")); + public static final CustomPayload.Id PlayerLeftClickItem = new CustomPayload.Id<>(Resources.id("player_use_left")); + public static final CustomPayload.Id PlayerItemAction = new CustomPayload.Id<>(Resources.id("item_action")); + public static final CustomPayload.Id ShipFire = new CustomPayload.Id<>(Resources.id("ship_fire")); // unit + public static final CustomPayload.Id ShipRotation = new CustomPayload.Id<>(Resources.id("srot")); + public static final CustomPayload.Id ShipControls = new CustomPayload.Id<>(Resources.id("sctrl")); + public static final CustomPayload.Id JetpackControls = new CustomPayload.Id<>(Resources.id("jctrl")); + public static final CustomPayload.Id TogglePatrolPosture = new CustomPayload.Id<>(Resources.id("toggle_patrol_posture")); // unit } public static class S2C { public static final Identifier SyncBlockToClient = Resources.id("csync"); - public static final Identifier PreciseEntityVelocityUpdate = Resources.id("pevel"); - public static final Identifier PreciseEntitySpawn = Resources.id("pesp"); - public static final Identifier OpenEntityInventory = Resources.id("entity_inventory"); - public static final Identifier AccumulateRecoil = Resources.id("recoil"); - public static final Identifier BlasterHit = Resources.id("blaster_hit"); - public static final Identifier PlayerSocketPyro = Resources.id("player_sparks"); + public static final CustomPayload.Id PreciseEntityVelocityUpdate = new CustomPayload.Id<>(Resources.id("pevel")); + public static final CustomPayload.Id PreciseEntitySpawn = new CustomPayload.Id<>(Resources.id("pesp")); + public static final CustomPayload.Id OpenEntityInventory = new CustomPayload.Id<>(Resources.id("entity_inventory")); + public static final CustomPayload.Id AccumulateRecoil = new CustomPayload.Id<>(Resources.id("recoil")); + public static final CustomPayload.Id BlasterHit = new CustomPayload.Id<>(Resources.id("blaster_hit")); + public static final CustomPayload.Id PlayerSocketPyro = new CustomPayload.Id<>(Resources.id("player_sparks")); public static final Identifier OpenCharacterCustomizer = Resources.id("open_character_customizer"); } } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgParticleFactories.java b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgParticleFactories.java index 41e35511d..68e7ec96f 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgParticleFactories.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgParticleFactories.java @@ -1,7 +1,5 @@ package com.parzivail.pswg.container; -import com.mojang.serialization.Codec; -import com.parzivail.pswg.Resources; import com.parzivail.pswg.client.particle.ExplosionSmokeParticle; import com.parzivail.pswg.client.particle.WakeParticle; import com.parzivail.pswg.client.particle.WaterWakeParticle; @@ -9,20 +7,15 @@ import com.parzivail.pswg.features.blasters.client.particle.ScorchParticle; import com.parzivail.pswg.features.blasters.client.particle.SlugTrailParticle; import com.parzivail.pswg.features.blasters.client.particle.SparkParticle; +import com.parzivail.pswg.network.PlayerSocketSparksS2CPacket; import com.parzivail.util.client.particle.PParticleType; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry; -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.client.particle.ParticleFactory; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.PacketByteBuf; import net.minecraft.particle.BlockStateParticleEffect; import net.minecraft.particle.ParticleType; import net.minecraft.particle.ParticleTypes; -import net.minecraft.registry.Registries; -import net.minecraft.registry.Registry; -import net.minecraft.util.Identifier; import org.joml.Vector3f; import static com.parzivail.pswg.container.SwgParticleTypes.*; @@ -49,53 +42,46 @@ public static void register() registerBlockStateBasedFactory(WAKE, new WakeParticle.Factory()); } - public static void handlePlayerSocketPyro(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handlePlayerSocketPyro(PlayerSocketSparksS2CPacket packet, ClientPlayNetworking.Context context) { - var playerId = buf.readInt(); - var playerSocket = buf.readString(); + var entity = context.client().world.getEntityById(packet.playerId()); + if (!(entity instanceof PlayerEntity player)) + return; - client.execute(() -> { - assert client.world != null; + var socket = PlayerSocket.getSocket(player, packet.playerSocket()); + if (socket == null) + return; - var entity = client.world.getEntityById(playerId); - if (!(entity instanceof PlayerEntity player)) - return; + for (var i = 0; i < 25; i++) + { + var vx = context.client().world.random.nextGaussian() * 0.03; + var vy = context.client().world.random.nextGaussian() * 0.03; + var vz = context.client().world.random.nextGaussian() * 0.03; - var socket = PlayerSocket.getSocket(player, playerSocket); - if (socket == null) - return; + var sparkVelocity = socket.normal().mul(0.6f * (context.client().world.random.nextFloat() * 0.5f + 0.5f), new Vector3f()); + context.client().world.addParticle( + SwgParticleTypes.SPARK, + socket.position().x, + socket.position().y, + socket.position().z, + sparkVelocity.x + vx, + sparkVelocity.y + vy, + sparkVelocity.z + vz + ); - for (var i = 0; i < 25; i++) + if (i % 3 == 0) { - var vx = client.world.random.nextGaussian() * 0.03; - var vy = client.world.random.nextGaussian() * 0.03; - var vz = client.world.random.nextGaussian() * 0.03; - - var sparkVelocity = socket.normal().mul(0.6f * (client.world.random.nextFloat() * 0.5f + 0.5f), new Vector3f()); - client.world.addParticle( - SwgParticleTypes.SPARK, + var smokeVelocity = socket.normal().mul(0.05f * (context.client().world.random.nextFloat() * 0.5f + 0.5f), new Vector3f()); + context.client().world.addParticle( + ParticleTypes.SMOKE, socket.position().x, socket.position().y, socket.position().z, - sparkVelocity.x + vx, - sparkVelocity.y + vy, - sparkVelocity.z + vz + smokeVelocity.x + vx, + smokeVelocity.y + vy, + smokeVelocity.z + vz ); - - if (i % 3 == 0) - { - var smokeVelocity = socket.normal().mul(0.05f * (client.world.random.nextFloat() * 0.5f + 0.5f), new Vector3f()); - client.world.addParticle( - ParticleTypes.SMOKE, - socket.position().x, - socket.position().y, - socket.position().z, - smokeVelocity.x + vx, - smokeVelocity.y + vy, - smokeVelocity.z + vz - ); - } } - }); + } } } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgSpeciesRegistry.java b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgSpeciesRegistry.java index 2715e2c25..03afca8b6 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgSpeciesRegistry.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgSpeciesRegistry.java @@ -6,6 +6,7 @@ import com.parzivail.pswg.character.SwgSpecies; import com.parzivail.pswg.component.PlayerData; import com.parzivail.pswg.mixin.ServerPlayerEntityAccessor; +import com.parzivail.pswg.network.SetOwnSpeciesC2SPacket; import com.parzivail.pswg.screen.CharacterScreenHandler; import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.networking.v1.PacketSender; @@ -94,20 +95,16 @@ public static String getTranslationKey(Identifier species) return "species." + species.getNamespace() + "." + species.getPath(); } - public static void handleSetOwnSpecies(MinecraftServer server, ServerPlayerEntity serverPlayerEntity, ServerPlayNetworkHandler serverPlayNetworkHandler, PacketByteBuf packetByteBuf, PacketSender packetSender) + public static void handleSetOwnSpecies(SetOwnSpeciesC2SPacket packet, ServerPlayNetworking.Context context) { - var speciesString = packetByteBuf.readString(); - - server.execute(() -> { - // TODO: replace this with some other system - if (serverPlayerEntity.currentScreenHandler instanceof CharacterScreenHandler characterScreenHandler) - characterScreenHandler.setSpecies(speciesString); - else - { - var c = PlayerData.getPersistentPublic(serverPlayerEntity); - c.setCharacter(deserialize(speciesString)); - } - }); + // TODO: replace this with some other system + if (context.player().currentScreenHandler instanceof CharacterScreenHandler characterScreenHandler) + characterScreenHandler.setSpecies(packet.species()); + else + { + var c = PlayerData.getPersistentPublic(context.player()); + c.setCharacter(deserialize(packet.species())); + } } public static void handleRequestCustomizeSelf(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler serverPlayNetworkHandler, PacketByteBuf packetByteBuf, PacketSender packetSender) diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java b/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java index efb01551e..a28849442 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java @@ -3,16 +3,14 @@ import com.parzivail.pswg.Resources; import com.parzivail.pswg.block.ThermalDetonatorBlock; import com.parzivail.pswg.container.SwgDamageTypes; -import com.parzivail.pswg.container.SwgPackets; import com.parzivail.pswg.container.SwgParticleTypes; import com.parzivail.pswg.container.SwgTags; import com.parzivail.pswg.features.lightsabers.LightsaberItem; -import com.parzivail.util.data.PacketByteBufHelper; +import com.parzivail.pswg.network.BlasterHitS2CPacket; import com.parzivail.util.entity.IPrecisionSpawnEntity; import com.parzivail.util.entity.IPrecisionVelocityEntity; import com.parzivail.util.math.MathUtil; import com.parzivail.util.network.PreciseEntitySpawnS2CPacket; -import io.netty.buffer.Unpooled; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.networking.v1.PlayerLookup; @@ -30,7 +28,6 @@ import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.projectile.thrown.ThrownEntity; import net.minecraft.nbt.NbtCompound; -import net.minecraft.network.PacketByteBuf; import net.minecraft.network.listener.ClientPlayPacketListener; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket; @@ -120,7 +117,7 @@ public void setRange(float range) public Packet createSpawnPacket(EntityTrackerEntry entityTrackerEntry) { var entity = this.getOwner(); - return PreciseEntitySpawnS2CPacket.createPacket(SwgPackets.S2C.PreciseEntitySpawn, this, entityTrackerEntry, entity == null ? 0 : entity.getId()); + return (Packet)(Packet) ServerPlayNetworking.createS2CPacket(new PreciseEntitySpawnS2CPacket(this, entityTrackerEntry, entity == null ? 0 : entity.getId())); } @Override @@ -363,13 +360,10 @@ else if (state.getBlock() instanceof ThermalDetonatorBlock tdb) var pos = hitResult.getPos(); - var passedData = new PacketByteBuf(Unpooled.buffer()); - PacketByteBufHelper.writeVec3d(passedData, pos); - PacketByteBufHelper.writeVec3d(passedData, incident); - PacketByteBufHelper.writeVec3d(passedData, normal); + final BlasterHitS2CPacket packet = new BlasterHitS2CPacket(pos, incident, normal); for (var trackingPlayer : PlayerLookup.tracking((ServerWorld)getWorld(), blockHit.getBlockPos())) - ServerPlayNetworking.send(trackingPlayer, SwgPackets.S2C.BlasterHit, passedData); + ServerPlayNetworking.send(trackingPlayer, packet); } } } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/entity/EntityWithInventory.java b/projects/pswg/src/main/java/com/parzivail/pswg/entity/EntityWithInventory.java index a7f581602..2c2593ed7 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/entity/EntityWithInventory.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/entity/EntityWithInventory.java @@ -2,18 +2,15 @@ import com.parzivail.pswg.mixin.ServerPlayerEntityAccessor; import com.parzivail.pswg.network.OpenEntityInventoryS2CPacket; -import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.Inventory; -import net.minecraft.network.PacketByteBuf; import net.minecraft.screen.ScreenHandler; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; public interface EntityWithInventory { - static void openScreen(Identifier packetId, ServerPlayerEntity player, EntityWithInventory entity) + static void openScreen(ServerPlayerEntity player, EntityWithInventory entity) { if (player.currentScreenHandler != player.playerScreenHandler) player.closeHandledScreen(); @@ -24,9 +21,7 @@ static void openScreen(Identifier packetId, ServerPlay var inventory = entity.getInventory(); - var buf = new PacketByteBuf(Unpooled.buffer()); - new OpenEntityInventoryS2CPacket(playera.getScreenHandlerSyncId(), inventory.size(), entity.getEntityId()).write(buf); - ServerPlayNetworking.send(player, packetId, buf); + ServerPlayNetworking.send(player, new OpenEntityInventoryS2CPacket(playera.getScreenHandlerSyncId(), inventory.size(), entity.getEntityId())); player.currentScreenHandler = entity.createScreenHandler(playera.getScreenHandlerSyncId(), player.getInventory(), inventory); playera.invokeOnScreenHandlerOpened(player.currentScreenHandler); diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/entity/droid/AstromechEntity.java b/projects/pswg/src/main/java/com/parzivail/pswg/entity/droid/AstromechEntity.java index 6e1482ff4..ab547bcdb 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/entity/droid/AstromechEntity.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/entity/droid/AstromechEntity.java @@ -1,7 +1,6 @@ package com.parzivail.pswg.entity.droid; import com.parzivail.pswg.Resources; -import com.parzivail.pswg.container.SwgPackets; import com.parzivail.pswg.entity.EntityWithInventory; import com.parzivail.pswg.screen.AstromechScreenHandler; import com.parzivail.util.entity.TrackedAnimationValue; @@ -27,10 +26,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; -import net.minecraft.network.listener.ClientPlayPacketListener; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket; -import net.minecraft.server.network.EntityTrackerEntry; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.ActionResult; import net.minecraft.util.Arm; @@ -312,7 +307,7 @@ public ActionResult interactMob(PlayerEntity player, Hand hand) if (player.shouldCancelInteraction()) { if (!this.getWorld().isClient) - EntityWithInventory.openScreen(SwgPackets.S2C.OpenEntityInventory, (ServerPlayerEntity)player, this); + EntityWithInventory.openScreen((ServerPlayerEntity)player, this); return ActionResult.success(this.getWorld().isClient); } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/entity/ship/ShipEntity.java b/projects/pswg/src/main/java/com/parzivail/pswg/entity/ship/ShipEntity.java index 82119ba63..97a6f479e 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/entity/ship/ShipEntity.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/entity/ship/ShipEntity.java @@ -6,7 +6,9 @@ import com.parzivail.pswg.client.render.camera.ChaseCam; import com.parzivail.pswg.client.sound.SoundHelper; import com.parzivail.pswg.container.SwgPackets; -import com.parzivail.util.data.PacketByteBufHelper; +import com.parzivail.pswg.network.ShipControlsC2SPacket; +import com.parzivail.pswg.network.ShipRotationC2SPacket; +import com.parzivail.pswg.network.UnitC2SPacket; import com.parzivail.util.entity.EntityUtil; import com.parzivail.util.entity.IFlyingVehicle; import com.parzivail.util.entity.IPrecisionVelocityEntity; @@ -18,9 +20,13 @@ import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.block.BlockState; import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.*; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.MovementType; import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; @@ -28,11 +34,8 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.listener.ClientPlayPacketListener; -import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket; import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.EntityTrackerEntry; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.SoundEvent; @@ -97,14 +100,12 @@ public static boolean handleMouseInput(double cursorDeltaX, double cursorDeltaY) return false; } - public static void handleFirePacket(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleFirePacket(UnitC2SPacket packet, ServerPlayNetworking.Context context) { - server.execute(() -> { - var ship = getShip(player); + var ship = getShip(context.player()); - if (ship != null && ship.isPilot(player)) - ship.acceptFireInput(); - }); + if (ship != null && ship.isPilot(context.player())) + ship.acceptFireInput(); } @Override @@ -137,28 +138,20 @@ protected SoundEvent getExteriorSoundEvent() return null; } - public static void handleRotationPacket(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleRotationPacket(ShipRotationC2SPacket packet, ServerPlayNetworking.Context context) { - var q = PacketByteBufHelper.readQuaternion(buf); + var ship = getShip(context.player()); - server.execute(() -> { - var ship = getShip(player); - - if (ship != null && ship.isPilot(player)) - ship.setRotation(q); - }); + if (ship != null && ship.isPilot(context.player())) + ship.setRotation(packet.rotation()); } - public static void handleControlPacket(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleControlPacket(ShipControlsC2SPacket packet, ServerPlayNetworking.Context context) { - var controls = buf.readShort(); - - server.execute(() -> { - var ship = getShip(player); + var ship = getShip(context.player()); - if (ship != null && ship.isPilot(player)) - ship.acceptControlInput(ShipControls.unpack(controls)); - }); + if (ship != null && ship.isPilot(context.player())) + ship.acceptControlInput(packet.shipControls()); } public static ShipEntity getShip(LivingEntity player) @@ -464,9 +457,7 @@ public void acceptControlInput(EnumSet controls) if (this.getWorld().isClient) { - var passedData = new PacketByteBuf(Unpooled.buffer()); - passedData.writeShort(ShipControls.pack(controls)); - ClientPlayNetworking.send(SwgPackets.C2S.ShipControls, passedData); + ClientPlayNetworking.send(new ShipControlsC2SPacket(controls)); } } @@ -536,9 +527,7 @@ public void acceptMouseInput(double mouseDx, double mouseDy) clientInstRotation = new Quaternionf(rotation); - var passedData = new PacketByteBuf(Unpooled.buffer()); - PacketByteBufHelper.writeQuaternion(passedData, rotation); - ClientPlayNetworking.send(SwgPackets.C2S.ShipRotation, passedData); + ClientPlayNetworking.send(new ShipRotationC2SPacket(rotation)); } protected boolean allowPitchMovement() @@ -552,7 +541,7 @@ public boolean acceptLeftClick(PlayerEntity player) if (!isPilot(player)) return false; - ClientPlayNetworking.send(SwgPackets.C2S.ShipFire, new PacketByteBuf(Unpooled.buffer())); + ClientPlayNetworking.send(UnitC2SPacket.ShipFire); return true; } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterItem.java b/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterItem.java index 548d461c4..4ac3d7826 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterItem.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterItem.java @@ -6,15 +6,15 @@ import com.parzivail.pswg.Resources; import com.parzivail.pswg.compat.gravitychanger.GravityChangerCompat; import com.parzivail.pswg.component.PlayerData; -import com.parzivail.pswg.container.SwgPackets; import com.parzivail.pswg.container.SwgSounds; import com.parzivail.pswg.features.blasters.data.*; +import com.parzivail.pswg.network.AccumulateRecoilS2CPacket; +import com.parzivail.pswg.network.PlayerSocketSparksS2CPacket; import com.parzivail.tarkin.api.TarkinLang; import com.parzivail.util.client.TextUtil; import com.parzivail.util.client.TooltipUtil; import com.parzivail.util.item.*; import com.parzivail.util.math.MathUtil; -import io.netty.buffer.Unpooled; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroupEntries; @@ -34,7 +34,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.tooltip.TooltipType; import net.minecraft.nbt.NbtCompound; -import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.SoundCategory; import net.minecraft.text.Text; @@ -53,7 +52,6 @@ import java.util.Map; import java.util.UUID; import java.util.function.DoubleUnaryOperator; -import java.util.function.Function; public class BlasterItem extends Item implements ILeftClickConsumer, ICustomVisualItemEquality, IZoomingItem, ICooldownItem, IItemActionListener, IItemHotbarListener, IItemEntityTickListener, ITabStackProvider { @@ -603,26 +601,22 @@ else if (firingMode == BlasterFiringMode.ION && hasAttachment(bd, bt.attachmentB if (shouldRecoil) { - var passedData = new PacketByteBuf(Unpooled.buffer()); var horizNoise = world.random.nextGaussian(); horizNoise = horizNoise * 0.3 + 0.7 * Math.signum(horizNoise); var recoilAmount = getRecoilAmount(bd, bt.attachmentBitmask); - passedData.writeFloat(recoilAmount * (float)(bd.recoil.horizontal * horizNoise)); - passedData.writeFloat(recoilAmount * (float)(bd.recoil.vertical * (0.7 + 0.3 * (world.random.nextGaussian() + 1) / 2))); - ServerPlayNetworking.send((ServerPlayerEntity)player, SwgPackets.S2C.AccumulateRecoil, passedData); + ServerPlayNetworking.send((ServerPlayerEntity)player, new AccumulateRecoilS2CPacket( + recoilAmount * (float)(bd.recoil.horizontal * horizNoise), + recoilAmount * (float)(bd.recoil.vertical * (0.7 + 0.3 * (world.random.nextGaussian() + 1) / 2)) + )); } if (bd.pyrotechnics) { - var passedData = new PacketByteBuf(Unpooled.buffer()); - passedData.writeInt(player.getId()); - passedData.writeString(SOCKET_ID_BARREL_END); - var sPlayer = (ServerPlayerEntity)player; for (var trackingPlayer : PlayerLookup.tracking(sPlayer.getServerWorld(), player.getBlockPos())) - ServerPlayNetworking.send(trackingPlayer, SwgPackets.S2C.PlayerSocketPyro, passedData); + ServerPlayNetworking.send(trackingPlayer, new PlayerSocketSparksS2CPacket(player.getId(), SOCKET_ID_BARREL_END)); } bt.serializeAsSubtag(stack); diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterUtil.java b/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterUtil.java index 1b8a3416e..b67630428 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterUtil.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/BlasterUtil.java @@ -5,18 +5,16 @@ import com.parzivail.pswg.entity.BlasterBoltEntity; import com.parzivail.pswg.entity.BlasterIonBoltEntity; import com.parzivail.pswg.entity.BlasterStunBoltEntity; -import com.parzivail.util.data.PacketByteBufHelper; +import com.parzivail.pswg.network.BlasterHitS2CPacket; import com.parzivail.util.entity.EntityUtil; import com.parzivail.util.math.MathUtil; -import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffects; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.PacketByteBuf; import net.minecraft.particle.BlockStateParticleEffect; import net.minecraft.particle.ParticleTypes; import net.minecraft.util.math.BlockPos; @@ -25,7 +23,6 @@ import java.util.function.Consumer; import java.util.function.DoubleUnaryOperator; -import java.util.function.Function; public class BlasterUtil { @@ -77,13 +74,9 @@ public static void fireStun(World world, PlayerEntity player, Vec3d fromDir, flo } } - public static void handleBoltHit(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleBoltHit(BlasterHitS2CPacket packet, ClientPlayNetworking.Context context) { - var pos = PacketByteBufHelper.readVec3d(buf); - var incident = PacketByteBufHelper.readVec3d(buf); - var normal = PacketByteBufHelper.readVec3d(buf); - - client.execute(() -> createScorchParticles(client, pos, incident, normal, true)); + createScorchParticles(context.client(), packet.pos(), packet.incident(), packet.normal(), true); } private static void createScorchParticles(MinecraftClient client, Vec3d pos, Vec3d incident, Vec3d normal, boolean energyScorch) diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/client/BlasterRecoilManager.java b/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/client/BlasterRecoilManager.java index 04685d0d7..781875f4a 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/client/BlasterRecoilManager.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/client/BlasterRecoilManager.java @@ -1,14 +1,13 @@ package com.parzivail.pswg.features.blasters.client; import com.parzivail.pswg.Resources; +import com.parzivail.pswg.network.AccumulateRecoilS2CPacket; import com.parzivail.util.math.Ease; import com.parzivail.util.math.MathUtil; -import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.client.render.Camera; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Arm; import org.joml.Quaternionf; @@ -84,13 +83,9 @@ public static void setRecoil(float vertical, float horizontal) horizontalVelocity = horizontal; } - public static void handleAccumulateRecoil(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleAccumulateRecoil(AccumulateRecoilS2CPacket packet, ClientPlayNetworking.Context context) { - var horiz = buf.readFloat(); - var vert = buf.readFloat(); - client.execute(() -> { - setRecoil(vert, horiz); - }); + setRecoil(packet.vert(), packet.horiz()); } public static void applyCameraShake(MinecraftClient mc, MatrixStack matrix, Camera camera, float tickDelta, double fov) diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/features/lightsabers/addon/AddonLightsaberManager.java b/projects/pswg/src/main/java/com/parzivail/pswg/features/lightsabers/addon/AddonLightsaberManager.java index 11cdbb466..044584cf6 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/features/lightsabers/addon/AddonLightsaberManager.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/features/lightsabers/addon/AddonLightsaberManager.java @@ -1,7 +1,6 @@ package com.parzivail.pswg.features.lightsabers.addon; import com.parzivail.pswg.features.lightsabers.data.LightsaberBladeType; -import com.parzivail.util.data.PacketByteBufHelper; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; @@ -37,7 +36,7 @@ public static Schema deserialize(PacketByteBuf buf) { var namespace = buf.readString(); var id = buf.readString(); - var owner = PacketByteBufHelper.readNullable(buf, PacketByteBuf::readString); + var owner = buf.readNullable(PacketByteBuf::readString); var unstable = buf.readBoolean(); var bladeType = LightsaberBladeType.TYPES.get(buf.readString()); diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/handler/LeftClickHandler.java b/projects/pswg/src/main/java/com/parzivail/pswg/handler/LeftClickHandler.java index fb79f0c30..f8cd5082e 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/handler/LeftClickHandler.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/handler/LeftClickHandler.java @@ -1,16 +1,14 @@ package com.parzivail.pswg.handler; -import com.parzivail.pswg.container.SwgPackets; import com.parzivail.pswg.entity.ship.ShipEntity; import com.parzivail.pswg.features.blasters.BlasterItem; import com.parzivail.pswg.features.blasters.BlasterWield; +import com.parzivail.pswg.network.PlayerItemLeftClickC2SPacket; import com.parzivail.util.item.ILeftClickConsumer; -import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayerInteractionManager; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.PacketByteBuf; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import org.jetbrains.annotations.NotNull; @@ -87,10 +85,7 @@ private static boolean useItemLeft(PlayerEntity player, ILeftClickConsumer lcc, if (ar == ActionResult.PASS) return false; - var passedData = new PacketByteBuf(Unpooled.buffer()); - passedData.writeInt(hand.ordinal()); - passedData.writeBoolean(isRepeatEvent); - ClientPlayNetworking.send(SwgPackets.C2S.PlayerLeftClickItem, passedData); + ClientPlayNetworking.send(new PlayerItemLeftClickC2SPacket(hand, isRepeatEvent)); return true; } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/item/jetpack/JetpackItem.java b/projects/pswg/src/main/java/com/parzivail/pswg/item/jetpack/JetpackItem.java index ec50fcefa..c083d23c9 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/item/jetpack/JetpackItem.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/item/jetpack/JetpackItem.java @@ -4,11 +4,12 @@ import com.parzivail.pswg.client.input.JetpackControls; import com.parzivail.pswg.entity.ship.ShipEntity; import com.parzivail.pswg.item.jetpack.data.JetpackTag; +import com.parzivail.pswg.network.JetpackControlsC2SPacket; import com.parzivail.util.item.IDefaultNbtProvider; import dev.emi.trinkets.api.SlotReference; import dev.emi.trinkets.api.TrinketItem; import dev.emi.trinkets.api.TrinketsApi; -import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.MovementType; @@ -16,11 +17,7 @@ import net.minecraft.item.ItemConvertible; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; -import net.minecraft.network.PacketByteBuf; import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; @@ -66,10 +63,9 @@ private static void setVerticalVelocity(LivingEntity player, double y) player.setVelocity(motion.x, y, motion.z); } - public static void handeControlPacket(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleControlPacket(JetpackControlsC2SPacket packet, ServerPlayNetworking.Context context) { - var controls = buf.readShort(); - server.execute(() -> ((IJetpackDataContainer)player).pswg_setJetpackControls(JetpackControls.unpack(controls))); + ((IJetpackDataContainer)context.player()).pswg_setJetpackControls(packet.jetpackControls()); } public static Optional tickFallFlying(LivingEntity entity, ItemStack jetpack, boolean flyFalling) diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/mixin/ClientPlayNetworkHandlerMixin.java b/projects/pswg/src/main/java/com/parzivail/pswg/mixin/ClientPlayNetworkHandlerMixin.java index a40ca0b7d..ed1c2faee 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/mixin/ClientPlayNetworkHandlerMixin.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/mixin/ClientPlayNetworkHandlerMixin.java @@ -24,7 +24,7 @@ public class ClientPlayNetworkHandlerMixin @Inject(method = "onEntityVelocityUpdate(Lnet/minecraft/network/packet/s2c/play/EntityVelocityUpdateS2CPacket;)V", at = @At("TAIL")) private void onEntityVelocityUpdate(EntityVelocityUpdateS2CPacket packet, CallbackInfo ci) { - Entity entity = this.world.getEntityById(packet.getId()); + Entity entity = this.world.getEntityById(packet.getEntityId()); if (!(entity instanceof IPrecisionVelocityEntity ipe)) return; diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/mixin/EntityTrackerEntryMixin.java b/projects/pswg/src/main/java/com/parzivail/pswg/mixin/EntityTrackerEntryMixin.java index ba9ef73b5..e7e6334cf 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/mixin/EntityTrackerEntryMixin.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/mixin/EntityTrackerEntryMixin.java @@ -1,12 +1,9 @@ package com.parzivail.pswg.mixin; -import com.parzivail.pswg.container.SwgPackets; import com.parzivail.util.entity.IPrecisionVelocityEntity; import com.parzivail.util.network.PreciseEntityVelocityUpdateS2CPacket; -import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.Entity; -import net.minecraft.network.PacketByteBuf; import net.minecraft.network.packet.Packet; import net.minecraft.server.network.EntityTrackerEntry; import org.objectweb.asm.Opcodes; @@ -32,9 +29,7 @@ void tick(CallbackInfo ci) if (this.entity.velocityModified) { - var passedData = new PacketByteBuf(Unpooled.buffer()); - new PreciseEntityVelocityUpdateS2CPacket(this.entity).write(passedData); - this.sendSyncPacket(ServerPlayNetworking.createS2CPacket(SwgPackets.S2C.PreciseEntityVelocityUpdate, passedData)); + this.sendSyncPacket(ServerPlayNetworking.createS2CPacket(new PreciseEntityVelocityUpdateS2CPacket(this.entity))); this.entity.velocityModified = false; } ci.cancel(); diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/AccumulateRecoilS2CPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/AccumulateRecoilS2CPacket.java new file mode 100644 index 000000000..952b1d6b1 --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/AccumulateRecoilS2CPacket.java @@ -0,0 +1,20 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; + +public record AccumulateRecoilS2CPacket(float vert, float horiz) implements CustomPayload +{ + public static final PacketCodec CODEC = PacketCodec.tuple( + PacketCodecs.FLOAT, AccumulateRecoilS2CPacket::vert, + PacketCodecs.FLOAT, AccumulateRecoilS2CPacket::horiz, + AccumulateRecoilS2CPacket::new); + @Override + public Id getId() + { + return SwgPackets.S2C.AccumulateRecoil; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/BlasterHitS2CPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/BlasterHitS2CPacket.java new file mode 100644 index 000000000..acb630c78 --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/BlasterHitS2CPacket.java @@ -0,0 +1,23 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import com.parzivail.util.data.MorePacketCodecs; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.util.math.Vec3d; + +public record BlasterHitS2CPacket(Vec3d pos, Vec3d incident, Vec3d normal) implements CustomPayload +{ + public static final PacketCodec CODEC = PacketCodec.tuple( + MorePacketCodecs.VEC3D, BlasterHitS2CPacket::pos, + MorePacketCodecs.VEC3D, BlasterHitS2CPacket::incident, + MorePacketCodecs.VEC3D, BlasterHitS2CPacket::normal, + BlasterHitS2CPacket::new); + + @Override + public Id getId() + { + return SwgPackets.S2C.BlasterHit; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/JetpackControlsC2SPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/JetpackControlsC2SPacket.java new file mode 100644 index 000000000..c20a4e847 --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/JetpackControlsC2SPacket.java @@ -0,0 +1,24 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.client.input.JetpackControls; +import com.parzivail.pswg.container.SwgPackets; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; + +import java.util.EnumSet; + +public record JetpackControlsC2SPacket(EnumSet jetpackControls) implements CustomPayload +{ + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + JetpackControls.PACKET_CODEC, + JetpackControlsC2SPacket::jetpackControls, + JetpackControlsC2SPacket::new + ); + + @Override + public Id getId() + { + return SwgPackets.C2S.JetpackControls; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/OpenEntityInventoryS2CPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/OpenEntityInventoryS2CPacket.java index 6926c7aac..536600226 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/network/OpenEntityInventoryS2CPacket.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/OpenEntityInventoryS2CPacket.java @@ -1,68 +1,41 @@ package com.parzivail.pswg.network; import com.parzivail.pswg.client.screen.ScreenHelper; +import com.parzivail.pswg.container.SwgPackets; import com.parzivail.pswg.entity.EntityWithInventory; -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.inventory.SimpleInventory; import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; -public class OpenEntityInventoryS2CPacket +public record OpenEntityInventoryS2CPacket(int syncId, int slotCount, int entityId) implements CustomPayload { - private final int syncId; - private final int slotCount; - private final int entityId; + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple(PacketCodecs.VAR_INT, OpenEntityInventoryS2CPacket::syncId, PacketCodecs.VAR_INT, OpenEntityInventoryS2CPacket::slotCount, PacketCodecs.INTEGER, OpenEntityInventoryS2CPacket::entityId, OpenEntityInventoryS2CPacket::new); - public OpenEntityInventoryS2CPacket(int syncId, int slotCount, int entityId) + public static OpenEntityInventoryS2CPacket create(PacketByteBuf buf) { - this.syncId = syncId; - this.slotCount = slotCount; - this.entityId = entityId; + return new OpenEntityInventoryS2CPacket(buf.readUnsignedByte(), buf.readVarInt(), buf.readInt()); } - public OpenEntityInventoryS2CPacket(PacketByteBuf buf) + @Override + public Id getId() { - this.syncId = buf.readUnsignedByte(); - this.slotCount = buf.readVarInt(); - this.entityId = buf.readInt(); + return SwgPackets.S2C.OpenEntityInventory; } - public void write(PacketByteBuf buf) + public static void handle(OpenEntityInventoryS2CPacket payload, ClientPlayNetworking.Context context) { - buf.writeByte(this.syncId); - buf.writeVarInt(this.slotCount); - buf.writeInt(this.entityId); - } - - public static void handle(MinecraftClient client, ClientPlayNetworkHandler networkHandler, PacketByteBuf buf, PacketSender sender) - { - var packet = new OpenEntityInventoryS2CPacket(buf); - client.execute(() -> { - var entity = client.world.getEntityById(packet.getEntityId()); - if (entity instanceof EntityWithInventory inventoryEntity) - { - var clientPlayerEntity = client.player; - var simpleInventory = new SimpleInventory(packet.getSlotCount()); - var handler = inventoryEntity.createScreenHandler(packet.getSyncId(), clientPlayerEntity.getInventory(), simpleInventory); - clientPlayerEntity.currentScreenHandler = handler; - client.setScreen(ScreenHelper.createEntityScreen(client, handler, clientPlayerEntity.getInventory(), inventoryEntity)); - } - }); - } - - public int getSyncId() - { - return this.syncId; - } - - public int getSlotCount() - { - return this.slotCount; - } - - public int getEntityId() - { - return this.entityId; + var entity = context.client().world.getEntityById(payload.entityId()); + if (entity instanceof EntityWithInventory inventoryEntity) + { + var clientPlayerEntity = context.client().player; + var simpleInventory = new SimpleInventory(payload.slotCount()); + var handler = inventoryEntity.createScreenHandler(payload.syncId(), clientPlayerEntity.getInventory(), simpleInventory); + clientPlayerEntity.currentScreenHandler = handler; + context.client().setScreen(ScreenHelper.createEntityScreen(context.client(), handler, clientPlayerEntity.getInventory(), inventoryEntity)); + } } } diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerItemActionC2SPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerItemActionC2SPacket.java new file mode 100644 index 000000000..0a3f3271d --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerItemActionC2SPacket.java @@ -0,0 +1,23 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import com.parzivail.util.data.MorePacketCodecs; +import com.parzivail.util.item.ItemAction; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; + +public record PlayerItemActionC2SPacket(ItemAction action) implements CustomPayload +{ + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + MorePacketCodecs.ITEM_ACTION, + PlayerItemActionC2SPacket::action, + PlayerItemActionC2SPacket::new + ); + + @Override + public Id getId() + { + return SwgPackets.C2S.PlayerItemAction; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerItemLeftClickC2SPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerItemLeftClickC2SPacket.java new file mode 100644 index 000000000..54d7be43e --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerItemLeftClickC2SPacket.java @@ -0,0 +1,26 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import com.parzivail.util.data.MorePacketCodecs; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.util.Hand; + +public record PlayerItemLeftClickC2SPacket(Hand hand, boolean isRepeatEvent) implements CustomPayload +{ + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + MorePacketCodecs.HAND, + PlayerItemLeftClickC2SPacket::hand, + PacketCodecs.BOOL, + PlayerItemLeftClickC2SPacket::isRepeatEvent, + PlayerItemLeftClickC2SPacket::new + ); + + @Override + public Id getId() + { + return SwgPackets.C2S.PlayerLeftClickItem; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerSocketSparksS2CPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerSocketSparksS2CPacket.java new file mode 100644 index 000000000..cc383c5bf --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/PlayerSocketSparksS2CPacket.java @@ -0,0 +1,24 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; + +public record PlayerSocketSparksS2CPacket(int playerId, String playerSocket) implements CustomPayload +{ + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + PacketCodecs.VAR_INT, + PlayerSocketSparksS2CPacket::playerId, + PacketCodecs.STRING, + PlayerSocketSparksS2CPacket::playerSocket, + PlayerSocketSparksS2CPacket::new + ); + + @Override + public Id getId() + { + return SwgPackets.S2C.PlayerSocketPyro; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/SetOwnSpeciesC2SPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/SetOwnSpeciesC2SPacket.java new file mode 100644 index 000000000..456d23b8b --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/SetOwnSpeciesC2SPacket.java @@ -0,0 +1,13 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import net.minecraft.network.packet.CustomPayload; + +public record SetOwnSpeciesC2SPacket(String species) implements CustomPayload +{ + @Override + public Id getId() + { + return SwgPackets.C2S.SetOwnSpecies; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/ShipControlsC2SPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/ShipControlsC2SPacket.java new file mode 100644 index 000000000..5f7fc6ef6 --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/ShipControlsC2SPacket.java @@ -0,0 +1,24 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.client.input.ShipControls; +import com.parzivail.pswg.container.SwgPackets; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; + +import java.util.EnumSet; + +public record ShipControlsC2SPacket(EnumSet shipControls) implements CustomPayload +{ + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + ShipControls.PACKET_CODEC, + ShipControlsC2SPacket::shipControls, + ShipControlsC2SPacket::new + ); + + @Override + public Id getId() + { + return SwgPackets.C2S.ShipControls; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/ShipRotationC2SPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/ShipRotationC2SPacket.java new file mode 100644 index 000000000..64901aa89 --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/ShipRotationC2SPacket.java @@ -0,0 +1,23 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; +import org.joml.Quaternionf; + +public record ShipRotationC2SPacket(Quaternionf rotation) implements CustomPayload +{ + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + PacketCodecs.QUATERNIONF, + ShipRotationC2SPacket::rotation, + ShipRotationC2SPacket::new + ); + + @Override + public Id getId() + { + return SwgPackets.C2S.ShipRotation; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/network/UnitC2SPacket.java b/projects/pswg/src/main/java/com/parzivail/pswg/network/UnitC2SPacket.java new file mode 100644 index 000000000..45e63502a --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/network/UnitC2SPacket.java @@ -0,0 +1,23 @@ +package com.parzivail.pswg.network; + +import com.parzivail.pswg.container.SwgPackets; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; + +public record UnitC2SPacket(CustomPayload.Id packetId) implements CustomPayload +{ + public static final UnitC2SPacket OpenCharacterCustomizer = new UnitC2SPacket(SwgPackets.C2S.RequestCustomizeSelf); + public static final UnitC2SPacket ShipFire = new UnitC2SPacket(SwgPackets.C2S.ShipFire); + public static final UnitC2SPacket TogglePatrolPosture = new UnitC2SPacket(SwgPackets.C2S.TogglePatrolPosture); + + @Override + public Id getId() + { + return packetId; + } + + public PacketCodec createPacketCodec() { + return PacketCodec.unit(this); + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/util/data/MorePacketCodecs.java b/projects/pswg/src/main/java/com/parzivail/util/data/MorePacketCodecs.java new file mode 100644 index 000000000..b3339fa2d --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/util/data/MorePacketCodecs.java @@ -0,0 +1,29 @@ +package com.parzivail.util.data; + +import com.parzivail.util.item.ItemAction; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.util.Hand; +import net.minecraft.util.math.Vec3d; + +public class MorePacketCodecs +{ + public static > PacketCodec enumCodec(Class cls) { + return PacketCodec.ofStatic( + PacketByteBuf::writeEnumConstant, + buf -> buf.readEnumConstant(cls) + ); + } + + public static final PacketCodec HAND = enumCodec(Hand.class); + + public static final PacketCodec ITEM_ACTION = enumCodec(ItemAction.class); + + public static final PacketCodec VEC3D = PacketCodec.tuple( + PacketCodecs.DOUBLE, Vec3d::getX, + PacketCodecs.DOUBLE, Vec3d::getY, + PacketCodecs.DOUBLE, Vec3d::getZ, + Vec3d::new + ); +} diff --git a/projects/pswg/src/main/java/com/parzivail/util/data/PacketByteBufHelper.java b/projects/pswg/src/main/java/com/parzivail/util/data/PacketByteBufHelper.java deleted file mode 100644 index 44bb64e71..000000000 --- a/projects/pswg/src/main/java/com/parzivail/util/data/PacketByteBufHelper.java +++ /dev/null @@ -1,162 +0,0 @@ -package com.parzivail.util.data; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.util.math.EulerAngle; -import net.minecraft.util.math.Vec3d; -import org.joml.Quaternionf; -import org.joml.Vector3f; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; - -public class PacketByteBufHelper -{ - @FunctionalInterface - public interface PacketByteBufWriter - { - void write(PacketByteBuf buf, T value); - } - - @FunctionalInterface - public interface PacketByteBufReader - { - T read(PacketByteBuf buf); - } - - public static void writeNullable(PacketByteBuf buf, T value, PacketByteBufWriter writer) - { - buf.writeBoolean(value != null); - if (value != null) - writer.write(buf, value); - } - - public static T readNullable(PacketByteBuf buf, PacketByteBufReader reader) - { - var hasValue = buf.readBoolean(); - - if (!hasValue) - return null; - - return reader.read(buf); - } - - public static void writeMany(PacketByteBuf buf, Collection value, PacketByteBufWriter writer) - { - buf.writeInt(value.size()); - for (T t : value) - writer.write(buf, t); - } - - public static ArrayList readMany(PacketByteBuf buf, PacketByteBufReader reader) - { - var n = buf.readInt(); - var arr = new ArrayList(); - for (var i = 0; i < n; i++) - arr.add(reader.read(buf)); - return arr; - } - - public static void writeHashMap(PacketByteBuf buf, HashMap map, PacketByteBufWriter keyWriter, PacketByteBufWriter valueWriter) - { - buf.writeInt(map.size()); - - for (var pair : map.entrySet()) - { - keyWriter.write(buf, pair.getKey()); - valueWriter.write(buf, pair.getValue()); - } - } - - public static HashMap readHashMap(PacketByteBuf buf, PacketByteBufReader keyReader, PacketByteBufReader valueReader) - { - var size = buf.readInt(); - - var map = new HashMap(size); - - for (var i = 0; i < size; i++) - { - var k = keyReader.read(buf); - var v = valueReader.read(buf); - - map.put(k, v); - } - - return map; - } - - public static void writeQuaternion(PacketByteBuf buf, Quaternionf q) - { - buf.writeFloat(q.w); - buf.writeFloat(q.x); - buf.writeFloat(q.y); - buf.writeFloat(q.z); - } - - public static Quaternionf readQuaternion(PacketByteBuf buf) - { - var qa = buf.readFloat(); - var qb = buf.readFloat(); - var qc = buf.readFloat(); - var qd = buf.readFloat(); - return new Quaternionf(qb, qc, qd, qa); - } - - public static void writeVec3d(PacketByteBuf buf, Vec3d v) - { - buf.writeDouble(v.x); - buf.writeDouble(v.y); - buf.writeDouble(v.z); - } - - public static Vec3d readVec3d(PacketByteBuf buf) - { - var x = buf.readDouble(); - var y = buf.readDouble(); - var z = buf.readDouble(); - return new Vec3d(x, y, z); - } - - public static Vec3d readVec3dAsSingles(PacketByteBuf buf) - { - var x = buf.readFloat(); - var y = buf.readFloat(); - var z = buf.readFloat(); - return new Vec3d(x, y, z); - } - - public static void writeVec3dAsSingles(PacketByteBuf buf, Vec3d v) - { - writeVec3f(buf, v.toVector3f()); - } - - public static void writeVec3f(PacketByteBuf buf, Vector3f v) - { - buf.writeFloat(v.x); - buf.writeFloat(v.y); - buf.writeFloat(v.z); - } - - public static Vector3f readVec3f(PacketByteBuf buf) - { - var x = buf.readFloat(); - var y = buf.readFloat(); - var z = buf.readFloat(); - return new Vector3f(x, y, z); - } - - public static void writeEulerAngle(PacketByteBuf buf, EulerAngle e) - { - buf.writeFloat(e.getPitch()); - buf.writeFloat(e.getYaw()); - buf.writeFloat(e.getRoll()); - } - - public static EulerAngle readEulerAngle(PacketByteBuf buf) - { - var pitch = buf.readFloat(); - var yaw = buf.readFloat(); - var roll = buf.readFloat(); - return new EulerAngle(pitch, yaw, roll); - } -} diff --git a/projects/pswg/src/main/java/com/parzivail/util/item/ItemAction.java b/projects/pswg/src/main/java/com/parzivail/util/item/ItemAction.java index 89cf6d9f6..2a5ca8415 100644 --- a/projects/pswg/src/main/java/com/parzivail/util/item/ItemAction.java +++ b/projects/pswg/src/main/java/com/parzivail/util/item/ItemAction.java @@ -3,5 +3,5 @@ public enum ItemAction { PRIMARY, - SECONDARY + SECONDARY, } diff --git a/projects/pswg/src/main/java/com/parzivail/util/network/PlayerPacketHandler.java b/projects/pswg/src/main/java/com/parzivail/util/network/PlayerPacketHandler.java index 4def679fa..07ce914b2 100644 --- a/projects/pswg/src/main/java/com/parzivail/util/network/PlayerPacketHandler.java +++ b/projects/pswg/src/main/java/com/parzivail/util/network/PlayerPacketHandler.java @@ -1,11 +1,14 @@ package com.parzivail.util.network; import com.parzivail.pswg.component.PlayerData; +import com.parzivail.pswg.network.PlayerItemLeftClickC2SPacket; +import com.parzivail.pswg.network.UnitC2SPacket; import com.parzivail.util.ParziUtil; import com.parzivail.util.item.IItemActionListener; import com.parzivail.util.item.ILeftClickConsumer; import com.parzivail.util.item.ItemAction; import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketByteBuf; @@ -16,34 +19,29 @@ public class PlayerPacketHandler { - public static void handleLeftClickPacket(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleLeftClickPacket(PlayerItemLeftClickC2SPacket packet, ServerPlayNetworking.Context context) { - var hand = Hand.values()[buf.readInt()]; - var isRepeatEvent = buf.readBoolean(); + if (context.player().isSpectator()) + return; - server.execute(() -> { - if (player.isSpectator()) - return; - - var stack = player.getStackInHand(hand); + var stack = context.player().getStackInHand(packet.hand()); - if (stack.getItem() instanceof ILeftClickConsumer) - ((ILeftClickConsumer)stack.getItem()).useLeft(player.getWorld(), player, hand, isRepeatEvent); - }); + if (stack.getItem() instanceof ILeftClickConsumer) + ((ILeftClickConsumer)stack.getItem()).useLeft(context.player().getWorld(), context.player(), packet.hand(), packet.isRepeatEvent()); } public static void handleItemAction(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { var action = buf.readInt(); - server.execute(() -> { - var actions = ItemAction.values(); - if (action < 0 || action >= actions.length) - { - ParziUtil.LOG.warn("Player %s attempted to use invalid item action ordinal %s", player, action); - return; - } + var actions = ItemAction.values(); + if (action < 0 || action >= actions.length) + { + ParziUtil.LOG.warn("Player %s attempted to use invalid item action ordinal %s", player, action); + return; + } + server.execute(() -> { var stack = getStackInHand(player, false); if (stack.getItem() instanceof IItemActionListener) ((IItemActionListener)stack.getItem()).onItemAction(player.getWorld(), player, stack, actions[action]); @@ -66,13 +64,11 @@ private static ItemStack getStackInHand(PlayerEntity player, boolean altHand) return player.getMainHandStack(); } - public static void handleTogglePatrolPosture(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) + public static void handleTogglePatrolPosture(UnitC2SPacket packet, ServerPlayNetworking.Context context) { - server.execute(() -> { - var data = PlayerData.getVolatilePublic(player); + var data = PlayerData.getVolatilePublic(context.player()); - data.setPatrolPosture(!data.isPatrolPosture()); - data.syncAll(); - }); + data.setPatrolPosture(!data.isPatrolPosture()); + data.syncAll(); } } diff --git a/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntitySpawnS2CPacket.java b/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntitySpawnS2CPacket.java index 2f743299d..fa74fcb2c 100644 --- a/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntitySpawnS2CPacket.java +++ b/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntitySpawnS2CPacket.java @@ -1,50 +1,56 @@ package com.parzivail.util.network; -import com.parzivail.util.data.PacketByteBufHelper; +import com.parzivail.pswg.container.SwgPackets; import com.parzivail.util.entity.IPrecisionSpawnEntity; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.entity.Entity; import net.minecraft.nbt.NbtCompound; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.listener.ClientPlayPacketListener; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket; -import net.minecraft.util.Identifier; +import net.minecraft.server.network.EntityTrackerEntry; import net.minecraft.util.math.Vec3d; -public class PreciseEntitySpawnS2CPacket extends EntitySpawnS2CPacket +public class PreciseEntitySpawnS2CPacket extends EntitySpawnS2CPacket implements CustomPayload { + public static final PacketCodec CODEC = PacketCodec.of( + PreciseEntitySpawnS2CPacket::write, PreciseEntitySpawnS2CPacket::new + ); + private final Vec3d velocity; private final NbtCompound data; - private PreciseEntitySpawnS2CPacket(Entity entity, int entityData) + public PreciseEntitySpawnS2CPacket(Entity entity, EntityTrackerEntry entityTrackerEntry, int entityData) { - super(entity, entityData); + super(entity, entityTrackerEntry, entityData); this.velocity = entity.getVelocity(); this.data = new NbtCompound(); if (entity instanceof IPrecisionSpawnEntity pse) pse.writeSpawnData(data); } - public PreciseEntitySpawnS2CPacket(PacketByteBuf buf) + public PreciseEntitySpawnS2CPacket(RegistryByteBuf buf) { super(buf); - this.velocity = PacketByteBufHelper.readVec3d(buf); + this.velocity = buf.readVec3d(); this.data = buf.readNbt(); } @Override - public void write(PacketByteBuf buf) + protected void write(RegistryByteBuf buf) { super.write(buf); - PacketByteBufHelper.writeVec3d(buf, velocity); + buf.writeVec3d(velocity); buf.writeNbt(data); } + @Override + public Id getId() + { + return SwgPackets.S2C.PreciseEntitySpawn; + } + public Vec3d getVelocity() { return velocity; @@ -55,18 +61,9 @@ public NbtCompound getData() return data; } - @SuppressWarnings("unchecked") - public static Packet createPacket(Identifier id, Entity entity, int entityData) - { - var passedData = new PacketByteBuf(Unpooled.buffer()); - new PreciseEntitySpawnS2CPacket(entity, entityData).write(passedData); - return (Packet)(Object)ServerPlayNetworking.createS2CPacket(id, passedData); - } - - public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender sender) + public static void handle(PreciseEntitySpawnS2CPacket packet, ClientPlayNetworking.Context context) { - var packet = new PreciseEntitySpawnS2CPacket(buf); - client.execute(() -> handler.onEntitySpawn(packet)); + context.client().getNetworkHandler().onEntitySpawn(packet); } } diff --git a/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntityVelocityUpdateS2CPacket.java b/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntityVelocityUpdateS2CPacket.java index e621a4b10..491d776dd 100644 --- a/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntityVelocityUpdateS2CPacket.java +++ b/projects/pswg/src/main/java/com/parzivail/util/network/PreciseEntityVelocityUpdateS2CPacket.java @@ -1,19 +1,17 @@ package com.parzivail.util.network; -import com.parzivail.util.data.PacketByteBufHelper; -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; +import com.parzivail.pswg.container.SwgPackets; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.entity.Entity; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.s2c.play.EntityVelocityUpdateS2CPacket; import net.minecraft.util.math.Vec3d; -public class PreciseEntityVelocityUpdateS2CPacket extends EntityVelocityUpdateS2CPacket +public class PreciseEntityVelocityUpdateS2CPacket extends EntityVelocityUpdateS2CPacket implements CustomPayload { - public static final PacketCodec CODEC = Packet.createCodec( + public static final PacketCodec CODEC = PacketCodec.of( PreciseEntityVelocityUpdateS2CPacket::write, PreciseEntityVelocityUpdateS2CPacket::new ); @@ -35,16 +33,22 @@ public PreciseEntityVelocityUpdateS2CPacket(int id, Vec3d position, Vec3d veloci public PreciseEntityVelocityUpdateS2CPacket(PacketByteBuf buf) { super(buf); - this.position = PacketByteBufHelper.readVec3d(buf); - this.velocity = PacketByteBufHelper.readVec3d(buf); + this.position = buf.readVec3d(); + this.velocity = buf.readVec3d(); + } + + @Override + public Id getId() + { + return SwgPackets.S2C.PreciseEntityVelocityUpdate; } @Override public void write(PacketByteBuf buf) { super.write(buf); - PacketByteBufHelper.writeVec3d(buf, position); - PacketByteBufHelper.writeVec3d(buf, velocity); + buf.writeVec3d(position); + buf.writeVec3d(velocity); } public Vec3d getVelocity() @@ -57,10 +61,9 @@ public Vec3d getPosition() return position; } - public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender sender) + public static void handle(PreciseEntityVelocityUpdateS2CPacket packet, ClientPlayNetworking.Context context) { - var packet = new PreciseEntityVelocityUpdateS2CPacket(buf); - client.execute(() -> handler.onEntityVelocityUpdate(packet)); + context.client().getNetworkHandler().onEntityVelocityUpdate(packet); } } diff --git a/projects/toolkit/src/main/java/com/parzivail/aurek/ui/view/addonbuilder/LightsaberAddonFeature.java b/projects/toolkit/src/main/java/com/parzivail/aurek/ui/view/addonbuilder/LightsaberAddonFeature.java index fd3192a29..0eb833899 100644 --- a/projects/toolkit/src/main/java/com/parzivail/aurek/ui/view/addonbuilder/LightsaberAddonFeature.java +++ b/projects/toolkit/src/main/java/com/parzivail/aurek/ui/view/addonbuilder/LightsaberAddonFeature.java @@ -7,7 +7,6 @@ import com.parzivail.pswg.features.lightsabers.LightsaberItem; import com.parzivail.pswg.features.lightsabers.client.LightsaberItemRenderer; import com.parzivail.pswg.features.lightsabers.data.LightsaberBladeType; -import com.parzivail.util.data.PacketByteBufHelper; import com.parzivail.util.math.ColorUtil; import imgui.flag.ImGuiColorEditFlags; import imgui.flag.ImGuiTableColumnFlags; @@ -132,7 +131,7 @@ public void serialize(String namespace, ZipOutputStream zip) var hasOwner = getNameType() == NameType.Owner; var ownerName = hasOwner ? getName() : null; - PacketByteBufHelper.writeNullable(buf, ownerName, PacketByteBuf::writeString); + buf.writeNullable(ownerName, PacketByteBuf::writeString); buf.writeBoolean(unstable.get()); buf.writeString(LightsaberBladeType.values()[bladeType.get()].getId());