From e6368b291f53a1112c6133498e0bfb0a71e33c47 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 24 Jan 2022 10:46:17 -0800 Subject: [PATCH] Initial move to client commands. - Nothing fancy, just inlining packets. --- gradle.properties | 4 +- .../java/com/jozufozu/flywheel/Flywheel.java | 2 - .../flywheel/config/BooleanConfig.java | 26 ------------ .../flywheel/config/BooleanConfigCommand.java | 14 ++----- .../flywheel/config/BooleanDirective.java | 16 -------- .../flywheel/config/EngineArgument.java | 8 +--- .../jozufozu/flywheel/config/FlwCommands.java | 23 ++++------- .../jozufozu/flywheel/config/FlwPackets.java | 36 ---------------- .../config/SConfigureBooleanPacket.java | 41 ------------------- .../config/SConfigureEnginePacket.java | 36 ---------------- src/main/resources/META-INF/mods.toml | 8 ++-- 11 files changed, 18 insertions(+), 196 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/config/FlwPackets.java delete mode 100644 src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java delete mode 100644 src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java diff --git a/gradle.properties b/gradle.properties index 4082064d5..0476c1c99 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.daemon = false mod_version = 0.6.0 mc_update_version = 1.18 minecraft_version = 1.18.1 -forge_version = 39.0.8 +forge_version = 39.0.59 # build dependency versions forgegradle_version = 5.1.+ @@ -13,7 +13,7 @@ mixingradle_version = 0.7-SNAPSHOT mixin_version = 0.8.5 librarian_version = 1.+ cursegradle_version = 1.4.0 -parchment_version = 2021.12.19 +parchment_version = 2022.01.23 # curseforge info projectId = 486392 diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 6214f67f5..71c9394c6 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -6,7 +6,6 @@ import org.apache.logging.log4j.Logger; import com.jozufozu.flywheel.config.EngineArgument; import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwConfig; -import com.jozufozu.flywheel.config.FlwPackets; import net.minecraft.commands.synchronization.ArgumentTypes; import net.minecraft.commands.synchronization.EmptyArgumentSerializer; @@ -41,7 +40,6 @@ public class Flywheel { } private void setup(final FMLCommonSetupEvent event) { - FlwPackets.registerPackets(); ArgumentTypes.register(rl("engine").toString(), EngineArgument.class, new EmptyArgumentSerializer<>(EngineArgument::getInstance)); } } diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java index ae33c75f3..4014fe5bd 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java @@ -6,7 +6,6 @@ import java.util.function.Supplier; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; -import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.TextComponent; @@ -23,31 +22,6 @@ public enum BooleanConfig { this.receiver = receiver; } - public SConfigureBooleanPacket packet(BooleanDirective directive) { - return new SConfigureBooleanPacket(this, directive); - } - - /** - * Encode a variant of BooleanConfig. Symmetrical function to {@link #decode} - */ - public void encode(FriendlyByteBuf buffer) { - buffer.writeByte(this.ordinal()); - } - - /** - * Safely decode a variant of BooleanConfig. Symmetrical function to {@link #encode} - */ - public static BooleanConfig decode(FriendlyByteBuf buffer) { - byte t = buffer.readByte(); - BooleanConfig[] values = values(); - // Protects against version differences. - // Shouldn't ever happen but do a sanity check for safety. - if (t >= 0 && t < values.length) - return values[t]; - else - return null; - } - @OnlyIn(Dist.CLIENT) private static void normalOverlay(BooleanDirective state) { LocalPlayer player = Minecraft.getInstance().player; diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java index b6c0d53ad..9c690af30 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java @@ -5,8 +5,6 @@ import com.mojang.brigadier.builder.ArgumentBuilder; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; -import net.minecraft.server.level.ServerPlayer; -import net.minecraftforge.network.PacketDistributor; public class BooleanConfigCommand { @@ -22,23 +20,17 @@ public class BooleanConfigCommand { public ArgumentBuilder register() { return Commands.literal(name) .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.DISPLAY)); + value.receiver.get().accept(BooleanDirective.DISPLAY); return Command.SINGLE_SUCCESS; }) .then(Commands.literal("on") .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.TRUE)); + value.receiver.get().accept(BooleanDirective.TRUE); return Command.SINGLE_SUCCESS; })) .then(Commands.literal("off") .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.FALSE)); + value.receiver.get().accept(BooleanDirective.FALSE); return Command.SINGLE_SUCCESS; })); } diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java b/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java index 86dabad8f..1d6892478 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java @@ -1,7 +1,5 @@ package com.jozufozu.flywheel.config; -import net.minecraft.network.FriendlyByteBuf; - public enum BooleanDirective { TRUE(true), FALSE(false), @@ -21,18 +19,4 @@ public enum BooleanDirective { if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value"); return b; } - - /** - * Encode a variant of BooleanDirective. Symmetrical function to {@link #decode} - */ - public void encode(FriendlyByteBuf buffer) { - buffer.writeByte(this.ordinal()); - } - - /** - * Safely decode a variant of BooleanDirective. Symmetrical function to {@link #encode} - */ - public static BooleanDirective decode(FriendlyByteBuf buffer) { - return values()[buffer.readByte()]; - } } diff --git a/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java b/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java index 90588554f..497a60870 100644 --- a/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java +++ b/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java @@ -14,18 +14,14 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.network.chat.TranslatableComponent; -public class EngineArgument implements ArgumentType { - - private static final EngineArgument INSTANCE = new EngineArgument(); +public enum EngineArgument implements ArgumentType { + INSTANCE; private static final Dynamic2CommandExceptionType INVALID = new Dynamic2CommandExceptionType((found, constants) -> { // TODO: don't steal lang return new TranslatableComponent("commands.forge.arguments.enum.invalid", constants, found); }); - private EngineArgument() { - } - @Override public FlwEngine parse(StringReader reader) throws CommandSyntaxException { String string = reader.readUnquotedString(); diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java index 6778d9996..5562d535a 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java @@ -6,17 +6,11 @@ import com.mojang.brigadier.builder.ArgumentBuilder; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; -import net.minecraft.server.level.ServerPlayer; -import net.minecraftforge.event.server.ServerStartingEvent; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.network.PacketDistributor; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; public class FlwCommands { - @SubscribeEvent - public static void onServerStarting(ServerStartingEvent event) { - CommandDispatcher dispatcher = event.getServer() - .getCommands() - .getDispatcher(); + public static void onServerStarting(RegisterClientCommandsEvent event) { + CommandDispatcher dispatcher = event.getDispatcher(); dispatcher.register(Commands.literal("flywheel") .then(debugCommand()) @@ -31,18 +25,15 @@ public class FlwCommands { private static ArgumentBuilder backendCommand() { return Commands.literal("backend") .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureEnginePacket()); + FlwEngine.handle(null); + return Command.SINGLE_SUCCESS; }) - .then(Commands.argument("type", EngineArgument.getInstance()) + .then(Commands.argument("type", EngineArgument.INSTANCE) .executes(context -> { FlwEngine type = context.getArgument("type", FlwEngine.class); - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureEnginePacket(type)); + FlwEngine.handle(type); return Command.SINGLE_SUCCESS; })); diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwPackets.java b/src/main/java/com/jozufozu/flywheel/config/FlwPackets.java deleted file mode 100644 index c3fa59234..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/FlwPackets.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.jozufozu.flywheel.config; - -import com.jozufozu.flywheel.Flywheel; - -import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.network.NetworkDirection; -import net.minecraftforge.network.NetworkRegistry; -import net.minecraftforge.network.simple.SimpleChannel; - -public class FlwPackets { - public static final ResourceLocation CHANNEL_NAME = Flywheel.rl("main"); - public static final String NETWORK_VERSION = String.valueOf(1); - public static SimpleChannel channel; - - public static void registerPackets() { - channel = NetworkRegistry.ChannelBuilder.named(CHANNEL_NAME) - .serverAcceptedVersions(NETWORK_VERSION::equals) - .clientAcceptedVersions(NETWORK_VERSION::equals) - .networkProtocolVersion(() -> NETWORK_VERSION) - .simpleChannel(); - - int id = 0; - - channel.messageBuilder(SConfigureBooleanPacket.class, id++, NetworkDirection.PLAY_TO_CLIENT) - .decoder(SConfigureBooleanPacket::new) - .encoder(SConfigureBooleanPacket::encode) - .consumer(SConfigureBooleanPacket::execute) - .add(); - - channel.messageBuilder(SConfigureEnginePacket.class, id++, NetworkDirection.PLAY_TO_CLIENT) - .decoder(SConfigureEnginePacket::new) - .encoder(SConfigureEnginePacket::encode) - .consumer(SConfigureEnginePacket::execute) - .add(); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java b/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java deleted file mode 100644 index 19853d9db..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.jozufozu.flywheel.config; - - -import java.util.function.Supplier; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.network.NetworkEvent; - -/** - * Thanks, @zelophed - */ -public class SConfigureBooleanPacket { - - private final BooleanConfig target; - private final BooleanDirective directive; - - public SConfigureBooleanPacket(BooleanConfig target, BooleanDirective directive) { - this.target = target; - this.directive = directive; - } - - public SConfigureBooleanPacket(FriendlyByteBuf buffer) { - target = BooleanConfig.decode(buffer); - directive = BooleanDirective.decode(buffer); - } - - public void encode(FriendlyByteBuf buffer) { - target.encode(buffer); - directive.encode(buffer); - } - - public void execute(Supplier ctx) { - if (directive != null) { - target.receiver.get() - .accept(directive); - } - ctx.get() - .setPacketHandled(true); - } - -} diff --git a/src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java b/src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java deleted file mode 100644 index d287d7aca..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.jozufozu.flywheel.config; - -import java.util.function.Supplier; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.network.NetworkEvent; - -public class SConfigureEnginePacket { - - private final FlwEngine type; - - public SConfigureEnginePacket() { - type = null; - } - - public SConfigureEnginePacket(FlwEngine type) { - this.type = type; - } - - public SConfigureEnginePacket(FriendlyByteBuf buffer) { - type = FlwEngine.decode(buffer); - } - - public void encode(FriendlyByteBuf buffer) { - if (type != null) - type.encode(buffer); - else - buffer.writeByte(-1); - } - - public void execute(Supplier ctx) { - FlwEngine.handle(type); - ctx.get() - .setPacketHandled(true); - } -} diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 5e2744c9f..3ac2d980c 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -17,13 +17,13 @@ A modern engine for modded minecraft. [[dependencies.flywheel]] modId = "forge" mandatory = true -versionRange = "[38,)" +versionRange = "[39.0.46,)" ordering = "NONE" -side = "BOTH" +side = "CLIENT" [[dependencies.flywheel]] modId = "minecraft" mandatory = true -versionRange = "[1.18,1.19)" +versionRange = "[1.18.1,1.19)" ordering = "NONE" -side = "BOTH" +side = "CLIENT"