From 15134cd665d9632f8876ff0fb5da83021b862d2a Mon Sep 17 00:00:00 2001 From: art-nova Date: Thu, 29 Feb 2024 02:45:05 +0100 Subject: [PATCH 1/2] Add basic architectury networking setup --- .../patterns/spells/OpCongrats.kt.jinja | 3 ++ .../client/LookClient.java.jinja | 25 +++++++++ .../networking/SetLookPitchS2CMsg.java.jinja | 52 +++++++++++++++++++ .../{{ classname }}Networking.java.jinja | 31 +++++++++++ .../{{ classname }}.java.jinja | 2 + .../assets/{{ modid }}/lang/en_us.json.jinja | 2 +- 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 template/{{ common_path }}/src/main/java/{{ package_path }}/client/LookClient.java.jinja create mode 100644 template/{{ common_path }}/src/main/java/{{ package_path }}/networking/SetLookPitchS2CMsg.java.jinja create mode 100644 template/{{ common_path }}/src/main/java/{{ package_path }}/networking/{{ classname }}Networking.java.jinja diff --git a/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja b/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja index 0bf43dd4..5a2dbcb4 100644 --- a/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja +++ b/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja @@ -18,6 +18,8 @@ import at.petrak.hexcasting.api.spell.mishaps.MishapBadEntity mojmap="net.minecraft.network.chat.Component", yarn="net.minecraft.text.Text", ) }}{% set Component = ns.import %} +import {{ package }}.networking.{{ classname }}Networking +import {{ package }}.networking.SetLookPitchS2CMsg class OpCongrats : SpellAction { /** @@ -66,6 +68,7 @@ class OpCongrats : SpellAction { private data class Spell(val player: {{ ServerPlayer }}) : RenderedSpell { override fun cast(ctx: CastingContext) { ctx.caster.{{ _(mojmap="sendSystemMessage", yarn="sendMessage") }}({{ Component }}.translatable("text.{{ modid }}.congrats", player.displayName)); + {{ classname }}Networking.sendToPlayer(ctx.caster, SetLookPitchS2CMsg(-90f)) } } } diff --git a/template/{{ common_path }}/src/main/java/{{ package_path }}/client/LookClient.java.jinja b/template/{{ common_path }}/src/main/java/{{ package_path }}/client/LookClient.java.jinja new file mode 100644 index 00000000..fcb2f0cb --- /dev/null +++ b/template/{{ common_path }}/src/main/java/{{ package_path }}/client/LookClient.java.jinja @@ -0,0 +1,25 @@ +{% from "macros/mappings.jinja" import mappings as _, import, ns with context -%} + +package {{ package }}.client; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +{{ import( + mojmap="net.minecraft.client.Minecraft", + yarn="net.minecraft.client.MinecraftClient", +) }}{% set MinecraftClient = ns.import %}; + +/** + * This class is created to separate client-only code into a dedicated package. + */ +@Environment(EnvType.CLIENT) +public class LookClient { + /** + * Alters the tilt of the player's head. + * @param pitch New pitch or {@code null} + */ + public static void setLookPitch(float pitch) { + assert {{ MinecraftClient }}.getInstance().player != null; + {{ MinecraftClient }}.getInstance().player.{{ _(mojmap="setXRot", yarn="setPitch") }}(pitch); + } +} diff --git a/template/{{ common_path }}/src/main/java/{{ package_path }}/networking/SetLookPitchS2CMsg.java.jinja b/template/{{ common_path }}/src/main/java/{{ package_path }}/networking/SetLookPitchS2CMsg.java.jinja new file mode 100644 index 00000000..6325afd9 --- /dev/null +++ b/template/{{ common_path }}/src/main/java/{{ package_path }}/networking/SetLookPitchS2CMsg.java.jinja @@ -0,0 +1,52 @@ +{% from "macros/mappings.jinja" import mappings as _, import, ns with context -%} + +package {{ package }}.networking; + +import dev.architectury.networking.NetworkManager; +{{ import( + mojmap="net.minecraft.network.FriendlyByteBuf", + yarn="net.minecraft.network.PacketByteBuf", +) }}{% set ByteBuf = ns.import %}; +import {{ package }}.client.LookClient; +import java.util.function.Supplier; + +/** + * Class that implements the functionality necessary to create, send, and execute a command (in this case rotate view) + * from the server to a client in architectury. + */ +public class SetLookPitchS2CMsg { + private final float pitch; + + /** + * Constructor for creation on the server. + * @param pitch Head pitch parameter. + */ + public SetLookPitchS2CMsg(float pitch) { + this.pitch = pitch; + } + + /** + * Constructor for recreation on the client from a {@link {{ ByteBuf }}} onto which it was encoded + * on the server using {@link #encode({{ ByteBuf }})}. + * @param buf Buffer onto which the message was encoded + */ + public SetLookPitchS2CMsg({{ ByteBuf }} buf) { + this.pitch = buf.readFloat(); + } + + /** + * Method that encodes the message, preparing it for transmission over the network. + * @param buf Buffer onto which the message will be encoded + */ + public void encode({{ ByteBuf }} buf) { + buf.writeFloat(pitch); + } + + /** + * Executes the command. + * @param contextSupplier Supplier of the context that is used to schedule the timely execution of the message. + */ + public void apply(Supplier contextSupplier) { + contextSupplier.get().queue(() -> LookClient.setLookPitch(pitch)); + } +} diff --git a/template/{{ common_path }}/src/main/java/{{ package_path }}/networking/{{ classname }}Networking.java.jinja b/template/{{ common_path }}/src/main/java/{{ package_path }}/networking/{{ classname }}Networking.java.jinja new file mode 100644 index 00000000..665c1afc --- /dev/null +++ b/template/{{ common_path }}/src/main/java/{{ package_path }}/networking/{{ classname }}Networking.java.jinja @@ -0,0 +1,31 @@ +{% from "macros/mappings.jinja" import mappings as _, import, ns with context -%} + +package {{ package }}.networking; + +import dev.architectury.networking.NetworkChannel; +{{ import( + mojmap="net.minecraft.server.level.ServerPlayer", + yarn="net.minecraft.server.network.ServerPlayerEntity", +) }}{% set ServerPlayer = ns.import %}; + +import static {{ package }}.{{ classname }}.id; + +public class {{ classname }}Networking { + private static final NetworkChannel CHANNEL = NetworkChannel.create(id("networking_channel")); + + public static void init() { + CHANNEL.register(SetLookPitchS2CMsg.class, SetLookPitchS2CMsg::encode, SetLookPitchS2CMsg::new, SetLookPitchS2CMsg::apply); + } + + public static void sendToServer(T message) { + CHANNEL.sendToServer(message); + } + + public static void sendToPlayer({{ ServerPlayer }} player, T message) { + CHANNEL.sendToPlayer(player, message); + } + + public static void sendToPlayers(Iterable<{{ ServerPlayer }}> players, T message) { + CHANNEL.sendToPlayers(players, message); + } +} diff --git a/template/{{ common_path }}/src/main/java/{{ package_path }}/{{ classname }}.java.jinja b/template/{{ common_path }}/src/main/java/{{ package_path }}/{{ classname }}.java.jinja index 13af0d26..f47bd174 100644 --- a/template/{{ common_path }}/src/main/java/{{ package_path }}/{{ classname }}.java.jinja +++ b/template/{{ common_path }}/src/main/java/{{ package_path }}/{{ classname }}.java.jinja @@ -5,6 +5,7 @@ package {{ package }}; import {{ package }}.registry.{{ classname }}IotaTypeRegistry; import {{ package }}.registry.{{ classname }}ItemRegistry; import {{ package }}.registry.{{ classname }}PatternRegistry; +import {{ package }}.networking.{{ classname }}Networking; {{ import( mojmap="net.minecraft.resources.ResourceLocation", yarn="net.minecraft.util.Identifier", @@ -28,6 +29,7 @@ public class {{ classname }} { {{ classname }}ItemRegistry.init(); {{ classname }}IotaTypeRegistry.init(); {{ classname }}PatternRegistry.init(); + {{ classname }}Networking.init(); LOGGER.info({{ classname }}Abstractions.getConfigDirectory().toAbsolutePath().normalize().toString()); } diff --git a/template/{{ common_path }}/src/main/resources/assets/{{ modid }}/lang/en_us.json.jinja b/template/{{ common_path }}/src/main/resources/assets/{{ modid }}/lang/en_us.json.jinja index 4163691b..434c5563 100644 --- a/template/{{ common_path }}/src/main/resources/assets/{{ modid }}/lang/en_us.json.jinja +++ b/template/{{ common_path }}/src/main/resources/assets/{{ modid }}/lang/en_us.json.jinja @@ -6,7 +6,7 @@ "hexcasting.spell.book.{{ modid }}:signum": "Dum. Actn.", "{{ modid }}.entry.dummy_spells": "Dummy Spells", "{{ modid }}.entry.dummy_actions": "Dummy Actions", - "{{ modid }}.page.dummy_spells.congrats": "Accepts a player entity, tells them they are doing a good job.", + "{{ modid }}.page.dummy_spells.congrats": "Accepts a player entity, tells them they are doing a good job and makes them look up.", "{{ modid }}.page.dummy_actions.signum": "Accepts a $(l:casting/101)number/$, returns -1 if it is negative, 0 if 0, 1 if positive.", "text.{{ modid }}.congrats": "Good job, %1$s!", "text.{{ modid }}.congrats.player": "a Player" From b88a5cf467596e012d0b0d7453bf1811f3854135 Mon Sep 17 00:00:00 2001 From: art-nova Date: Thu, 7 Mar 2024 18:40:21 +0100 Subject: [PATCH 2/2] Fix OpCongrats targeting caster instead of target --- .../casting/patterns/spells/OpCongrats.kt.jinja | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja b/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja index 5a2dbcb4..3b81e675 100644 --- a/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja +++ b/template/{{ common_path }}/src/main/java/{{ package_path }}/casting/patterns/spells/OpCongrats.kt.jinja @@ -44,7 +44,7 @@ class OpCongrats : SpellAction { override fun execute(args: List, ctx: CastingContext): Triple> { val target = args.getEntity(0, argc) - // makes sure that the position is inside the range + // makes sure that the target player is inside the range // the caster is allowed to affect. ctx.assertEntityInRange(target) @@ -67,8 +67,8 @@ class OpCongrats : SpellAction { */ private data class Spell(val player: {{ ServerPlayer }}) : RenderedSpell { override fun cast(ctx: CastingContext) { - ctx.caster.{{ _(mojmap="sendSystemMessage", yarn="sendMessage") }}({{ Component }}.translatable("text.{{ modid }}.congrats", player.displayName)); - {{ classname }}Networking.sendToPlayer(ctx.caster, SetLookPitchS2CMsg(-90f)) + player.{{ _(mojmap="sendSystemMessage", yarn="sendMessage") }}({{ Component }}.translatable("text.{{ modid }}.congrats", player.displayName)); + {{ classname }}Networking.sendToPlayer(player, SetLookPitchS2CMsg(-90f)) } } }