Initial move to client commands.

- Nothing fancy, just inlining packets.
This commit is contained in:
Jozufozu 2022-01-24 10:46:17 -08:00
parent 231e79984a
commit e6368b291f
11 changed files with 18 additions and 196 deletions

View file

@ -5,7 +5,7 @@ org.gradle.daemon = false
mod_version = 0.6.0 mod_version = 0.6.0
mc_update_version = 1.18 mc_update_version = 1.18
minecraft_version = 1.18.1 minecraft_version = 1.18.1
forge_version = 39.0.8 forge_version = 39.0.59
# build dependency versions # build dependency versions
forgegradle_version = 5.1.+ forgegradle_version = 5.1.+
@ -13,7 +13,7 @@ mixingradle_version = 0.7-SNAPSHOT
mixin_version = 0.8.5 mixin_version = 0.8.5
librarian_version = 1.+ librarian_version = 1.+
cursegradle_version = 1.4.0 cursegradle_version = 1.4.0
parchment_version = 2021.12.19 parchment_version = 2022.01.23
# curseforge info # curseforge info
projectId = 486392 projectId = 486392

View file

@ -6,7 +6,6 @@ import org.apache.logging.log4j.Logger;
import com.jozufozu.flywheel.config.EngineArgument; import com.jozufozu.flywheel.config.EngineArgument;
import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwCommands;
import com.jozufozu.flywheel.config.FlwConfig; import com.jozufozu.flywheel.config.FlwConfig;
import com.jozufozu.flywheel.config.FlwPackets;
import net.minecraft.commands.synchronization.ArgumentTypes; import net.minecraft.commands.synchronization.ArgumentTypes;
import net.minecraft.commands.synchronization.EmptyArgumentSerializer; import net.minecraft.commands.synchronization.EmptyArgumentSerializer;
@ -41,7 +40,6 @@ public class Flywheel {
} }
private void setup(final FMLCommonSetupEvent event) { private void setup(final FMLCommonSetupEvent event) {
FlwPackets.registerPackets();
ArgumentTypes.register(rl("engine").toString(), EngineArgument.class, new EmptyArgumentSerializer<>(EngineArgument::getInstance)); ArgumentTypes.register(rl("engine").toString(), EngineArgument.class, new EmptyArgumentSerializer<>(EngineArgument::getInstance));
} }
} }

View file

@ -6,7 +6,6 @@ import java.util.function.Supplier;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer; import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextComponent; import net.minecraft.network.chat.TextComponent;
@ -23,31 +22,6 @@ public enum BooleanConfig {
this.receiver = receiver; 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) @OnlyIn(Dist.CLIENT)
private static void normalOverlay(BooleanDirective state) { private static void normalOverlay(BooleanDirective state) {
LocalPlayer player = Minecraft.getInstance().player; LocalPlayer player = Minecraft.getInstance().player;

View file

@ -5,8 +5,6 @@ import com.mojang.brigadier.builder.ArgumentBuilder;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands; import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.PacketDistributor;
public class BooleanConfigCommand { public class BooleanConfigCommand {
@ -22,23 +20,17 @@ public class BooleanConfigCommand {
public ArgumentBuilder<CommandSourceStack, ?> register() { public ArgumentBuilder<CommandSourceStack, ?> register() {
return Commands.literal(name) return Commands.literal(name)
.executes(context -> { .executes(context -> {
ServerPlayer player = context.getSource() value.receiver.get().accept(BooleanDirective.DISPLAY);
.getPlayerOrException();
FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.DISPLAY));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
}) })
.then(Commands.literal("on") .then(Commands.literal("on")
.executes(context -> { .executes(context -> {
ServerPlayer player = context.getSource() value.receiver.get().accept(BooleanDirective.TRUE);
.getPlayerOrException();
FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.TRUE));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
})) }))
.then(Commands.literal("off") .then(Commands.literal("off")
.executes(context -> { .executes(context -> {
ServerPlayer player = context.getSource() value.receiver.get().accept(BooleanDirective.FALSE);
.getPlayerOrException();
FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.FALSE));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
})); }));
} }

View file

