Skip to content

Commit

Permalink
Move config command to common
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Sep 21, 2024
1 parent c0fd525 commit f014a39
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
*/
public class GeneralConfig extends DummyConfigCommon<ModBaseCommon<?>> {

@ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if a button should be added to the main menu to open a dev world (shift-click creates a new world).", configLocation = ModConfigLocation.CLIENT)
@ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if a button should be added to the main menu to open a dev world (shift-click creates a new world).", configLocation = ModConfigLocation.CLIENT, isCommandable = true)
public static boolean devWorldButton = true;

@ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if music should be disabled at startup.", configLocation = ModConfigLocation.CLIENT)
@ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if music should be disabled at startup.", configLocation = ModConfigLocation.CLIENT, isCommandable = true)
public static boolean devDisableMusic = true;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.neoforged.neoforge.common.ModConfigSpec;
import org.cyclops.cyclopscore.command.argument.ArgumentTypeConfigProperty;
import org.cyclops.cyclopscore.config.ConfigurablePropertyData;
import org.cyclops.cyclopscore.helper.Helpers;
import org.cyclops.cyclopscore.init.ModBase;
import org.cyclops.cyclopscore.init.IModBase;

/**
* Command for selecting and defining a {@link ConfigurablePropertyData}.
Expand All @@ -21,10 +19,10 @@
*/
public class CommandConfig implements Command<CommandSourceStack> {

private final ModBase mod;
private final IModBase mod;
private final boolean valueSet;

public CommandConfig(ModBase mod, boolean valueSet) {
public CommandConfig(IModBase mod, boolean valueSet) {
this.mod = mod;
this.valueSet = valueSet;
}
Expand All @@ -36,10 +34,9 @@ public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxE
context.getSource().getPlayerOrException().sendSystemMessage(Component.literal(property.getConfigProperty().get().toString()));
} else {
String value = context.getArgument("value", String.class);
Object newValue = Helpers.tryParse(value, property.getConfigProperty().get());
Object newValue = mod.getModHelpers().getBaseHelpers().tryParse(value, property.getConfigProperty().get());
if(newValue != null) {
((ModConfigSpec.ConfigValue) property.getConfigProperty()).set(newValue);
((ModConfigSpec.ConfigValue) property.getConfigProperty()).save();
property.getConfigPropertyUpdater().accept(newValue);
context.getSource().getPlayerOrException().sendSystemMessage(Component.translatable("chat.cyclopscore.command.updatedValue",
property.getName(), newValue.toString()));
} else {
Expand All @@ -50,7 +47,7 @@ public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxE
return 0;
}

public static LiteralArgumentBuilder<CommandSourceStack> make(ModBase mod) {
public static LiteralArgumentBuilder<CommandSourceStack> make(IModBase mod) {
return Commands.literal("config")
.requires((commandSource) -> commandSource.hasPermission(2))
.then(Commands.argument("property", new ArgumentTypeConfigProperty(mod))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.network.FriendlyByteBuf;
import org.cyclops.cyclopscore.init.ModBase;
import org.cyclops.cyclopscore.helper.ModBaseCommon;
import org.cyclops.cyclopscore.init.IModBase;
import org.cyclops.cyclopscore.network.PacketCodec;

/**
Expand All @@ -21,7 +22,7 @@ public void serializeToNetwork(ArgumentInfoMod.Template t, FriendlyByteBuf packe

@Override
public ArgumentInfoMod.Template deserializeFromNetwork(FriendlyByteBuf packetBuffer) {
return new Template(ModBase.get(packetBuffer.readUtf(PacketCodec.READ_STRING_MAX_LENGTH)));
return new Template(ModBaseCommon.getCommon(packetBuffer.readUtf(PacketCodec.READ_STRING_MAX_LENGTH)));
}

@Override
Expand All @@ -35,9 +36,9 @@ public Template unpack(ArgumentTypeConfigProperty p_235372_) {
}

public final class Template implements ArgumentTypeInfo.Template<ArgumentTypeConfigProperty> {
private final ModBase mod;
private final IModBase mod;

Template(ModBase mod) {
Template(IModBase mod) {
this.mod = mod;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Component;
import org.cyclops.cyclopscore.config.ConfigurablePropertyData;
import org.cyclops.cyclopscore.init.ModBase;
import org.cyclops.cyclopscore.init.IModBase;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -23,13 +23,13 @@
*/
public class ArgumentTypeConfigProperty implements ArgumentType<ConfigurablePropertyData> {

private final ModBase mod;
private final IModBase mod;

public ArgumentTypeConfigProperty(ModBase mod) {
public ArgumentTypeConfigProperty(IModBase mod) {
this.mod = mod;
}

public ModBase getMod() {
public IModBase getMod() {
return mod;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.cyclops.cyclopscore.command.argument;

import org.cyclops.cyclopscore.config.extendedconfig.ArgumentTypeConfigCommon;
import org.cyclops.cyclopscore.init.IModBase;

public class ArgumentTypeConfigPropertyConfig<M extends IModBase> extends ArgumentTypeConfigCommon<ArgumentTypeConfigProperty, ArgumentInfoMod<ArgumentTypeConfigProperty>.Template, M> {
public ArgumentTypeConfigPropertyConfig(M mod) {
super(mod, "configprop", new ArgumentInfoMod<>(), ArgumentTypeConfigProperty.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.cyclops.cyclopscore.init.IModBase;

import java.lang.reflect.Field;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -31,6 +32,7 @@ public final class ConfigurablePropertyData<T> {
public final int maxValue;

private Supplier<T> configProperty;
private Consumer<T> configPropertyUpdater;

/**
* Define a new configurable property.
Expand Down Expand Up @@ -66,14 +68,19 @@ public ConfigurablePropertyData(IModBase mod, String category, String name, T de
this.maxValue = maxValue;
}

public void setConfigProperty(Supplier<T> configProperty) {
public void setConfigProperty(Supplier<T> configProperty, Consumer<T> configPropertyUpdater) {
this.configProperty = configProperty;
this.configPropertyUpdater = configPropertyUpdater;
}

public Supplier<T> getConfigProperty() {
return configProperty;
}

public Consumer<T> getConfigPropertyUpdater() {
return configPropertyUpdater;
}

/**
* @return Can this property be changed through commands.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.apache.commons.lang3.tuple.Pair;
import org.cyclops.cyclopscore.command.CommandConfig;
import org.cyclops.cyclopscore.config.ConfigHandlerCommon;
import org.cyclops.cyclopscore.config.extendedconfig.CreativeModeTabConfigCommon;
import org.cyclops.cyclopscore.init.IModBase;
Expand Down Expand Up @@ -95,6 +96,8 @@ protected void loadModCompats(ModCompatLoader modCompatLoader) {
protected LiteralArgumentBuilder<CommandSourceStack> constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) {
LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal(this.getModId());

root.then(CommandConfig.make(this));

return root;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.cyclops.cyclopscore.command.CommandDebug;
import org.cyclops.cyclopscore.command.CommandIgnite;
import org.cyclops.cyclopscore.command.CommandReloadResources;
import org.cyclops.cyclopscore.command.argument.ArgumentTypeConfigPropertyConfig;
import org.cyclops.cyclopscore.command.argument.ArgumentTypeDebugPacketConfig;
import org.cyclops.cyclopscore.command.argument.ArgumentTypeEnumConfig;
import org.cyclops.cyclopscore.component.DataComponentCapacityConfig;
Expand Down Expand Up @@ -85,6 +86,7 @@ protected void onConfigsRegister(ConfigHandlerCommon configHandler) {
configHandler.addConfigurable(new GeneralConfig(this));

// Argument types
configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig<>(this));
configHandler.addConfigurable(new ArgumentTypeDebugPacketConfig<>(this));
configHandler.addConfigurable(new ArgumentTypeEnumConfig<>(this));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ protected <T> void onConfigPropertyInit(ConfigurablePropertyData<T> configProper
.comment(configPropertyData.comment)
.translation(configPropertyData.getLanguageKey())
.define(configPropertyData.name, configPropertyData.defaultValue);
configPropertyData.setConfigProperty(configProperty);
configPropertyData.setConfigProperty(configProperty, (newValue) -> {
configProperty.set(newValue);
configProperty.save();
});

configBuilder.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.cyclops.cyclopscore.command.CommandDebug;
import org.cyclops.cyclopscore.command.CommandIgnite;
import org.cyclops.cyclopscore.command.CommandReloadResources;
import org.cyclops.cyclopscore.command.argument.ArgumentTypeConfigPropertyConfig;
import org.cyclops.cyclopscore.command.argument.ArgumentTypeDebugPacketConfig;
import org.cyclops.cyclopscore.command.argument.ArgumentTypeEnumConfig;
import org.cyclops.cyclopscore.component.DataComponentCapacityConfig;
Expand Down Expand Up @@ -94,6 +95,7 @@ protected void onConfigsRegister(ConfigHandlerCommon configHandler) {
configHandler.addConfigurable(new GeneralConfig(this));

// Argument types
configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig<>(this));
configHandler.addConfigurable(new ArgumentTypeDebugPacketConfig<>(this));
configHandler.addConfigurable(new ArgumentTypeEnumConfig<>(this));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ protected <T> void onConfigPropertyInit(ConfigurablePropertyData<T> configProper
.comment(configPropertyData.comment)
.translation(configPropertyData.getLanguageKey())
.define(configPropertyData.name, configPropertyData.defaultValue);
configPropertyData.setConfigProperty(configProperty);
configPropertyData.setConfigProperty(configProperty, (newValue) -> {
configProperty.set(newValue);
configProperty.save();
});

configBuilder.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void onConfigsRegister(ConfigHandlerCommon configHandler) {
configHandler.addConfigurable(new ContainerInfoBookTestConfig());

// Argument types
configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig());
configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig<>(this));
configHandler.addConfigurable(new ArgumentTypeDebugPacketConfig<>(this));
configHandler.addConfigurable(new ArgumentTypeEnumConfig<>(this));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ protected <T> void onConfigPropertyInit(ConfigurablePropertyData<T> configProper
.comment(configPropertyData.comment)
.translation(configPropertyData.getLanguageKey())
.define(configPropertyData.name, configPropertyData.defaultValue);
configPropertyData.setConfigProperty(configProperty);
configPropertyData.setConfigProperty(configProperty, (newValue) -> {
configProperty.set(newValue);
configProperty.save();
});

configBuilder.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.logging.log4j.Level;
import org.cyclops.cyclopscore.client.key.IKeyRegistry;
import org.cyclops.cyclopscore.client.key.KeyRegistry;
import org.cyclops.cyclopscore.command.CommandConfig;
import org.cyclops.cyclopscore.command.CommandVersion;
import org.cyclops.cyclopscore.config.ConfigHandler;
import org.cyclops.cyclopscore.config.ConfigHandlerCommon;
Expand Down Expand Up @@ -206,7 +205,6 @@ protected IMCHandler constructIMCHandler() {
protected LiteralArgumentBuilder<CommandSourceStack> constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) {
LiteralArgumentBuilder<CommandSourceStack> root = super.constructBaseCommand(selection, context);

root.then(CommandConfig.make(this));
root.then(CommandVersion.make(this));

return root;
Expand Down

0 comments on commit f014a39

Please sign in to comment.