From d3cffaf495ec74a79bc5de5fdbc1c15fe417ab22 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Tue, 1 Feb 2022 20:44:53 -0800 Subject: [PATCH] Compress config command code - Remove BooleanConfig, BooleanConfigCommand, and BooleanDirective - Make Flywheel.VERSION private so it cannot be changed - Move createUpdateLimiter from FlwConfig to InstanceManager --- .../java/com/jozufozu/flywheel/Flywheel.java | 25 +-- .../backend/instancing/InstanceManager.java | 15 +- .../instancing/InstancedRenderDispatcher.java | 7 +- .../flywheel/config/BooleanConfig.java | 63 ------- .../flywheel/config/BooleanConfigCommand.java | 37 ----- .../flywheel/config/BooleanDirective.java | 22 --- .../jozufozu/flywheel/config/FlwCommands.java | 154 +++++++++++++++--- .../jozufozu/flywheel/config/FlwConfig.java | 16 +- .../jozufozu/flywheel/config/FlwEngine.java | 64 +------- .../core/compile/ProgramCompiler.java | 1 + 10 files changed, 166 insertions(+), 238 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java delete mode 100644 src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java delete mode 100644 src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index cecf6fae8..653aecf7d 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -36,30 +36,26 @@ public class Flywheel { public static final String ID = "flywheel"; public static final Logger LOGGER = LogManager.getLogger(Flywheel.class); - public static ArtifactVersion VERSION; + private static ArtifactVersion version; public Flywheel() { IModFileInfo modFileById = ModList.get() .getModFileById(ID); - VERSION = modFileById.getMods() + version = modFileById.getMods() .get(0) .getVersion(); FMLJavaModLoadingContext.get() .getModEventBus() - .addListener(this::setup); + .addListener(Flywheel::setup); FlwConfig.init(); DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> Flywheel::clientInit); } - public static ResourceLocation rl(String path) { - return new ResourceLocation(ID, path); - } - - public static void clientInit() { + private static void clientInit() { CrashReportCallables.registerCrashCallable("Flywheel Backend", Backend::getBackendDescriptor); OptifineHandler.init(); @@ -74,7 +70,6 @@ public class Flywheel { modEventBus.addListener(StitchedSprite::onTextureStitchPost); MinecraftForge.EVENT_BUS.addListener(FlwCommands::registerClientCommands); - MinecraftForge.EVENT_BUS.addListener(ProgramCompiler::invalidateAll); VanillaInstances.init(); @@ -84,10 +79,18 @@ public class Flywheel { // Only thing I've seen that's close to a fix is to force the class to load before trying to use it. // From the SpongePowered discord: // https://discord.com/channels/142425412096491520/626802111455297538/675007581168599041 - LOGGER.info("Successfully loaded {}", PausedPartialTickAccessor.class.getName()); + LOGGER.debug("Successfully loaded {}", PausedPartialTickAccessor.class.getName()); } - private void setup(final FMLCommonSetupEvent event) { + private static void setup(final FMLCommonSetupEvent event) { ArgumentTypes.register(rl("engine").toString(), EngineArgument.class, new EmptyArgumentSerializer<>(EngineArgument::getInstance)); } + + public static ArtifactVersion getVersion() { + return version; + } + + public static ResourceLocation rl(String path) { + return new ResourceLocation(ID, path); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java index 871572127..7f741cd8c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java @@ -14,7 +14,9 @@ import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine; +import com.jozufozu.flywheel.backend.instancing.ratelimit.BandedPrimeLimiter; import com.jozufozu.flywheel.backend.instancing.ratelimit.DistanceUpdateLimiter; +import com.jozufozu.flywheel.backend.instancing.ratelimit.NonLimiter; import com.jozufozu.flywheel.config.FlwConfig; import com.jozufozu.flywheel.light.LightUpdater; import com.mojang.math.Vector3f; @@ -46,9 +48,16 @@ public abstract class InstanceManager implements InstancingEngine.OriginShift this.dynamicInstances = new Object2ObjectOpenHashMap<>(); this.tickableInstances = new Object2ObjectOpenHashMap<>(); - FlwConfig config = FlwConfig.get(); - frame = config.createUpdateLimiter(); - tick = config.createUpdateLimiter(); + frame = createUpdateLimiter(); + tick = createUpdateLimiter(); + } + + protected DistanceUpdateLimiter createUpdateLimiter() { + if (FlwConfig.get().limitUpdates()) { + return new BandedPrimeLimiter(); + } else { + return new NonLimiter(); + } } /** diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java index ebf6a8e56..ca423380b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -4,7 +4,7 @@ import java.util.List; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.config.BooleanConfig; +import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwConfig; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; @@ -73,7 +73,6 @@ public class InstancedRenderDispatcher { @SubscribeEvent public static void tick(TickEvent.ClientTickEvent event) { - if (!Backend.isGameActive() || event.phase == TickEvent.Phase.START) { return; } @@ -120,12 +119,12 @@ public class InstancedRenderDispatcher { public static void getDebugString(List debug) { debug.add(""); - debug.add("Flywheel: " + Flywheel.VERSION); + debug.add("Flywheel: " + Flywheel.getVersion()); if (Backend.isOn()) { InstanceWorld instanceWorld = instanceWorlds.get(Minecraft.getInstance().level); - debug.add("Update limiting: " + BooleanConfig.boolToText(FlwConfig.get().limitUpdates()).getString()); + debug.add("Update limiting: " + FlwCommands.boolToText(FlwConfig.get().limitUpdates()).getString()); debug.add("B: " + instanceWorld.blockEntityInstanceManager.getObjectCount() + ", E: " + instanceWorld.entityInstanceManager.getObjectCount()); instanceWorld.engine.addDebugInfo(debug); } else { diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java deleted file mode 100644 index 2cdf578b6..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.jozufozu.flywheel.config; - -import java.util.function.Consumer; - -import com.jozufozu.flywheel.backend.Backend; - -import net.minecraft.ChatFormatting; -import net.minecraft.client.Minecraft; -import net.minecraft.client.player.LocalPlayer; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; -import net.minecraft.network.chat.TextComponent; - -public enum BooleanConfig { - NORMAL_OVERLAY(BooleanConfig::normalOverlay), - LIMIT_UPDATES(BooleanConfig::limitUpdates); - - final Consumer receiver; - - BooleanConfig(Consumer receiver) { - this.receiver = receiver; - } - - private static void limitUpdates(BooleanDirective booleanDirective) { - LocalPlayer player = Minecraft.getInstance().player; - if (player == null || booleanDirective == null) return; - - if (booleanDirective == BooleanDirective.DISPLAY) { - Component text = new TextComponent("Update limiting is currently: ").append(boolToText(FlwConfig.get().limitUpdates())); - player.displayClientMessage(text, false); - return; - } - - FlwConfig.get().client.limitUpdates.set(booleanDirective.get()); - - Component text = boolToText(FlwConfig.get().limitUpdates()).append(new TextComponent(" update limiting.").withStyle(ChatFormatting.WHITE)); - - player.displayClientMessage(text, false); - - Backend.reloadWorldRenderers(); - } - - private static void normalOverlay(BooleanDirective state) { - LocalPlayer player = Minecraft.getInstance().player; - if (player == null || state == null) return; - - if (state == BooleanDirective.DISPLAY) { - Component text = new TextComponent("Normal debug mode is currently: ").append(boolToText(FlwConfig.get().debugNormals())); - player.displayClientMessage(text, false); - return; - } - - FlwConfig.get().client.debugNormals.set(state.get()); - - Component text = boolToText(FlwConfig.get().debugNormals()).append(new TextComponent(" normal debug mode").withStyle(ChatFormatting.WHITE)); - - player.displayClientMessage(text, false); - } - - public static MutableComponent boolToText(boolean b) { - return b ? new TextComponent("enabled").withStyle(ChatFormatting.DARK_GREEN) : new TextComponent("disabled").withStyle(ChatFormatting.RED); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java deleted file mode 100644 index d3be59e10..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.jozufozu.flywheel.config; - -import com.mojang.brigadier.Command; -import com.mojang.brigadier.builder.ArgumentBuilder; - -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; - -public class BooleanConfigCommand { - - private final String name; - - private final BooleanConfig value; - - public BooleanConfigCommand(String name, BooleanConfig value) { - this.name = name; - this.value = value; - } - - public ArgumentBuilder register() { - return Commands.literal(name) - .executes(context -> { - value.receiver.accept(BooleanDirective.DISPLAY); - return Command.SINGLE_SUCCESS; - }) - .then(Commands.literal("on") - .executes(context -> { - value.receiver.accept(BooleanDirective.TRUE); - return Command.SINGLE_SUCCESS; - })) - .then(Commands.literal("off") - .executes(context -> { - value.receiver.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 deleted file mode 100644 index 1d6892478..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.jozufozu.flywheel.config; - -public enum BooleanDirective { - TRUE(true), - FALSE(false), - /** - * Don't change anything, just display what the value currently is. - */ - DISPLAY(true), - ; - - private final boolean b; - - BooleanDirective(boolean b) { - this.b = b; - } - - public boolean get() { - if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value"); - return b; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java index 018e8efce..ccd9e262d 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java @@ -1,46 +1,152 @@ package com.jozufozu.flywheel.config; +import java.util.function.BiConsumer; + +import org.jetbrains.annotations.NotNull; + +import com.jozufozu.flywheel.backend.Backend; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import net.minecraft.ChatFormatting; +import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.TextComponent; import net.minecraftforge.client.event.RegisterClientCommandsEvent; +import net.minecraftforge.common.ForgeConfigSpec.ConfigValue; +import net.minecraftforge.fml.ModList; public class FlwCommands { public static void registerClientCommands(RegisterClientCommandsEvent event) { - CommandDispatcher dispatcher = event.getDispatcher(); + FlwConfig config = FlwConfig.get(); - dispatcher.register(Commands.literal("flywheel") - .then(debugNormalsCommand()) - .then(backendCommand()) - .then(limitUpdatesCommand()) - ); - } + ConfigCommandBuilder commandBuilder = new ConfigCommandBuilder("flywheel"); - private static ArgumentBuilder debugNormalsCommand() { - return new BooleanConfigCommand("debugNormals", BooleanConfig.NORMAL_OVERLAY).register(); - } - - private static ArgumentBuilder limitUpdatesCommand() { - return new BooleanConfigCommand("limitUpdates", BooleanConfig.LIMIT_UPDATES).register(); - } - - private static ArgumentBuilder backendCommand() { - return Commands.literal("backend") + commandBuilder.addValue(config.client.engine, "backend", (builder, value) -> + builder .executes(context -> { - FlwEngine.handle(null); - + LocalPlayer player = Minecraft.getInstance().player; + if (player != null) { + player.displayClientMessage(getEngineMessage(value.get()), false); + } return Command.SINGLE_SUCCESS; }) .then(Commands.argument("type", EngineArgument.INSTANCE) - .executes(context -> { + .executes(context -> { + LocalPlayer player = Minecraft.getInstance().player; + if (player != null) { FlwEngine type = context.getArgument("type", FlwEngine.class); + value.set(type); - FlwEngine.handle(type); + Component message = getEngineMessage(type); + player.displayClientMessage(message, false); - return Command.SINGLE_SUCCESS; - })); + Backend.reloadWorldRenderers(); + } + return Command.SINGLE_SUCCESS; + }))); + + commandBuilder.addValue(config.client.debugNormals, "debugNormals", (builder, value) -> booleanValueCommand(builder, config, value, + (source, bool) -> { + LocalPlayer player = Minecraft.getInstance().player; + if (player == null) return; + + Component text = new TextComponent("Normal debug mode is currently: ").append(boolToText(bool)); + player.displayClientMessage(text, false); + }, + (source, bool) -> { + LocalPlayer player = Minecraft.getInstance().player; + if (player == null) return; + + Component text = boolToText(bool).append(new TextComponent(" normal debug mode").withStyle(ChatFormatting.WHITE)); + player.displayClientMessage(text, false); + } + )); + + commandBuilder.addValue(config.client.limitUpdates, "limitUpdates", (builder, value) -> booleanValueCommand(builder, config, value, + (source, bool) -> { + LocalPlayer player = Minecraft.getInstance().player; + if (player == null) return; + + Component text = new TextComponent("Update limiting is currently: ").append(boolToText(bool)); + player.displayClientMessage(text, false); + }, + (source, bool) -> { + LocalPlayer player = Minecraft.getInstance().player; + if (player == null) return; + + Component text = boolToText(bool).append(new TextComponent(" update limiting.").withStyle(ChatFormatting.WHITE)); + player.displayClientMessage(text, false); + + Backend.reloadWorldRenderers(); + } + )); + + commandBuilder.build(event.getDispatcher()); + } + + public static void booleanValueCommand(LiteralArgumentBuilder builder, FlwConfig config, ConfigValue value, BiConsumer displayAction, BiConsumer setAction) { + builder + .executes(context -> { + displayAction.accept(context.getSource(), value.get()); + return Command.SINGLE_SUCCESS; + }) + .then(Commands.literal("on") + .executes(context -> { + value.set(true); + setAction.accept(context.getSource(), value.get()); + return Command.SINGLE_SUCCESS; + })) + .then(Commands.literal("off") + .executes(context -> { + value.set(false); + setAction.accept(context.getSource(), value.get()); + return Command.SINGLE_SUCCESS; + })); + } + + public static MutableComponent boolToText(boolean b) { + return b ? new TextComponent("enabled").withStyle(ChatFormatting.DARK_GREEN) : new TextComponent("disabled").withStyle(ChatFormatting.RED); + } + + public static Component getEngineMessage(@NotNull FlwEngine type) { + return switch (type) { + case OFF -> new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED); + case INSTANCING -> new TextComponent("Using Instancing Engine").withStyle(ChatFormatting.GREEN); + case BATCHING -> { + MutableComponent msg = new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN); + + if (ModList.get() + .isLoaded("create")) { + // FIXME: batching engine contraption lighting issues + msg.append(new TextComponent("\nWARNING: May cause issues with Create Contraptions").withStyle(ChatFormatting.RED)); + } + + yield msg; + } + }; + } + + public static class ConfigCommandBuilder { + protected LiteralArgumentBuilder command; + + public ConfigCommandBuilder(String baseLiteral) { + command = Commands.literal(baseLiteral); + } + + public > void addValue(T value, String subcommand, BiConsumer, T> consumer) { + LiteralArgumentBuilder builder = Commands.literal(subcommand); + consumer.accept(builder, value); + command.then(builder); + } + + public void build(CommandDispatcher dispatcher) { + dispatcher.register(command); + } } } diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java index 468252db0..95be3cc46 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java @@ -2,12 +2,9 @@ package com.jozufozu.flywheel.config; import org.apache.commons.lang3.tuple.Pair; -import com.jozufozu.flywheel.backend.instancing.ratelimit.BandedPrimeLimiter; -import com.jozufozu.flywheel.backend.instancing.ratelimit.DistanceUpdateLimiter; -import com.jozufozu.flywheel.backend.instancing.ratelimit.NonLimiter; - import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.common.ForgeConfigSpec.BooleanValue; +import net.minecraftforge.common.ForgeConfigSpec.EnumValue; import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.config.ModConfig; @@ -42,24 +39,15 @@ public class FlwConfig { return client.limitUpdates.get(); } - public DistanceUpdateLimiter createUpdateLimiter() { - if (limitUpdates()) { - return new BandedPrimeLimiter(); - } else { - return new NonLimiter(); - } - } - public static void init() { } public static class ClientConfig { - public final ForgeConfigSpec.EnumValue engine; + public final EnumValue engine; public final BooleanValue debugNormals; public final BooleanValue limitUpdates; public ClientConfig(ForgeConfigSpec.Builder builder) { - engine = builder.comment("Enable or disable the entire engine") .defineEnum("backend", FlwEngine.INSTANCING); diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java b/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java index f0a977224..f5665e1f4 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java @@ -6,19 +6,6 @@ import java.util.Map; import javax.annotation.Nullable; -import org.jetbrains.annotations.NotNull; - -import com.jozufozu.flywheel.backend.Backend; - -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; -import net.minecraftforge.fml.ModList; - public enum FlwEngine { OFF("off", "Off"), BATCHING("batching", "Parallel Batching"), @@ -42,57 +29,14 @@ public enum FlwEngine { this.properName = properName; } + public String getShortName() { + return shortName; + } + public String getProperName() { return properName; } - public void encode(FriendlyByteBuf buffer) { - buffer.writeByte(this.ordinal()); - } - - public static void handle(@Nullable FlwEngine type) { - LocalPlayer player = Minecraft.getInstance().player; - if (player == null) return; - - if (type != null) { - FlwConfig.get().client.engine.set(type); - - Component message = getMessage(type); - - player.displayClientMessage(message, false); - Backend.reloadWorldRenderers(); - } else { - player.displayClientMessage(getMessage(FlwConfig.get().getEngine()), false); - } - } - - private static Component getMessage(@NotNull FlwEngine type) { - return switch (type) { - case OFF -> new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED); - case INSTANCING -> new TextComponent("Using Instancing Engine").withStyle(ChatFormatting.GREEN); - case BATCHING -> { - MutableComponent msg = new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN); - - if (ModList.get() - .isLoaded("create")) { - // FIXME: batching engine contraption lighting issues - msg.append(new TextComponent("\nWARNING: May cause issues with Create Contraptions").withStyle(ChatFormatting.RED)); - } - - yield msg; - } - }; - } - - @Nullable - public static FlwEngine decode(FriendlyByteBuf buffer) { - byte b = buffer.readByte(); - - if (b == -1) return null; - - return values()[b]; - } - @Nullable public static FlwEngine byName(String name) { return lookup.get(name); diff --git a/src/main/java/com/jozufozu/flywheel/core/compile/ProgramCompiler.java b/src/main/java/com/jozufozu/flywheel/core/compile/ProgramCompiler.java index 4931c63fd..43d0d7193 100644 --- a/src/main/java/com/jozufozu/flywheel/core/compile/ProgramCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/core/compile/ProgramCompiler.java @@ -54,6 +54,7 @@ public class ProgramCompiler

extends Memoizer