Skip to content

Commit

Permalink
Actual working commands
Browse files Browse the repository at this point in the history
  • Loading branch information
OroArmor committed Apr 4, 2021
1 parent 6f10595 commit 451b182
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 235 deletions.
74 changes: 0 additions & 74 deletions common/src/main/java/com/oroarmor/config/ArrayConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import org.jetbrains.annotations.Nullable;

import net.minecraft.command.CommandSource;

import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;


/**
* {@link ArrayConfigItem} stores an array of the supported types <br>
Expand Down Expand Up @@ -172,69 +161,6 @@ public <S> boolean isValidType(Class<S> clazz) {
return clazz == defaultValue[0].getClass();
}

@SuppressWarnings("unchecked")
@Override
public <S extends CommandSource> ArgumentBuilder<?, ?> getSetCommand(ConfigItemGroup group, Config config) {
ArgumentBuilder<S, ?> setCommand;
switch (defaultValue[0].getClass().isEnum() ? "ENUM" : defaultValue[0].getClass().getSimpleName().toUpperCase()) {
case "BOOLEAN":
setCommand = RequiredArgumentBuilder.<S, Boolean>argument("boolean", BoolArgumentType.bool()).executes(c -> {
boolean result = BoolArgumentType.getBool(c, "boolean");
int index = IntegerArgumentType.getInteger(c, "index");
this.setValue((T) (Object) result, index);
config.saveConfigToFile();
return 1;
});
break;

case "INTEGER":
setCommand = RequiredArgumentBuilder.<S, Integer>argument("int", IntegerArgumentType.integer()).executes(c -> {
int result = IntegerArgumentType.getInteger(c, "int");
int index = IntegerArgumentType.getInteger(c, "index");
this.setValue((T) (Object) result, index);
config.saveConfigToFile();
return 1;
});
break;
case "DOUBLE":
setCommand = RequiredArgumentBuilder.<S, Double>argument("double", DoubleArgumentType.doubleArg()).executes(c -> {
double result = DoubleArgumentType.getDouble(c, "double");
int index = IntegerArgumentType.getInteger(c, "index");
this.setValue((T) (Object) result, index);
config.saveConfigToFile();
return 1;
});
break;

case "STRING":
setCommand = RequiredArgumentBuilder.<S, String>argument("string", StringArgumentType.greedyString()).executes(c -> {
String result = StringArgumentType.getString(c, "string");
int index = IntegerArgumentType.getInteger(c, "index");
this.setValue((T) result, index);
config.saveConfigToFile();
return 1;
});
break;

case "ENUM":
setCommand = literal("set");
Enum<?>[] enums = ((Enum<?>) this.getValue()[0]).getClass().getEnumConstants();
for (Enum<?> _enum : enums) {
setCommand.then(LiteralArgumentBuilder.<S>literal(_enum.toString()).executes(c -> {
int index = IntegerArgumentType.getInteger(c, "index");
this.setValue((T) _enum, index);
config.saveConfigToFile();
return 1;
}));
}
break;

default:
throw new IllegalStateException("Class " + defaultValue[0].getClass().getSimpleName() + " is an unsupported type");
}
return RequiredArgumentBuilder.<S, Integer>argument("index", IntegerArgumentType.integer(0, this.value.length)).then(setCommand);
}

@Override
public String getCommandValue() {
return Arrays.stream(this.value).map(Object::toString).collect(Collectors.joining(","));
Expand Down
16 changes: 0 additions & 16 deletions common/src/main/java/com/oroarmor/config/BooleanConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import org.jetbrains.annotations.Nullable;

import net.minecraft.command.CommandSource;

import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;

public class BooleanConfigItem extends ConfigItem<Boolean> {
public BooleanConfigItem(String name, Boolean defaultValue, String details) {
super(name, defaultValue, details);
Expand All @@ -60,16 +54,6 @@ public <T> boolean isValidType(Class<T> clazz) {
return clazz == Boolean.class;
}

@Override
public <S extends CommandSource> ArgumentBuilder<?, ?> getSetCommand(ConfigItemGroup group, Config config) {
return argument("boolean", BoolArgumentType.bool()).executes(c -> {
boolean result = BoolArgumentType.getBool(c, "boolean");
this.setValue(result);
config.saveConfigToFile();
return 1;
});
}

@Override
public String getCommandValue() {
return this.value.toString();
Expand Down
48 changes: 0 additions & 48 deletions common/src/main/java/com/oroarmor/config/ConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.builder.ArgumentBuilder;
import org.jetbrains.annotations.Nullable;

import net.minecraft.command.CommandSource;

/**
* {@link ConfigItem} often stores a name and a value for saving data into a
* config. <br>
Expand Down Expand Up @@ -138,8 +135,6 @@ public String toString() {

public abstract <T1> boolean isValidType(Class<T1> clazz);

public abstract <S extends CommandSource> ArgumentBuilder<?, ?> getSetCommand(ConfigItemGroup group, Config config);

public abstract String getCommandValue();

public boolean atDefaultValue() {
Expand All @@ -149,47 +144,4 @@ public boolean atDefaultValue() {
public String getCommandDefaultValue() {
return this.value.toString();
}

// /**
// * The current types for the config items
// *
// * @author Eli Orona
// */
// public enum Type {
// BOOLEAN, INTEGER, DOUBLE, STRING, GROUP, ENUM;
//
// /**
// * Gets the corresponding type for an object. If the object's type is not
// * supported, this returns null.
// *
// * @param value The object to find the type for
// * @return The type for that object
// */
// public static Type getTypeFrom(Object value) {
// if (value instanceof Boolean) {
// return BOOLEAN;
// }
// if (value instanceof Integer) {
// return INTEGER;
// }
// if (value instanceof Double) {
// return DOUBLE;
// }
// if (value instanceof String) {
// return STRING;
// }
// if (value instanceof ConfigItemGroup) {
// return GROUP;
// }
// if (value instanceof Enum) {
// return ENUM;
// }
//
// if (value != null && value.getClass().isArray()) {
// return getTypeFrom(((Object[]) value)[0]);
// }
//
// return null;
// }
// }
}
8 changes: 0 additions & 8 deletions common/src/main/java/com/oroarmor/config/ConfigItemGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.builder.ArgumentBuilder;

