From 1763f433d4c7a1e09f6b89170781b3f73725e910 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 29 Dec 2022 22:37:10 +0100 Subject: [PATCH] Switch to using the simple networking apis provided by fabwork --- .../unicopia/ability/AbilityDispatcher.java | 4 +- .../magic/spell/trait/TraitDiscovery.java | 4 +- .../block/data/BlockDestructionManager.java | 2 +- .../client/gui/DismissSpellScreen.java | 2 +- .../client/gui/TribeSelectionScreen.java | 2 +- .../client/gui/spellbook/SpellbookScreen.java | 2 +- .../container/SpellbookChapterLoader.java | 4 +- .../unicopia/entity/SpellbookEntity.java | 2 +- .../unicopia/entity/player/Pony.java | 6 +- .../unicopia/network/Channel.java | 20 ++-- .../unicopia/network/MsgBlockDestruction.java | 2 +- .../network/MsgCancelPlayerAbility.java | 2 +- .../unicopia/network/MsgMarkTraitRead.java | 2 +- .../unicopia/network/MsgPlayerAbility.java | 4 +- .../network/MsgPlayerAnimationChange.java | 2 +- .../network/MsgPlayerCapabilities.java | 2 +- .../unicopia/network/MsgRemoveSpell.java | 2 +- .../network/MsgRequestSpeciesChange.java | 4 +- .../unicopia/network/MsgServerResources.java | 2 +- .../unicopia/network/MsgSpawnProjectile.java | 2 +- .../network/MsgSpellbookStateChanged.java | 2 +- .../unicopia/network/MsgTribeSelect.java | 2 +- .../unicopia/network/MsgUnlockTraits.java | 2 +- .../unicopia/util/network/C2SPacketType.java | 21 ---- .../unicopia/util/network/Packet.java | 35 ------- .../util/network/S2CBroadcastPacketType.java | 22 ----- .../unicopia/util/network/S2CPacketType.java | 22 ----- .../util/network/SimpleNetworking.java | 96 ------------------- 28 files changed, 35 insertions(+), 239 deletions(-) delete mode 100644 src/main/java/com/minelittlepony/unicopia/util/network/C2SPacketType.java delete mode 100644 src/main/java/com/minelittlepony/unicopia/util/network/Packet.java delete mode 100644 src/main/java/com/minelittlepony/unicopia/util/network/S2CBroadcastPacketType.java delete mode 100644 src/main/java/com/minelittlepony/unicopia/util/network/S2CPacketType.java delete mode 100644 src/main/java/com/minelittlepony/unicopia/util/network/SimpleNetworking.java diff --git a/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java b/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java index 5ad2fbd3..f680ec2e 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java @@ -46,7 +46,7 @@ public class AbilityDispatcher implements Tickable, NbtSerialisable { private boolean triggerQuickAction(Ability ability, ActivationType pressType) { Optional data = ability.prepareQuickAction(player, pressType); if (ability.onQuickAction(player, pressType, data)) { - Channel.CLIENT_PLAYER_ABILITY.send(new MsgPlayerAbility<>(ability, data, pressType)); + Channel.CLIENT_PLAYER_ABILITY.sendToServer(new MsgPlayerAbility<>(ability, data, pressType)); return true; } return false; @@ -209,7 +209,7 @@ public class AbilityDispatcher implements Tickable, NbtSerialisable { Optional data = ability.prepare(player); if (data.isPresent()) { - Channel.CLIENT_PLAYER_ABILITY.send(new MsgPlayerAbility<>(ability, data, ActivationType.NONE)); + Channel.CLIENT_PLAYER_ABILITY.sendToServer(new MsgPlayerAbility<>(ability, data, ActivationType.NONE)); } else { player.asEntity().playSound(USounds.GUI_ABILITY_FAIL, 1, 1); setCooldown(0); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitDiscovery.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitDiscovery.java index 19de23fd..a4c5c244 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitDiscovery.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitDiscovery.java @@ -52,7 +52,7 @@ public class TraitDiscovery implements NbtSerialisable { @Environment(EnvType.CLIENT) public void markRead(Trait trait) { - Channel.MARK_TRAIT_READ.send(new MsgMarkTraitRead(Set.of(trait))); + Channel.MARK_TRAIT_READ.sendToServer(new MsgMarkTraitRead(Set.of(trait))); } public void markRead(Set traits) { @@ -76,7 +76,7 @@ public class TraitDiscovery implements NbtSerialisable { unreadTraits.addAll(newTraits); pony.setDirty(); if (!newTraits.isEmpty() && !pony.asWorld().isClient) { - Channel.UNLOCK_TRAITS.send((ServerPlayerEntity)pony.asEntity(), new MsgUnlockTraits(newTraits)); + Channel.UNLOCK_TRAITS.sendToPlayer(new MsgUnlockTraits(newTraits), (ServerPlayerEntity)pony.asEntity()); } } diff --git a/src/main/java/com/minelittlepony/unicopia/block/data/BlockDestructionManager.java b/src/main/java/com/minelittlepony/unicopia/block/data/BlockDestructionManager.java index f05db351..665ab81c 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/data/BlockDestructionManager.java +++ b/src/main/java/com/minelittlepony/unicopia/block/data/BlockDestructionManager.java @@ -90,7 +90,7 @@ public class BlockDestructionManager implements Tickable { players.forEach(player -> { if (player instanceof ServerPlayerEntity) { - Channel.SERVER_BLOCK_DESTRUCTION.send(player, msg); + Channel.SERVER_BLOCK_DESTRUCTION.sendToPlayer(msg, player); } }); } diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java index 754739e1..ec62bee2 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java @@ -146,7 +146,7 @@ public class DismissSpellScreen extends GameGui { if (isMouseOver(relativeMouseX, relativeMouseY)) { remove(this); pony.getSpellSlot().removeIf(spell -> spell == this.spell, true); - Channel.REMOVE_SPELL.send(new MsgRemoveSpell(spell)); + Channel.REMOVE_SPELL.sendToServer(new MsgRemoveSpell(spell)); playClickEffect(); return true; } 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 2abe8028..72f7d312 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.send(new MsgRequestSpeciesChange(race, true)); + Channel.CLIENT_REQUEST_SPECIES_CHANGE.sendToServer(new MsgRequestSpeciesChange(race, true)); finish(); } else { client.setScreen(this); diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java index 3889d8b0..772518c1 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java @@ -70,7 +70,7 @@ public class SpellbookScreen extends HandledScreen imple return chapters.getCurrentChapter() == craftingChapter; }); handler.getSpellbookState().setSynchronizer(state -> { - Channel.CLIENT_SPELLBOOK_UPDATE.send(new MsgSpellbookStateChanged(handler.syncId, state)); + Channel.CLIENT_SPELLBOOK_UPDATE.sendToServer(new MsgSpellbookStateChanged(handler.syncId, state)); }); } diff --git a/src/main/java/com/minelittlepony/unicopia/container/SpellbookChapterLoader.java b/src/main/java/com/minelittlepony/unicopia/container/SpellbookChapterLoader.java index fce8d434..002a8d0a 100644 --- a/src/main/java/com/minelittlepony/unicopia/container/SpellbookChapterLoader.java +++ b/src/main/java/com/minelittlepony/unicopia/container/SpellbookChapterLoader.java @@ -55,9 +55,7 @@ public class SpellbookChapterLoader extends JsonDataLoader implements Identifiab dirty = false; MsgServerResources msg = new MsgServerResources(); server.getWorlds().forEach(world -> { - world.getPlayers().forEach(player -> { - Channel.SERVER_RESOURCES_SEND.send(player, msg); - }); + Channel.SERVER_RESOURCES_SEND.sendToAllPlayers(msg, world); }); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java index 77d93cf3..147bc82b 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java @@ -58,7 +58,7 @@ public class SpellbookEntity extends MobEntity { if (player instanceof ServerPlayerEntity recipient && player.currentScreenHandler instanceof SpellbookScreenHandler book && getUuid().equals(book.entityId)) { - Channel.SERVER_SPELLBOOK_UPDATE.send(recipient, new MsgSpellbookStateChanged<>(player.currentScreenHandler.syncId, state)); + Channel.SERVER_SPELLBOOK_UPDATE.sendToPlayer(new MsgSpellbookStateChanged<>(player.currentScreenHandler.syncId, state), recipient); } }); }); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index aae889cf..13c2c1cf 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -131,7 +131,7 @@ public class Pony extends Living implements Copyable, Update this.animationMaxDuration = animationDuration; if (!isClient()) { - Channel.SERVER_PLAYER_ANIMATION_CHANGE.send(asWorld(), new MsgPlayerAnimationChange(this, animation, animationDuration)); + Channel.SERVER_PLAYER_ANIMATION_CHANGE.sendToAllPlayers(new MsgPlayerAnimationChange(this, animation, animationDuration), asWorld()); } animation.getSound().ifPresent(sound -> { @@ -243,8 +243,8 @@ public class Pony extends Living implements Copyable, Update if (entity instanceof ServerPlayerEntity) { MsgOtherPlayerCapabilities packet = new MsgOtherPlayerCapabilities(this); - Channel.SERVER_PLAYER_CAPABILITIES.send((ServerPlayerEntity)entity, packet); - Channel.SERVER_OTHER_PLAYER_CAPABILITIES.send(entity.world, packet); + Channel.SERVER_PLAYER_CAPABILITIES.sendToPlayer(packet, (ServerPlayerEntity)entity); + Channel.SERVER_OTHER_PLAYER_CAPABILITIES.sendToSurroundingPlayers(packet, entity); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index 9eeb1643..286cf16e 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -1,16 +1,12 @@ package com.minelittlepony.unicopia.network; -import com.minelittlepony.unicopia.util.network.S2CBroadcastPacketType; -import com.minelittlepony.unicopia.util.network.S2CPacketType; import com.minelittlepony.unicopia.*; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.network.C2SPacketType; -import com.minelittlepony.unicopia.util.network.SimpleNetworking; +import com.sollace.fabwork.api.packets.*; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; public interface Channel { C2SPacketType> CLIENT_PLAYER_ABILITY = SimpleNetworking.clientToServer(Unicopia.id("player_ability"), MsgPlayerAbility::new); @@ -24,17 +20,15 @@ public interface Channel { S2CPacketType CANCEL_PLAYER_ABILITY = SimpleNetworking.serverToClient(Unicopia.id("player_ability_cancel"), MsgCancelPlayerAbility::new); S2CPacketType UNLOCK_TRAITS = SimpleNetworking.serverToClient(Unicopia.id("unlock_traits"), MsgUnlockTraits::new); - Identifier SERVER_SELECT_TRIBE_ID = Unicopia.id("select_tribe"); - S2CPacketType SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(SERVER_SELECT_TRIBE_ID, MsgTribeSelect::new); + S2CPacketType SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(Unicopia.id("select_tribe"), MsgTribeSelect::new); S2CPacketType> SERVER_SPELLBOOK_UPDATE = SimpleNetworking.serverToClient(Unicopia.id("server_spellbook_update"), MsgSpellbookStateChanged::new); C2SPacketType> CLIENT_SPELLBOOK_UPDATE = SimpleNetworking.clientToServer(Unicopia.id("client_spellbook_update"), MsgSpellbookStateChanged::new); - Identifier SERVER_RESOURCES_SEND_ID = Unicopia.id("resources_send"); - S2CPacketType SERVER_RESOURCES_SEND = SimpleNetworking.serverToClient(SERVER_RESOURCES_SEND_ID, MsgServerResources::new); + S2CPacketType SERVER_RESOURCES_SEND = SimpleNetworking.serverToClient(Unicopia.id("resources_send"), MsgServerResources::new); - S2CBroadcastPacketType SERVER_OTHER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClients(Unicopia.id("other_player_capabilities"), MsgOtherPlayerCapabilities::new); - S2CBroadcastPacketType SERVER_PLAYER_ANIMATION_CHANGE = SimpleNetworking.serverToClients(Unicopia.id("other_player_animation_change"), MsgPlayerAnimationChange::new); + S2CPacketType SERVER_OTHER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClient(Unicopia.id("other_player_capabilities"), MsgOtherPlayerCapabilities::new); + S2CPacketType SERVER_PLAYER_ANIMATION_CHANGE = SimpleNetworking.serverToClient(Unicopia.id("other_player_animation_change"), MsgPlayerAnimationChange::new); static void bootstrap() { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { @@ -48,10 +42,10 @@ 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.getId(), new MsgTribeSelect(handler.player).toBuffer()); + sender.sendPacket(SERVER_SELECT_TRIBE.id(), new MsgTribeSelect(handler.player).toBuffer()); } } - sender.sendPacket(SERVER_RESOURCES_SEND.getId(), new MsgServerResources().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 a1cc8c21..7caed7c2 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java @@ -1,7 +1,7 @@ package com.minelittlepony.unicopia.network; import com.minelittlepony.unicopia.InteractionManager; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java b/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java index dbcc1e3f..473c999a 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java @@ -1,7 +1,7 @@ package com.minelittlepony.unicopia.network; import com.minelittlepony.unicopia.InteractionManager; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java b/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java index 9374bdca..d7fd199f 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgMarkTraitRead.java @@ -5,7 +5,7 @@ import java.util.Set; import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayerEntity; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java index f53f51ea..2ec0c3d5 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java @@ -7,7 +7,7 @@ import com.minelittlepony.unicopia.ability.Ability; import com.minelittlepony.unicopia.ability.ActivationType; import com.minelittlepony.unicopia.ability.data.Hit; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayerEntity; @@ -52,7 +52,7 @@ public class MsgPlayerAbility implements Packet power.canApply(player, data)).ifPresentOrElse( data -> power.apply(player, data), - () -> Channel.CANCEL_PLAYER_ABILITY.send(sender, new MsgCancelPlayerAbility()) + () -> Channel.CANCEL_PLAYER_ABILITY.sendToPlayer(new MsgCancelPlayerAbility(), sender) ); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java index af2170e8..1fcad8e7 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAnimationChange.java @@ -4,7 +4,7 @@ import java.util.UUID; import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.player.PlayerEntity; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java index a7573c02..1b446560 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java @@ -7,7 +7,7 @@ import java.util.UUID; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufOutputStream; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java b/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java index f2257412..523c7ec5 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java @@ -4,7 +4,7 @@ import java.util.UUID; import com.minelittlepony.unicopia.ability.magic.spell.Spell; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayerEntity; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java index b9cec5f8..6d133dba 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java @@ -3,7 +3,7 @@ package com.minelittlepony.unicopia.network; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.WorldTribeManager; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayerEntity; @@ -53,6 +53,6 @@ public class MsgRequestSpeciesChange implements Packet { } } - Channel.SERVER_PLAYER_CAPABILITIES.send(sender, new MsgPlayerCapabilities(player)); + Channel.SERVER_PLAYER_CAPABILITIES.sendToPlayer(new MsgPlayerCapabilities(player), sender); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java b/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java index b7418f2d..cbccef22 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java @@ -6,7 +6,7 @@ import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.ability.data.tree.TreeTypeLoader; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.container.SpellbookChapterLoader; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java index d895f700..0cc0dc83 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java @@ -4,7 +4,7 @@ import java.util.Optional; import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.Owned; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgSpellbookStateChanged.java b/src/main/java/com/minelittlepony/unicopia/network/MsgSpellbookStateChanged.java index fe3e3129..f282f39b 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgSpellbookStateChanged.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgSpellbookStateChanged.java @@ -2,7 +2,7 @@ package com.minelittlepony.unicopia.network; import com.minelittlepony.unicopia.container.SpellbookScreenHandler; import com.minelittlepony.unicopia.container.SpellbookState; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java b/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java index d3dcecff..3578953f 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgTribeSelect.java @@ -5,7 +5,7 @@ import java.util.Set; import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.Race; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java b/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java index 7d9f55aa..7dd8efc9 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgUnlockTraits.java @@ -5,7 +5,7 @@ import java.util.Set; import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait; -import com.minelittlepony.unicopia.util.network.Packet; +import com.sollace.fabwork.api.packets.Packet; import net.minecraft.network.PacketByteBuf; import net.minecraft.entity.player.PlayerEntity; diff --git a/src/main/java/com/minelittlepony/unicopia/util/network/C2SPacketType.java b/src/main/java/com/minelittlepony/unicopia/util/network/C2SPacketType.java deleted file mode 100644 index e9d0d1a6..00000000 --- a/src/main/java/com/minelittlepony/unicopia/util/network/C2SPacketType.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.minelittlepony.unicopia.util.network; - -import com.google.common.base.Preconditions; - -import net.fabricmc.api.EnvType; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; - -/** - * A server packet type. Sent by the client to the server. - */ -public interface C2SPacketType> { - Identifier getId(); - - default void send(T packet) { - Preconditions.checkState(FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT, "Client packet send called by the server"); - ClientPlayNetworking.send(getId(), packet.toBuffer()); - } -} \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/unicopia/util/network/Packet.java b/src/main/java/com/minelittlepony/unicopia/util/network/Packet.java deleted file mode 100644 index e3131d11..00000000 --- a/src/main/java/com/minelittlepony/unicopia/util/network/Packet.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.minelittlepony.unicopia.util.network; - -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.PacketByteBuf; - -/** - * Represents a message that can be either send from the client to the server or back. - */ -public interface Packet

{ - /** - * Called to handle this packet on the receiving end. - * - * @param sender The player who initially sent this packet. - */ - void handle(P sender); - - /** - * Writes this packet to the supplied buffer prior to transmission. - * - * @param buffer The buffer to write to. - */ - void toBuffer(PacketByteBuf buffer); - - /** - * Writes this packet to a new buffer. - * - * @return The resulting buffer for transmission - */ - default PacketByteBuf toBuffer() { - PacketByteBuf buf = PacketByteBufs.create(); - toBuffer(buf); - return buf; - } -} \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/unicopia/util/network/S2CBroadcastPacketType.java b/src/main/java/com/minelittlepony/unicopia/util/network/S2CBroadcastPacketType.java deleted file mode 100644 index 643ec14e..00000000 --- a/src/main/java/com/minelittlepony/unicopia/util/network/S2CBroadcastPacketType.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.minelittlepony.unicopia.util.network; - -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; -import net.minecraft.world.World; - -/** - * A broadcast packet type. Sent by the server to all surrounding players. - */ -public interface S2CBroadcastPacketType> { - Identifier getId(); - - default void send(World world, T packet) { - world.getPlayers().forEach(player -> { - if (player instanceof ServerPlayerEntity) { - ServerPlayNetworking.send((ServerPlayerEntity)player, getId(), packet.toBuffer()); - } - }); - } -} diff --git a/src/main/java/com/minelittlepony/unicopia/util/network/S2CPacketType.java b/src/main/java/com/minelittlepony/unicopia/util/network/S2CPacketType.java deleted file mode 100644 index bf4ef076..00000000 --- a/src/main/java/com/minelittlepony/unicopia/util/network/S2CPacketType.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.minelittlepony.unicopia.util.network; - -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.listener.ClientPlayPacketListener; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; - -/** - * A client packet type. Sent by the server to a specific player. - */ -public interface S2CPacketType> { - Identifier getId(); - - default void send(ServerPlayerEntity recipient, T packet) { - ServerPlayNetworking.send(recipient, getId(), packet.toBuffer()); - } - - default net.minecraft.network.Packet toPacket(T packet) { - return ServerPlayNetworking.createS2CPacket(getId(), packet.toBuffer()); - } -} \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/unicopia/util/network/SimpleNetworking.java b/src/main/java/com/minelittlepony/unicopia/util/network/SimpleNetworking.java deleted file mode 100644 index 8fce0ecd..00000000 --- a/src/main/java/com/minelittlepony/unicopia/util/network/SimpleNetworking.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.minelittlepony.unicopia.util.network; - -import java.util.function.Function; - -import net.fabricmc.api.EnvType; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; - -/** - * A simplified, side-agnostic, and declaritive wrapper around {@link ServerPlayNetworking} and {@link ClientPlayNetworking} - * designed to bring networking in line with the declaritive/registered nature of other parts of Mojang's echosystem. - *

- * It is safe to call these methods from either the client or the server, so modders can implement a - * single static PacketTypes class with which they can easily send packets without worrying - * about the complexities of the network thread, which side to register a global receiver on, - * which method to use to send, or even whether their receiver is registered for a given player or not. - *

- * All of the above is handled in a black-box style by this class. - *

- *

    - *
  • Packets are automatically registered on the appropriate sides.
  • - *
  • Sending is done in the same way by calling `send` on your packet type.
  • - *
  • Your packet's handle method is executed on the main thread where it is safe to interact with the world.
  • - */ -public final class SimpleNetworking { - private SimpleNetworking() {throw new RuntimeException("new SimpleNetworking()");} - /** - * Registers a packet type for transmisison to the server. - *

    - * The returned handle can be used by the client to send messages to the active minecraft server. - *

    - * - * @param The type of packet to implement - * @param id The message's unique used for serialization - * @param factory A constructor returning new instances of the packet type - * - * @return A registered PacketType - */ - public static > C2SPacketType clientToServer(Identifier id, Function factory) { - ServerPlayNetworking.registerGlobalReceiver(id, (server, player, handler, buffer, responder) -> { - T packet = factory.apply(buffer); - server.execute(() -> packet.handle(player)); - }); - return () -> id; - } - /** - * Registers a packet type for transmission to the client. - * - * The returned handle can be used by the server to send messages to a given recipient. - * - * @param The type of packet to implement - * @param id The message's unique used for serialization - * @param factory A constructor returning new instances of the packet type - * - * @return A registered PacketType - */ - public static > S2CPacketType serverToClient(Identifier id, Function factory) { - if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - ClientProxy.register(id, factory); - } - return () -> id; - } - /** - * Registers a packet type for transmission to all clients. - * - * The returned handle can be used by the server to broadcast a message to all connected clients in a given dimension. - * - * @param The type of packet to implement - * @param id The message's unique used for serialization - * @param factory A constructor returning new instances of the packet type - * - * @return A registered PacketType - */ - public static > S2CBroadcastPacketType serverToClients(Identifier id, Function factory) { - if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - ClientProxy.register(id, factory); - } - return () -> id; - } - // Fabric's APIs are not side-agnostic. - // We punt this to a separate class file to keep it from being eager-loaded on a server environment. - private static final class ClientProxy { - private ClientProxy() {throw new RuntimeException("new ClientProxy()");} - public static > void register(Identifier id, Function factory) { - ClientPlayNetworking.registerGlobalReceiver(id, (client, ignore1, buffer, ignore2) -> { - T packet = factory.apply(buffer); - client.execute(() -> packet.handle(client.player)); - }); - } - } -}