mirror of
https://github.com/TeamMidnightDust/MidnightLib.git
synced 2025-12-17 09:45:10 +01:00
MidnightLib 1.5.0 - 1.20.2, slight redesign & cleanup
- Port to 1.20.2 - "Reset" button now uses a custom icon (closes #25) - Config instances can now specify custom config change behavior (implement "writeChanges" method in your class; closes #33) - Remove TexturedOverlayButtonWidget (replaced by vanilla's TextIconButtonWidget) - Removed radialRainbow (used very rarely) - Updated AutoCommand to support editing list and float config values via commands on serverside installs - Reworded description - Forge & Quilt temporarily disabled until they release, as always
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package eu.midnightdust.lib.config;
|
||||
|
||||
import com.mojang.brigadier.arguments.DoubleArgumentType;
|
||||
import com.mojang.brigadier.arguments.FloatArgumentType;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
@@ -14,9 +15,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class AutoCommand {
|
||||
public static List<LiteralArgumentBuilder<ServerCommandSource>> commands = new ArrayList<>();
|
||||
private LiteralArgumentBuilder<ServerCommandSource> command;
|
||||
final Field entry;
|
||||
final String modid;
|
||||
|
||||
@@ -26,14 +27,7 @@ public class AutoCommand {
|
||||
}
|
||||
|
||||
public void register() {
|
||||
command = CommandManager.literal(modid);
|
||||
command();
|
||||
LiteralArgumentBuilder<ServerCommandSource> finalized = CommandManager.literal("midnightconfig").requires(source -> source.hasPermissionLevel(2)).then(command);
|
||||
|
||||
PlatformFunctions.registerCommand(finalized); commands.add(finalized);
|
||||
}
|
||||
|
||||
private void command() {
|
||||
LiteralArgumentBuilder<ServerCommandSource> command = CommandManager.literal(modid);
|
||||
if (entry.getType() == int.class)
|
||||
command = command.then(CommandManager.literal(this.entry.getName()).executes(ctx -> getValue(ctx.getSource())).then(
|
||||
CommandManager.argument("value", IntegerArgumentType.integer((int) entry.getAnnotation(MidnightConfig.Entry.class).min(),(int) entry.getAnnotation(MidnightConfig.Entry.class).max()))
|
||||
@@ -44,15 +38,18 @@ public class AutoCommand {
|
||||
CommandManager.argument("value", DoubleArgumentType.doubleArg(entry.getAnnotation(MidnightConfig.Entry.class).min(),entry.getAnnotation(MidnightConfig.Entry.class).max()))
|
||||
.executes(ctx -> this.setValue(ctx.getSource(), DoubleArgumentType.getDouble(ctx, "value")))
|
||||
));
|
||||
else if (entry.getType() == float.class)
|
||||
command = command.then(CommandManager.literal(this.entry.getName()).executes(ctx -> getValue(ctx.getSource())).then(
|
||||
CommandManager.argument("value", FloatArgumentType.floatArg((float) entry.getAnnotation(MidnightConfig.Entry.class).min(), (float) entry.getAnnotation(MidnightConfig.Entry.class).max()))
|
||||
.executes(ctx -> this.setValue(ctx.getSource(), FloatArgumentType.getFloat(ctx, "value")))
|
||||
));
|
||||
else if (entry.getType() == boolean.class) {
|
||||
command = command.then(CommandManager.literal(this.entry.getName()).executes(ctx -> getValue(ctx.getSource())).then(
|
||||
CommandManager.literal("true")
|
||||
.executes(ctx -> this.setValue(ctx.getSource(), true))
|
||||
));
|
||||
command = command.then(CommandManager.literal(this.entry.getName()).executes(ctx -> getValue(ctx.getSource())).then(
|
||||
CommandManager.literal("false")
|
||||
.executes(ctx -> this.setValue(ctx.getSource(), false))
|
||||
));
|
||||
for (int i = 0; i < 2; i++) {
|
||||
command = command.then(CommandManager.literal(this.entry.getName()).executes(ctx -> getValue(ctx.getSource())).then(
|
||||
CommandManager.literal(i==0 ? "true":"false")
|
||||
.executes(ctx -> this.setValue(ctx.getSource(), ctx.getInput().endsWith("true")))
|
||||
));
|
||||
}
|
||||
}
|
||||
else if (entry.getType().isEnum()) {
|
||||
for (int i = 0; i < entry.getType().getEnumConstants().length; ++i) {
|
||||
@@ -63,16 +60,28 @@ public class AutoCommand {
|
||||
));
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (entry.getType() == List.class) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
int finalI = i;
|
||||
command = command.then(CommandManager.literal(this.entry.getName()).executes(ctx -> getValue(ctx.getSource())).then(CommandManager.literal(i==0 ? "add":"remove").then(
|
||||
CommandManager.argument("value", StringArgumentType.string())
|
||||
.executes(ctx -> this.setList(ctx.getSource(), StringArgumentType.getString(ctx, "value"), finalI==0))
|
||||
)));
|
||||
}
|
||||
}
|
||||
else {
|
||||
command = command.then(CommandManager.literal(this.entry.getName()).executes(ctx -> getValue(ctx.getSource())).then(
|
||||
CommandManager.argument("value", StringArgumentType.string())
|
||||
.executes(ctx -> this.setValue(ctx.getSource(), StringArgumentType.getString(ctx, "value")))
|
||||
));
|
||||
}
|
||||
}
|
||||
LiteralArgumentBuilder<ServerCommandSource> finalized = CommandManager.literal("midnightconfig").requires(source -> source.hasPermissionLevel(2)).then(command);
|
||||
|
||||
PlatformFunctions.registerCommand(finalized); commands.add(finalized);
|
||||
}
|
||||
private int setValue(ServerCommandSource source, Object value) {
|
||||
try {
|
||||
entry.set(null,value);
|
||||
if (entry.getType() != List.class) entry.set(null,value);
|
||||
MidnightConfig.write(modid);
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -83,14 +92,26 @@ public class AutoCommand {
|
||||
source.sendFeedback(() -> Text.literal("Successfully set " + entry.getName()+" to "+value), true);
|
||||
return 1;
|
||||
}
|
||||
private int setList(ServerCommandSource source, String value, boolean add) {
|
||||
try {
|
||||
List<String> e = (List<String>)entry.get(null);
|
||||
if (add) e.add(value);
|
||||
else if (!e.contains(value)) throw new IllegalArgumentException("List does not contain this string!");
|
||||
else e.remove(value);
|
||||
MidnightConfig.write(modid);
|
||||
}
|
||||
catch (Exception e) {
|
||||
source.sendError(Text.literal((add ? "Could not add "+value+" to " : "Could not remove "+value+" from ")+entry.getName() +": " + e));
|
||||
return 0;
|
||||
}
|
||||
source.sendFeedback(() -> Text.literal((add ? "Successfully added " +value+" to " : "Successfully removed " +value+" from ") +entry.getName()), true);
|
||||
return 1;
|
||||
}
|
||||
private int getValue(ServerCommandSource source) {
|
||||
source.sendFeedback(() -> {
|
||||
try {
|
||||
return Text.literal("The value of "+entry.getName()+" is "+entry.get(null));
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}, false);
|
||||
try {return Text.literal("The value of "+entry.getName()+" is "+entry.get(null));
|
||||
} catch (IllegalAccessException e) {throw new RuntimeException(e);}
|
||||
}, true);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user