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
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

View file

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

View file

@ -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;

View file

@ -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<CommandSourceStack, ?> 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;
}));
}

View file

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

View file

@ -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<FlwEngine> {
private static final EngineArgument INSTANCE = new EngineArgument();
public enum EngineArgument implements ArgumentType<FlwEngine> {
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();

View file

@ -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<CommandSourceStack> dispatcher = event.getServer()
.getCommands()
.getDispatcher();
public static void onServerStarting(RegisterClientCommandsEvent event) {
CommandDispatcher<CommandSourceStack> dispatcher = event.getDispatcher();
dispatcher.register(Commands.literal("flywheel")
.then(debugCommand())
@ -31,18 +25,15 @@ public class FlwCommands {
private static ArgumentBuilder<CommandSourceStack, ?> 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;
}));

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]]
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"