import net.minecraft.command.CommandSource;

/**
* Extending {@link ConfigItem}, {@link ConfigItemGroup} can store multiple
Expand Down Expand Up @@ -116,11 +113,6 @@ public <T> boolean isValidType(Class<T> clazz) {
return clazz == this.getClass();
}

@Override
public <S extends CommandSource> ArgumentBuilder<S, ?> getSetCommand(ConfigItemGroup group, Config config) {
return null;
}

@Override
public String getCommandValue() {
return null;
Expand Down
25 changes: 8 additions & 17 deletions common/src/main/java/com/oroarmor/config/DoubleConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,8 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import org.jetbrains.annotations.Nullable;

import net.minecraft.command.CommandSource;

import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;

public class DoubleConfigItem extends ConfigItem<Double> {
protected double min = Double.MIN_VALUE;
protected double max = Double.MAX_VALUE;
Expand Down Expand Up @@ -75,16 +68,6 @@ public <T> boolean isValidType(Class<T> clazz) {
return clazz == Double.class;
}

@Override
public <S extends CommandSource> ArgumentBuilder<?, ?> getSetCommand(ConfigItemGroup group, Config config) {
return argument("double", DoubleArgumentType.doubleArg(min, max)).executes(c -> {
double result = DoubleArgumentType.getDouble(c, "double");
this.setValue(result);
config.saveConfigToFile();
return 1;
});
}

@Override
public void setValue(Double value) {
super.setValue(Double.max(Double.min(value, max), min));
Expand All @@ -94,4 +77,12 @@ public void setValue(Double value) {
public String getCommandValue() {
return this.value.toString();
}

public double getMin() {
return min;
}

public double getMax() {
return max;
}
}
21 changes: 0 additions & 21 deletions common/src/main/java/com/oroarmor/config/EnumConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import org.jetbrains.annotations.Nullable;

import net.minecraft.command.CommandSource;

import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;

public class EnumConfigItem<T extends Enum<T>> extends ConfigItem<T> {
public EnumConfigItem(String name, T defaultValue, String details) {
super(name, defaultValue, details);
Expand All @@ -62,21 +56,6 @@ public <T1> boolean isValidType(Class<T1> clazz) {
return clazz == defaultValue.getClass();
}

@Override
@SuppressWarnings("unchecked")
public <S extends CommandSource> ArgumentBuilder<?, ?> getSetCommand(ConfigItemGroup group, Config config) {
LiteralArgumentBuilder<S> builder = literal("set");
Enum<?>[] enums = ((Enum<?>) this.getValue()).getClass().getEnumConstants();
for (Enum<?> _enum : enums) {
builder.then(LiteralArgumentBuilder.<S>literal(_enum.toString()).executes(c -> {
this.setValue((T) _enum);
config.saveConfigToFile();
return 1;
}));
}
return builder;
}

@Override
public String getCommandValue() {
return this.value.toString();
Expand Down
24 changes: 8 additions & 16 deletions common/src/main/java/com/oroarmor/config/IntegerConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import org.jetbrains.annotations.Nullable;

import net.minecraft.command.CommandSource;

import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;

public class IntegerConfigItem extends ConfigItem<Integer> {
protected int min = Integer.MIN_VALUE;
protected int max = Integer.MAX_VALUE;
Expand Down Expand Up @@ -74,16 +68,6 @@ public <T> boolean isValidType(Class<T> clazz) {
return clazz == Integer.class;
}

@Override
public <S extends CommandSource> ArgumentBuilder<?, ?> getSetCommand(ConfigItemGroup group, Config config) {
return argument("int", IntegerArgumentType.integer(min, max)).executes(c -> {
int result = IntegerArgumentType.getInteger(c, "int");
this.setValue(result);
config.saveConfigToFile();
return 1;
});
}

@Override
public void setValue(Integer value) {
super.setValue(Integer.max(Integer.min(value, max), min));
Expand All @@ -93,4 +77,12 @@ public void setValue(Integer value) {
public String getCommandValue() {
return this.value.toString();
}

public int getMin() {
return min;
}

public int getMax() {
return max;
}
}
16 changes: 0 additions & 16 deletions common/src/main/java/com/oroarmor/config/StringConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import org.jetbrains.annotations.Nullable;

import net.minecraft.command.CommandSource;

import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;

public class StringConfigItem extends ConfigItem<String> {
public StringConfigItem(String name, String defaultValue, String details) {
super(name, defaultValue, details);
Expand All @@ -60,16 +54,6 @@ public <T> boolean isValidType(Class<T> clazz) {
return clazz == String.class;
}

@Override
public <S extends CommandSource> ArgumentBuilder<?, ?> getSetCommand(ConfigItemGroup group, Config config) {
return argument("string", StringArgumentType.string()).executes(c -> {
String result = StringArgumentType.getString(c, "string");
this.setValue(result);
config.saveConfigToFile();
return 1;
});
}

@Override
public String getCommandValue() {
return this.value;
Expand Down
Loading

0 comments on commit 451b182

Please sign in to comment.