Skip to content

Commit

Permalink
Fix most networking code
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-1000 committed Jul 26, 2024
1 parent 840f14f commit 10a55d5
Show file tree
Hide file tree
Showing 38 changed files with 477 additions and 495 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ByteBuf, EnumSet<JetpackControls>> PACKET_CODEC = PacketCodecs.SHORT.xmap(JetpackControls::unpack, JetpackControls::pack);

private final short flag;

JetpackControls(int flag)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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<ByteBuf, EnumSet<ShipControls>> PACKET_CODEC = PacketCodecs.INTEGER.xmap(ShipControls::unpack, ShipControls::pack);

private final int flag;

ShipControls(int flag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UnitC2SPacket> RequestCustomizeSelf = new CustomPayload.Id<>(Resources.id("open_character_customizer")); // unit
public static final CustomPayload.Id<SetOwnSpeciesC2SPacket> SetOwnSpecies = new CustomPayload.Id<>(Resources.id("set_own_species"));
public static final CustomPayload.Id<PlayerItemLeftClickC2SPacket> PlayerLeftClickItem = new CustomPayload.Id<>(Resources.id("player_use_left"));
public static final CustomPayload.Id<PlayerItemActionC2SPacket> PlayerItemAction = new CustomPayload.Id<>(Resources.id("item_action"));
public static final CustomPayload.Id<UnitC2SPacket> ShipFire = new CustomPayload.Id<>(Resources.id("ship_fire")); // unit
public static final CustomPayload.Id<ShipRotationC2SPacket> ShipRotation = new CustomPayload.Id<>(Resources.id("srot"));
public static final CustomPayload.Id<ShipControlsC2SPacket> ShipControls = new CustomPayload.Id<>(Resources.id("sctrl"));
public static final CustomPayload.Id<JetpackControlsC2SPacket> JetpackControls = new CustomPayload.Id<>(Resources.id("jctrl"));
public static final CustomPayload.Id<UnitC2SPacket> 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<PreciseEntityVelocityUpdateS2CPacket> PreciseEntityVelocityUpdate = new CustomPayload.Id<>(Resources.id("pevel"));
public static final CustomPayload.Id<PreciseEntitySpawnS2CPacket> PreciseEntitySpawn = new CustomPayload.Id<>(Resources.id("pesp"));
public static final CustomPayload.Id<OpenEntityInventoryS2CPacket> OpenEntityInventory = new CustomPayload.Id<>(Resources.id("entity_inventory"));
public static final CustomPayload.Id<AccumulateRecoilS2CPacket> AccumulateRecoil = new CustomPayload.Id<>(Resources.id("recoil"));
public static final CustomPayload.Id<BlasterHitS2CPacket> BlasterHit = new CustomPayload.Id<>(Resources.id("blaster_hit"));
public static final CustomPayload.Id<PlayerSocketSparksS2CPacket> PlayerSocketPyro = new CustomPayload.Id<>(Resources.id("player_sparks"));
public static final Identifier OpenCharacterCustomizer = Resources.id("open_character_customizer");
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
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;
import com.parzivail.pswg.client.render.player.PlayerSocket;
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.*;
Expand All @@ -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
);
}
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -120,7 +117,7 @@ public void setRange(float range)
public Packet<ClientPlayPacketListener> createSpawnPacket(EntityTrackerEntry entityTrackerEntry)
{
var entity = this.getOwner();
return PreciseEntitySpawnS2CPacket.createPacket(SwgPackets.S2C.PreciseEntitySpawn, this, entityTrackerEntry, entity == null ? 0 : entity.getId());
return (Packet<ClientPlayPacketListener>)(Packet<?>) ServerPlayNetworking.createS2CPacket(new PreciseEntitySpawnS2CPacket(this, entityTrackerEntry, entity == null ? 0 : entity.getId()));
}

@Override
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Loading

0 comments on commit 10a55d5

Please sign in to comment.