From 83932bfea1f8be586cf76ee5d8f9bbdae7d11412 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 30 Dec 2022 20:42:18 +0100 Subject: [PATCH] Update fabwork and switch a lot of the packets to records and replace the client handler with the new receiver method of registering callbacks --- gradle.properties | 2 +- .../unicopia/InteractionManager.java | 11 ++--- .../client/ClientInteractionManager.java | 12 ++--- .../unicopia/client/UnicopiaClient.java | 3 ++ .../client/gui/TribeSelectionScreen.java | 2 +- .../unicopia/network/Channel.java | 6 +-- .../unicopia/network/MsgBlockDestruction.java | 20 ++------ .../network/MsgCancelPlayerAbility.java | 17 +++---- .../unicopia/network/MsgMarkTraitRead.java | 10 +--- .../network/MsgOtherPlayerCapabilities.java | 1 - .../unicopia/network/MsgPlayerAbility.java | 26 +++++----- .../network/MsgPlayerAnimationChange.java | 17 +++---- .../unicopia/network/MsgRemoveSpell.java | 8 ++- .../network/MsgRequestSpeciesChange.java | 20 ++------ .../unicopia/network/MsgServerResources.java | 30 ++++++------ .../unicopia/network/MsgSpawnProjectile.java | 6 +-- .../network/MsgSpellbookStateChanged.java | 17 ++----- .../unicopia/network/MsgTribeSelect.java | 19 ++----- .../unicopia/network/MsgUnlockTraits.java | 13 ++--- .../network/handler/ClientNetworkHandler.java | 25 ---------- .../handler/ClientNetworkHandlerImpl.java | 49 +++++++++---------- 21 files changed, 109 insertions(+), 205 deletions(-) delete mode 100644 src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandler.java diff --git a/gradle.properties b/gradle.properties index be2e9edd..5342eaa9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,7 +20,7 @@ org.gradle.daemon=false modrinth_project_id= # Dependencies - fabwork_version=1.0.1 + fabwork_version=1.1.0 modmenu_version=5.0.0-alpha.3 minelp_version=4.7.2 kirin_version=1.13.2 diff --git a/src/main/java/com/minelittlepony/unicopia/InteractionManager.java b/src/main/java/com/minelittlepony/unicopia/InteractionManager.java index 0ab9703b..ec1d0696 100644 --- a/src/main/java/com/minelittlepony/unicopia/InteractionManager.java +++ b/src/main/java/com/minelittlepony/unicopia/InteractionManager.java @@ -1,5 +1,6 @@ package com.minelittlepony.unicopia; +import java.util.Map; import java.util.Optional; import org.jetbrains.annotations.NotNull; @@ -8,12 +9,13 @@ import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.ability.magic.CasterView; import com.minelittlepony.unicopia.block.data.Ether; import com.minelittlepony.unicopia.entity.player.dummy.DummyPlayerEntity; -import com.minelittlepony.unicopia.network.handler.ClientNetworkHandler; import com.mojang.authlib.GameProfile; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.PacketByteBuf; import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Identifier; import net.minecraft.world.BlockView; import net.minecraft.world.World; @@ -41,11 +43,8 @@ public class InteractionManager { return Optional.empty(); } - /** - * Returns the client network handler, or throws if called on the server. - */ - public ClientNetworkHandler getClientNetworkHandler() { - throw new NullPointerException("Client network handler called by the server"); + public Map readChapters(PacketByteBuf buf) { + throw new RuntimeException("Method not supported"); } /** diff --git a/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java b/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java index 09e7c2be..1c635855 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java +++ b/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java @@ -1,5 +1,6 @@ package com.minelittlepony.unicopia.client; +import java.util.Map; import java.util.Optional; import java.util.function.Predicate; @@ -12,12 +13,11 @@ import com.minelittlepony.unicopia.USounds; import com.minelittlepony.unicopia.ability.magic.CasterView; import com.minelittlepony.unicopia.block.data.Ether; import com.minelittlepony.unicopia.client.gui.DismissSpellScreen; +import com.minelittlepony.unicopia.client.gui.spellbook.ClientChapters; import com.minelittlepony.unicopia.client.sound.*; import com.minelittlepony.unicopia.entity.player.PlayerPhysics; import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.dummy.DummyClientPlayerEntity; -import com.minelittlepony.unicopia.network.handler.ClientNetworkHandler; -import com.minelittlepony.unicopia.network.handler.ClientNetworkHandlerImpl; import com.mojang.authlib.GameProfile; import net.minecraft.client.MinecraftClient; @@ -31,16 +31,17 @@ import net.minecraft.entity.LivingEntity; import net.minecraft.entity.passive.BeeEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.vehicle.AbstractMinecartEntity; +import net.minecraft.network.PacketByteBuf; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; +import net.minecraft.util.Identifier; import net.minecraft.util.math.random.Random; import net.minecraft.world.BlockView; import net.minecraft.world.World; public class ClientInteractionManager extends InteractionManager { - private final ClientNetworkHandler handler = new ClientNetworkHandlerImpl(); private final MinecraftClient client = MinecraftClient.getInstance(); private final Optional clientWorld = Optional.of(() -> MinecraftClient.getInstance().world); @@ -54,9 +55,8 @@ public class ClientInteractionManager extends InteractionManager { } @Override - @Nullable - public ClientNetworkHandler getClientNetworkHandler() { - return handler; + public Map readChapters(PacketByteBuf buffer) { + return buffer.readMap(PacketByteBuf::readIdentifier, ClientChapters::loadChapter); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java b/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java index e9818465..abd657d7 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java +++ b/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java @@ -16,6 +16,8 @@ import com.minelittlepony.unicopia.client.render.shader.ViewportShader; import com.minelittlepony.unicopia.container.*; import com.minelittlepony.unicopia.entity.player.PlayerCamera; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.network.handler.ClientNetworkHandlerImpl; + import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback; @@ -60,6 +62,7 @@ public class UnicopiaClient implements ClientModInitializer { @Override public void onInitializeClient() { InteractionManager.INSTANCE = new ClientInteractionManager(); + new ClientNetworkHandlerImpl(); KeyBindingsHandler.bootstrap(); URenderers.bootstrap(); diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java index 72f7d312..241a4c51 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java @@ -78,7 +78,7 @@ public class TribeSelectionScreen extends GameGui implements HidesHud { finished = false; if (result) { - Channel.CLIENT_REQUEST_SPECIES_CHANGE.sendToServer(new MsgRequestSpeciesChange(race, true)); + Channel.CLIENT_REQUEST_SPECIES_CHANGE.sendToServer(new MsgRequestSpeciesChange(true, race)); finish(); } else { client.setScreen(this); diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index 286cf16e..bf0f0191 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -9,7 +9,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.ServerPlayerEntity; public interface Channel { - C2SPacketType> CLIENT_PLAYER_ABILITY = SimpleNetworking.clientToServer(Unicopia.id("player_ability"), MsgPlayerAbility::new); + C2SPacketType> CLIENT_PLAYER_ABILITY = SimpleNetworking.clientToServer(Unicopia.id("player_ability"), MsgPlayerAbility::read); C2SPacketType CLIENT_REQUEST_SPECIES_CHANGE = SimpleNetworking.clientToServer(Unicopia.id("request_capabilities"), MsgRequestSpeciesChange::new); C2SPacketType MARK_TRAIT_READ = SimpleNetworking.clientToServer(Unicopia.id("mark_trait_read"), MsgMarkTraitRead::new); C2SPacketType REMOVE_SPELL = SimpleNetworking.clientToServer(Unicopia.id("remove_spell"), MsgRemoveSpell::new); @@ -17,7 +17,7 @@ public interface Channel { S2CPacketType SERVER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClient(Unicopia.id("player_capabilities"), MsgPlayerCapabilities::new); S2CPacketType SERVER_SPAWN_PROJECTILE = SimpleNetworking.serverToClient(Unicopia.id("projectile_entity"), MsgSpawnProjectile::new); S2CPacketType SERVER_BLOCK_DESTRUCTION = SimpleNetworking.serverToClient(Unicopia.id("block_destruction"), MsgBlockDestruction::new); - S2CPacketType CANCEL_PLAYER_ABILITY = SimpleNetworking.serverToClient(Unicopia.id("player_ability_cancel"), MsgCancelPlayerAbility::new); + S2CPacketType CANCEL_PLAYER_ABILITY = SimpleNetworking.serverToClient(Unicopia.id("player_ability_cancel"), MsgCancelPlayerAbility::read); S2CPacketType UNLOCK_TRAITS = SimpleNetworking.serverToClient(Unicopia.id("unlock_traits"), MsgUnlockTraits::new); S2CPacketType SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(Unicopia.id("select_tribe"), MsgTribeSelect::new); @@ -42,7 +42,7 @@ public interface Channel { pony.setSpecies(race); Unicopia.LOGGER.info("Setting {}'s race to {} due to host setting", handler.player.getDisplayName().getString(), Race.REGISTRY.getId(race).toString()); } else { - sender.sendPacket(SERVER_SELECT_TRIBE.id(), new MsgTribeSelect(handler.player).toBuffer()); + sender.sendPacket(SERVER_SELECT_TRIBE.id(), new MsgTribeSelect(Race.allPermitted(handler.player)).toBuffer()); } } sender.sendPacket(SERVER_RESOURCES_SEND.id(), new MsgServerResources().toBuffer()); diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java b/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java index 7caed7c2..321b1900 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java @@ -1,6 +1,5 @@ package com.minelittlepony.unicopia.network; -import com.minelittlepony.unicopia.InteractionManager; import com.sollace.fabwork.api.packets.Packet; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; @@ -11,26 +10,15 @@ import net.minecraft.entity.player.PlayerEntity; /** * Sent by the server to update block destruction progress on the client. */ -public class MsgBlockDestruction implements Packet { - - private final Long2ObjectMap destructions; - +public record MsgBlockDestruction (Long2ObjectMap destructions) implements Packet { MsgBlockDestruction(PacketByteBuf buffer) { - destructions = new Long2ObjectOpenHashMap<>(); + this(new Long2ObjectOpenHashMap<>()); int size = buffer.readInt(); for (int i = 0; i < size; i++) { destructions.put(buffer.readLong(), (Float)buffer.readFloat()); } } - public MsgBlockDestruction(Long2ObjectMap destructions) { - this.destructions = destructions; - } - - public Long2ObjectMap getDestructions() { - return destructions; - } - @Override public void toBuffer(PacketByteBuf buffer) { buffer.writeInt(destructions.size()); @@ -41,7 +29,5 @@ public class MsgBlockDestruction implements Packet { } @Override - public void handle(PlayerEntity sender) { - InteractionManager.instance().getClientNetworkHandler().handleBlockDestruction(this); - } + public void handle(PlayerEntity sender) {} } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java b/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java index 473c999a..c3fc5065 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java @@ -1,6 +1,5 @@ package com.minelittlepony.unicopia.network; -import com.minelittlepony.unicopia.InteractionManager; import com.sollace.fabwork.api.packets.Packet; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; @@ -8,20 +7,16 @@ import net.minecraft.network.PacketByteBuf; /** * Sent to the client when an ability fails its server-side activation checks. */ -public class MsgCancelPlayerAbility implements Packet { +public final class MsgCancelPlayerAbility implements Packet { + static final MsgCancelPlayerAbility INSTANCE = new MsgCancelPlayerAbility(); - MsgCancelPlayerAbility(PacketByteBuf buffer) { - } - - public MsgCancelPlayerAbility() { + static MsgCancelPlayerAbility read(PacketByteBuf buffer) { + return INSTANCE; } @Override - public void toBuffer(PacketByteBuf buffer) { - } + public void toBuffer(PacketByteBuf buffer) { } @Override - public void handle(PlayerEntity sender) { - InteractionManager.instance().getClientNetworkHandler().handleCancelAbility(this); - } + public void handle(PlayerEntity sender) { } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java b/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java index d7fd199f..3c87a3c0 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java @@ -10,21 +10,15 @@ import com.sollace.fabwork.api.packets.Packet; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayerEntity; -public class MsgMarkTraitRead implements Packet { - - public final Set traits = new HashSet<>(); - +public record MsgMarkTraitRead (Set traits) implements Packet { MsgMarkTraitRead(PacketByteBuf buffer) { + this(new HashSet<>()); int length = buffer.readInt(); for (int i = 0; i < length; i++) { Trait.fromId(buffer.readIdentifier()).ifPresent(traits::add); } } - public MsgMarkTraitRead(Set traits) { - this.traits.addAll(traits); - } - @Override public void toBuffer(PacketByteBuf buffer) { buffer.writeInt(traits.size()); diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java index 4318ad26..d0492f07 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java @@ -17,7 +17,6 @@ public class MsgOtherPlayerCapabilities extends MsgPlayerCapabilities { public MsgOtherPlayerCapabilities(Pony player) { super(player); - } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java index 2ec0c3d5..0553ebe9 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java @@ -15,22 +15,20 @@ import net.minecraft.server.network.ServerPlayerEntity; /** * Sent to the server when a player activates an ability. */ -public class MsgPlayerAbility implements Packet { - private final Ability power; - private final Optional data; - private final ActivationType type; +public record MsgPlayerAbility ( + Ability power, + Optional data, + ActivationType type + ) implements Packet { @SuppressWarnings("unchecked") - MsgPlayerAbility(PacketByteBuf buffer) { - power = (Ability) Abilities.REGISTRY.get(buffer.readIdentifier()); - data = buffer.readOptional(power.getSerializer()::fromBuffer); - type = ActivationType.of(buffer.readInt()); - } - - public MsgPlayerAbility(Ability power, Optional data, ActivationType type) { - this.power = power; - this.data = data; - this.type = type; + static MsgPlayerAbility read(PacketByteBuf buffer) { + Ability power = (Ability) Abilities.REGISTRY.get(buffer.readIdentifier()); + return new MsgPlayerAbility<>( + power, + buffer.readOptional(power.getSerializer()::fromBuffer), + ActivationType.of(buffer.readInt()) + ); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java index 1fcad8e7..bcde2bb1 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java @@ -13,21 +13,18 @@ import net.minecraft.network.PacketByteBuf; /** * Sent to the client when a player's animation changes. */ -public class MsgPlayerAnimationChange implements Packet { - private final UUID playerId; - private final Animation animation; - private final int duration; +public record MsgPlayerAnimationChange ( + UUID playerId, + Animation animation, + int duration + ) implements Packet { MsgPlayerAnimationChange(PacketByteBuf buffer) { - playerId = buffer.readUuid(); - animation = Animation.values()[buffer.readInt()]; - duration = buffer.readInt(); + this(buffer.readUuid(), buffer.readEnumConstant(Animation.class), buffer.readInt()); } public MsgPlayerAnimationChange(Pony player, Animation animation, int duration) { - this.playerId = player.asEntity().getUuid(); - this.animation = animation; - this.duration = duration; + this(player.asEntity().getUuid(), animation, duration); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java b/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java index 523c7ec5..d0221a6d 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java @@ -12,15 +12,13 @@ import net.minecraft.server.network.ServerPlayerEntity; /** * Sent to the server when a player activates an ability. */ -public class MsgRemoveSpell implements Packet { - private final UUID id; - +public record MsgRemoveSpell (UUID id) implements Packet { MsgRemoveSpell(PacketByteBuf buffer) { - id = buffer.readUuid(); + this(buffer.readUuid()); } public MsgRemoveSpell(Spell spell) { - id = spell.getUuid(); + this(spell.getUuid()); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java index 6d133dba..d13b3bbd 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java @@ -14,23 +14,13 @@ import net.minecraft.server.world.ServerWorld; *