@ -1,7 +1,5 @@
package com.jozufozu.flywheel.config; package com.jozufozu.flywheel.config;
import net.minecraft.network.FriendlyByteBuf;
public enum BooleanDirective { public enum BooleanDirective {
TRUE(true), TRUE(true),
FALSE(false), FALSE(false),
@ -21,18 +19,4 @@ public enum BooleanDirective {
if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value"); if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value");
return b; 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()];
}
} }

View file

@ -14,18 +14,14 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.network.chat.TranslatableComponent;
public class EngineArgument implements ArgumentType<FlwEngine> { public enum EngineArgument implements ArgumentType<FlwEngine> {
INSTANCE;
private static final EngineArgument INSTANCE = new EngineArgument();
private static final Dynamic2CommandExceptionType INVALID = new Dynamic2CommandExceptionType((found, constants) -> { private static final Dynamic2CommandExceptionType INVALID = new Dynamic2CommandExceptionType((found, constants) -> {
// TODO: don't steal lang // TODO: don't steal lang
return new TranslatableComponent("commands.forge.arguments.enum.invalid", constants, found); return new TranslatableComponent("commands.forge.arguments.enum.invalid", constants, found);
}); });
private EngineArgument() {
}
@Override @Override
public FlwEngine parse(StringReader reader) throws CommandSyntaxException { public FlwEngine parse(StringReader reader) throws CommandSyntaxException {
String string = reader.readUnquotedString(); String string = reader.readUnquotedString();

View file

@ -6,17 +6,11 @@ import com.mojang.brigadier.builder.ArgumentBuilder;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands; import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.network.PacketDistributor;
public class FlwCommands { public class FlwCommands {
@SubscribeEvent public static void onServerStarting(RegisterClientCommandsEvent event) {
public static void onServerStarting(ServerStartingEvent event) { CommandDispatcher<CommandSourceStack> dispatcher = event.getDispatcher();
CommandDispatcher<CommandSourceStack> dispatcher = event.getServer()
.getCommands()
.getDispatcher();
dispatcher.register(Commands.literal("flywheel") dispatcher.register(Commands.literal("flywheel")
.then(debugCommand()) .then(debugCommand())
@ -31,18 +25,15 @@ public class FlwCommands {
private static ArgumentBuilder<CommandSourceStack, ?> backendCommand() { private static ArgumentBuilder<CommandSourceStack, ?> backendCommand() {
return Commands.literal("backend") return Commands.literal("backend")
.executes(context -> { .executes(context -> {
ServerPlayer player = context.getSource() FlwEngine.handle(null);
.getPlayerOrException();
FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureEnginePacket());
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
}) })
.then(Commands.argument("type", EngineArgument.getInstance()) .then(Commands.argument("type", EngineArgument.INSTANCE)
.executes(context -> { .executes(context -> {
FlwEngine type = context.getArgument("type", FlwEngine.class); FlwEngine type = context.getArgument("type", FlwEngine.class);
ServerPlayer player = context.getSource() FlwEngine.handle(type);
.getPlayerOrException();
FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureEnginePacket(type));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
})); }));

View file

@ -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();
}
}

View file

@ -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<NetworkEvent.Context> ctx) {
if (directive != null) {
target.receiver.get()
.accept(directive);
}
ctx.get()
.setPacketHandled(true);
}
}

View file

@ -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<NetworkEvent.Context> ctx) {
FlwEngine.handle(type);
ctx.get()
.setPacketHandled(true);
}
}

View file

@ -17,13 +17,13 @@ A modern engine for modded minecraft.
[[dependencies.flywheel]] [[dependencies.flywheel]]
modId = "forge" modId = "forge"
mandatory = true mandatory = true
versionRange = "[38,)" versionRange = "[39.0.46,)"
ordering = "NONE" ordering = "NONE"
side = "BOTH" side = "CLIENT"
[[dependencies.flywheel]] [[dependencies.flywheel]]
modId = "minecraft" modId = "minecraft"
mandatory = true mandatory = true
versionRange = "[1.18,1.19)" versionRange = "[1.18.1,1.19)"
ordering = "NONE" ordering = "NONE"
side = "BOTH" side = "CLIENT"