Skip to content

Commit

Permalink
no null for you
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed Aug 2, 2023
1 parent 0588695 commit fdc3b7a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
public class CreateUnlimited {
public static final String ID = "createunlimited";
public static final String NAME = "Create Unlimited";
public static final String VERSION = "0.4.1";
public static final String VERSION = "0.5.0";
public static final Logger LOGGER = LoggerFactory.getLogger(NAME);

public static void init() {
LOGGER.info("{} initializing! Create version: {} on platform: {}", NAME, Create.VERSION, Util.platformName());
LOGGER.info("{} v{} initializing! Create version: {} on platform: {}", NAME, VERSION, Create.VERSION, Util.platformName());

EnumArgument.init();
Util.registerConfig(ID, ModConfig.Type.SERVER, CUConfig.SPEC, "createunlimited.toml");
CUConfig.init(Util.getConfigDirectory().resolve("createunlimited-IGNOREME.toml"));

CUCommands.registerConfigCommand();
Util.registerArgument("enumargument", EnumArgument.class, new EnumArgument.Info(), asResource("enumargument"));
}

public static ResourceLocation asResource(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public static void registerConfigCommand() {
return Command.SINGLE_SUCCESS;
});

LiteralArgumentBuilder<CommandSourceStack> category = null;
LiteralArgumentBuilder<CommandSourceStack> category = base;

for (Field field : CUConfig.class.getDeclaredFields()) {
//skip if not config value or string
if (!ForgeConfigSpec.ConfigValue.class.isAssignableFrom(field.getType()) && field.getType() != String.class) continue;
Expand All @@ -83,15 +84,14 @@ public static void registerConfigCommand() {
category = literal(field.getName());

//add description for category
assert base != null;
base.then(literal(field.getName()).executes(context -> {
message(CUConfig.comments.get(field.getName()), context);
return Command.SINGLE_SUCCESS;
}));

continue;
}
if(category == null)
category = base; // if no category, append everything to base

// get config as ConfigValue
ForgeConfigSpec.ConfigValue<?> value;
Expand All @@ -109,15 +109,15 @@ public static void registerConfigCommand() {
if (value instanceof ForgeConfigSpec.BooleanValue bValue)
setBoolean(category, field, bValue);

// set for enums
// set for enums
else if (value.get() instanceof Enum<?>)
setEnum(category, field, (ForgeConfigSpec.EnumValue<? extends Enum<?>>) value);

// set for int
// set for int
else if (value instanceof ForgeConfigSpec.IntValue iValue)
setInt(category, field, iValue);

// set for double
// set for double
else if (value instanceof ForgeConfigSpec.DoubleValue dValue)
setDouble(category, field, dValue);

Expand All @@ -131,6 +131,9 @@ private static boolean perms(CommandSourceStack source) {
return source.hasPermission(4) || !source.getLevel().getServer().isDedicatedServer();
}

private static boolean perms(Object o) {
return o instanceof CommandSourceStack source && perms(source);
}

private static <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category, Field field, ForgeConfigSpec.ConfigValue<T> value) {
category.then(literal(field.getName())
Expand Down Expand Up @@ -195,7 +198,7 @@ private static void setDouble(LiteralArgumentBuilder<CommandSourceStack> categor
@SuppressWarnings("unchecked")
private static <T extends Enum<T>> void setEnum(LiteralArgumentBuilder<CommandSourceStack> category, Field field, ForgeConfigSpec.EnumValue<T> value) {
category.then(literal(field.getName())
.then(argument("value", EnumArgument.enumArg(value.get().getClass(), true))
.then(argument("value", EnumArgument.enumArg(value.get().getClass(), true)).requires(CUCommands::perms)
.executes(context -> {
T set = (T) context.getArgument("value", value.get().getClass());
value.set(set);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import dev.rdh.createunlimited.CreateUnlimited;
import dev.rdh.createunlimited.util.Util;

import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
Expand All @@ -36,6 +42,10 @@ public class EnumArgument<T extends Enum<T>> implements ArgumentType<T> {
private final Class<T> enumClass;
private final boolean lowercase;

public static void init() {
Util.registerArgument("enumargument", EnumArgument.class, new EnumArgument.Info(), CreateUnlimited.asResource("enumargument"));
}

public static <R extends Enum<R>> EnumArgument<R> enumArg(Class<R> enumClass, boolean lowercase) {
return new EnumArgument<>(enumClass, lowercase);
}
Expand Down Expand Up @@ -90,7 +100,7 @@ public void serializeToJson(Template template, JsonObject json) {
}

@Override
public Template unpack(EnumArgument<T> argument) {
public @NotNull Template unpack(EnumArgument<T> argument) {
return new Template(argument.enumClass, argument.lowercase);
}

Expand All @@ -104,12 +114,12 @@ public class Template implements ArgumentTypeInfo.Template<EnumArgument<T>> {
}

@Override
public EnumArgument<T> instantiate(CommandBuildContext ctx) {
public @NotNull EnumArgument<T> instantiate(@NotNull CommandBuildContext ctx) {
return new EnumArgument<>(this.enumClass, this.lowercase);
}

@Override
public ArgumentTypeInfo<EnumArgument<T>, ?> type() {
public @NotNull ArgumentTypeInfo<EnumArgument<T>, ?> type() {
return Info.this;
}
}
Expand Down
2 changes: 1 addition & 1 deletion qodana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ profile:
# paths:
# - <path/where/not/run/inspection>

projectJDK: zulu-17 #(Applied in CI/CD pipeline)
projectJDK: temurin-17 #(Applied in CI/CD pipeline)

#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
#bootstrap: sh ./prepare-qodana.sh
Expand Down

0 comments on commit fdc3b7a

Please sign in to comment.