* The server responds back with the accepted capabilities and the race the client should use (if the preferred was not permitted) */ -public class MsgRequestSpeciesChange implements Packet { - - private final boolean force; - private final Race newRace; +public record MsgRequestSpeciesChange ( + boolean force, + Race newRace + ) implements Packet { MsgRequestSpeciesChange(PacketByteBuf buffer) { - force = buffer.readBoolean(); - newRace = buffer.readRegistryValue(Race.REGISTRY); - } - - public MsgRequestSpeciesChange(Race newRace) { - this(newRace, false); - } - - public MsgRequestSpeciesChange(Race newRace, boolean force) { - this.newRace = newRace; - this.force = force; + this(buffer.readBoolean(), buffer.readRegistryValue(Race.REGISTRY)); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java b/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java index cbccef22..0b4cb05e 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java @@ -12,21 +12,25 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; -public class MsgServerResources implements Packet { - public final Map traits; - public final Map chapters; - public final Map treeTypes; - +public record MsgServerResources ( + Map traits, + Map chapters, + Map treeTypes + ) implements Packet { public MsgServerResources() { - traits = SpellTraits.all(); - chapters = SpellbookChapterLoader.INSTANCE.getChapters(); - treeTypes = TreeTypeLoader.INSTANCE.getEntries(); + this( + SpellTraits.all(), + SpellbookChapterLoader.INSTANCE.getChapters(), + TreeTypeLoader.INSTANCE.getEntries() + ); } public MsgServerResources(PacketByteBuf buffer) { - traits = buffer.readMap(PacketByteBuf::readIdentifier, SpellTraits::fromPacket); - chapters = InteractionManager.instance().getClientNetworkHandler().readChapters(buffer); - treeTypes = buffer.readMap(PacketByteBuf::readIdentifier, TreeTypeLoader.TreeTypeDef::new); + this( + buffer.readMap(PacketByteBuf::readIdentifier, SpellTraits::fromPacket), + InteractionManager.instance().readChapters(buffer), + buffer.readMap(PacketByteBuf::readIdentifier, TreeTypeLoader.TreeTypeDef::new) + ); } @Override @@ -37,7 +41,5 @@ public class MsgServerResources implements Packet { } @Override - public void handle(PlayerEntity sender) { - InteractionManager.instance().getClientNetworkHandler().handleServerResources(this); - } + public void handle(PlayerEntity sender) { } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java index 0cc0dc83..1d0bff16 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java @@ -2,7 +2,6 @@ package com.minelittlepony.unicopia.network; import java.util.Optional; -import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.Owned; import com.sollace.fabwork.api.packets.Packet; @@ -30,9 +29,6 @@ public class MsgSpawnProjectile extends EntitySpawnS2CPacket implements Packet

implements Packet { - - private final int syncId; - private final SpellbookState state; - - public MsgSpellbookStateChanged(int syncId, SpellbookState state) { - this.syncId = syncId; - this.state = state; - } +public record MsgSpellbookStateChanged ( + int syncId, + SpellbookState state + ) implements Packet { public MsgSpellbookStateChanged(PacketByteBuf buffer) { - syncId = buffer.readInt(); - state = new SpellbookState().fromPacket(buffer); + this(buffer.readInt(), new SpellbookState().fromPacket(buffer)); } @Override @@ -35,7 +29,6 @@ public class MsgSpellbookStateChanged implements Packet< @Override public void handle(T sender) { - if (sender.currentScreenHandler.syncId != syncId) { return; } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java b/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java index 3578953f..f3b0107d 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java @@ -3,32 +3,21 @@ package com.minelittlepony.unicopia.network; import java.util.HashSet; import java.util.Set; -import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.Race; import com.sollace.fabwork.api.packets.Packet; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; -public class MsgTribeSelect implements Packet { - private final Set availableRaces; - - public MsgTribeSelect(PlayerEntity player) { - availableRaces = Race.allPermitted(player); - } - +public record MsgTribeSelect (Set availableRaces) implements Packet { public MsgTribeSelect(PacketByteBuf buffer) { + this(new HashSet<>()); int len = buffer.readInt(); - availableRaces = new HashSet<>(); while (len-- > 0) { availableRaces.add(buffer.readRegistryValue(Race.REGISTRY)); } } - public Set getRaces() { - return availableRaces; - } - @Override public void toBuffer(PacketByteBuf buffer) { buffer.writeInt(availableRaces.size()); @@ -36,7 +25,5 @@ public class MsgTribeSelect implements Packet { } @Override - public void handle(PlayerEntity sender) { - InteractionManager.instance().getClientNetworkHandler().handleTribeScreen(this); - } + public void handle(PlayerEntity sender) {} } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java b/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java index 7dd8efc9..ba9b395c 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java @@ -3,18 +3,15 @@ package com.minelittlepony.unicopia.network; import java.util.HashSet; import java.util.Set; -import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait; import com.sollace.fabwork.api.packets.Packet; import net.minecraft.network.PacketByteBuf; import net.minecraft.entity.player.PlayerEntity; -public class MsgUnlockTraits implements Packet { - - public final Set traits = new HashSet<>(); - +public record MsgUnlockTraits (Set traits) implements Packet { MsgUnlockTraits(PacketByteBuf buffer) { + this(new HashSet<>()); int length = buffer.readInt(); for (int i = 0; i < length; i++) { Trait.fromId(buffer.readIdentifier()).ifPresent(traits::add); @@ -22,7 +19,7 @@ public class MsgUnlockTraits implements Packet { } public MsgUnlockTraits(Set traits) { - this.traits.addAll(traits); + this.traits = new HashSet<>(traits); } @Override @@ -32,7 +29,5 @@ public class MsgUnlockTraits implements Packet { } @Override - public void handle(PlayerEntity sender) { - InteractionManager.instance().getClientNetworkHandler().handleUnlockTraits(this); - } + public void handle(PlayerEntity sender) { } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandler.java b/src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandler.java deleted file mode 100644 index da3cedcf..00000000 --- a/src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandler.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.minelittlepony.unicopia.network.handler; - -import java.util.Map; - -import com.minelittlepony.unicopia.network.*; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.util.Identifier; - -public interface ClientNetworkHandler { - - void handleTribeScreen(MsgTribeSelect packet); - - void handleSpawnProjectile(MsgSpawnProjectile packet); - - void handleBlockDestruction(MsgBlockDestruction packet); - - void handleCancelAbility(MsgCancelPlayerAbility packet); - - void handleUnlockTraits(MsgUnlockTraits packet); - - void handleServerResources(MsgServerResources packet); - - Map readChapters(PacketByteBuf buf); -} diff --git a/src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandlerImpl.java b/src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandlerImpl.java index ada59a7e..585fe663 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandlerImpl.java +++ b/src/main/java/com/minelittlepony/unicopia/network/handler/ClientNetworkHandlerImpl.java @@ -20,21 +20,27 @@ import com.minelittlepony.unicopia.network.*; import net.minecraft.client.MinecraftClient; import net.minecraft.client.world.ClientWorld; import net.minecraft.entity.Entity; -import net.minecraft.network.PacketByteBuf; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.Identifier; -public class ClientNetworkHandlerImpl implements ClientNetworkHandler { - +public class ClientNetworkHandlerImpl { private final MinecraftClient client = MinecraftClient.getInstance(); - @Override - public void handleTribeScreen(MsgTribeSelect packet) { - client.setScreen(new TribeSelectionScreen(packet.getRaces())); + public ClientNetworkHandlerImpl() { + Channel.SERVER_SELECT_TRIBE.receiver().addPersistentListener(this::handleTribeScreen); + Channel.SERVER_SPAWN_PROJECTILE.receiver().addPersistentListener(this::handleSpawnProjectile); + Channel.SERVER_BLOCK_DESTRUCTION.receiver().addPersistentListener(this::handleBlockDestruction); + Channel.CANCEL_PLAYER_ABILITY.receiver().addPersistentListener(this::handleCancelAbility); + Channel.UNLOCK_TRAITS.receiver().addPersistentListener(this::handleUnlockTraits); + Channel.SERVER_RESOURCES_SEND.receiver().addPersistentListener(this::handleServerResources); + } + + private void handleTribeScreen(PlayerEntity sender, MsgTribeSelect packet) { + client.setScreen(new TribeSelectionScreen(packet.availableRaces())); } @SuppressWarnings("unchecked") - @Override - public void handleSpawnProjectile(MsgSpawnProjectile packet) { + private void handleSpawnProjectile(PlayerEntity sender, MsgSpawnProjectile packet) { ClientWorld world = client.world; Entity entity = packet.getEntityType().create(world); @@ -57,38 +63,29 @@ public class ClientNetworkHandlerImpl implements ClientNetworkHandler { world.addEntity(packet.getId(), entity); } - @Override - public void handleBlockDestruction(MsgBlockDestruction packet) { + private void handleBlockDestruction(PlayerEntity sender, MsgBlockDestruction packet) { ClientBlockDestructionManager destr = ((ClientBlockDestructionManager.Source)client.worldRenderer).getDestructionManager(); - packet.getDestructions().forEach((i, d) -> { + packet.destructions().forEach((i, d) -> { destr.setBlockDestruction(i, d); }); } - @Override - public void handleCancelAbility(MsgCancelPlayerAbility packet) { + private void handleCancelAbility(PlayerEntity sender, MsgCancelPlayerAbility packet) { client.player.playSound(USounds.GUI_ABILITY_FAIL, 1, 1); Pony.of(client.player).getAbilities().getStats().forEach(s -> s.setCooldown(0)); } - @Override - public void handleUnlockTraits(MsgUnlockTraits packet) { - for (Trait trait : packet.traits) { + private void handleUnlockTraits(PlayerEntity sender, MsgUnlockTraits packet) { + for (Trait trait : packet.traits()) { DiscoveryToast.show(client.getToastManager(), trait.getSprite()); } } - @Override - public Map readChapters(PacketByteBuf buffer) { - return buffer.readMap(PacketByteBuf::readIdentifier, ClientChapters::loadChapter); - } - @SuppressWarnings("unchecked") - @Override - public void handleServerResources(MsgServerResources packet) { - SpellTraits.load(packet.traits); - ClientChapters.load((Map)packet.chapters); - TreeTypes.load(packet.treeTypes); + private void handleServerResources(PlayerEntity sender, MsgServerResources packet) { + SpellTraits.load(packet.traits()); + ClientChapters.load((Map)packet.chapters()); + TreeTypes.load(packet.treeTypes()); } }