Skip to content

Commit

Permalink
sorta plug in the new config system
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Mar 29, 2023
1 parent 34863fa commit 4f5addf
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import agency.highlysuspect.apathy.config.MobConfig;
import agency.highlysuspect.apathy.core.ApathyHell;
import agency.highlysuspect.apathy.core.CoreOptions;
import agency.highlysuspect.apathy.core.JsonRule;
import agency.highlysuspect.apathy.core.TriState;
import agency.highlysuspect.apathy.core.rule.Rule;
import agency.highlysuspect.apathy.core.newconfig.ConfigSchema;
import agency.highlysuspect.apathy.core.wrapper.Attacker;
import agency.highlysuspect.apathy.core.wrapper.Defender;
import agency.highlysuspect.apathy.core.wrapper.DragonDuck;
Expand All @@ -21,7 +20,6 @@
import agency.highlysuspect.apathy.rule.PartialSpecLocation;
import agency.highlysuspect.apathy.rule.PartialSpecRevengeTimer;
import agency.highlysuspect.apathy.rule.PartialSpecScore;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Difficulty;
import net.minecraft.world.entity.Entity;
Expand Down Expand Up @@ -110,23 +108,6 @@ public void noticePlayerAttack(Player player, Entity provoked) {
if(provoked instanceof DragonDuck dragn) dragn.apathy$allowAttackingPlayers();
}

//Random util crap
public static ResourceLocation id(String path) {
return new ResourceLocation(MODID, path);
}

public static <T extends Enum<?>> Set<T> allOf(Class<T> enumClass) {
Set<T> set = new HashSet<>();
Collections.addAll(set, enumClass.getEnumConstants());
return set;
}

public static Set<Difficulty> allDifficultiesNotPeaceful() {
Set<Difficulty> wow = allOf(Difficulty.class);
wow.remove(Difficulty.PEACEFUL);
return wow;
}

/// Cross platform stuff

@Override
Expand All @@ -145,7 +126,23 @@ public void addRules() {
partialSerializers.register("apathy:score", PartialSpecScore.Serializer.INSTANCE);
}

public abstract void installConfigFileReloader();
public abstract void installCommandRegistrationCallback();
public abstract void installPlayerSetManagerTicker();
@Override
public void addMobConfig(ConfigSchema schema) {
super.addMobConfig(schema);
PlatformOptions.Mobs.visit(schema);
}

@Deprecated(forRemoval = true)
public static <T extends Enum<?>> Set<T> allOf(Class<T> enumClass) {
Set<T> set = new HashSet<>();
Collections.addAll(set, enumClass.getEnumConstants());
return set;
}

@Deprecated(forRemoval = true)
public static Set<Difficulty> allDifficultiesNotPeaceful() {
Set<Difficulty> wow = allOf(Difficulty.class);
wow.remove(Difficulty.PEACEFUL);
return wow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package agency.highlysuspect.apathy;

import agency.highlysuspect.apathy.core.newconfig.ConfigProperty;
import agency.highlysuspect.apathy.core.newconfig.ConfigSchema;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.EntityType;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

public class PlatformOptions {
private static <B extends ConfigProperty.Builder<Set<EntityType<?>>, B>> B entityTypeSetOpt(String name, Set<EntityType<?>> defaultValue, String... comment) {
return new ConfigProperty.Builder<Set<EntityType<?>>, B>(name, Set.class, defaultValue)
.comment(comment)
.writer(set -> set.stream()
.map(Registry.ENTITY_TYPE::getKey)
.map(ResourceLocation::toString)
.collect(Collectors.joining(", ")))
.parser(s -> Arrays.stream(s.split(","))
.map(String::trim)
.map(ResourceLocation::new)
.map(Registry.ENTITY_TYPE::get)
.collect(Collectors.toSet()));
}

private static <B extends ConfigProperty.Builder<Set<TagKey<EntityType<?>>>, B>> B entityTypeTagKeySetOpt(String name, Set<TagKey<EntityType<?>>> defaultValue, String... comment) {
return new ConfigProperty.Builder<Set<TagKey<EntityType<?>>>, B>(name, Set.class, defaultValue)
.comment(comment)
.writer(set -> set.stream()
.map(TagKey::location)
.map(ResourceLocation::toString)
.collect(Collectors.joining(", ")))
.parser(s -> Arrays.stream(s.split(","))
.map(String::trim)
.map(ResourceLocation::new)
.map(rl -> TagKey.create(Registry.ENTITY_TYPE_REGISTRY, rl))
.collect(Collectors.toSet()));
}

public static class Mobs {
public static final ConfigProperty<Set<EntityType<?>>> mobSet = entityTypeSetOpt("mobSet", Collections.emptySet(), "A comma-separated set of mob IDs.")
.example("minecraft:creeper, minecraft:spider")
.build();

public static final ConfigProperty<Set<TagKey<EntityType<?>>>> tagSet = entityTypeTagKeySetOpt("tagSet", Collections.emptySet(), "A comma-separated set of entity type tags.")
.example("minecraft:raiders, some_datapack:some_tag")
.build();

public static void visit(ConfigSchema schema) {
schema.getSection("Mob Set Rule").add(0, mobSet);
schema.getSection("Tag Set Rule").add(0, tagSet);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class ApathyHell {
public final NotRegistry<PartialSerializer<?>> partialSerializers = new NotRegistry<>();

public CookedConfig generalConfigCooked; //TODO remove -cooked suffix after migrating over, it name-clashes rn
public CookedConfig mobConfigCooked;
public CookedConfig mobsConfigCooked;
public CookedConfig bossConfigCooked;
public @Nullable Rule jsonRule;

Expand Down Expand Up @@ -71,6 +71,14 @@ public void init() {
addGeneralConfig(generalConfigSchema);
generalConfigCooked = generalConfigBakery().cook(generalConfigSchema);

ConfigSchema mobsConfigSchema = new ConfigSchema();
addMobConfig(mobsConfigSchema);
mobsConfigCooked = mobsConfigBakery().cook(mobsConfigSchema);

ConfigSchema bossConfigSchema = new ConfigSchema();
addBossConfig(bossConfigSchema);
bossConfigCooked = bossConfigBakery().cook(bossConfigSchema);

//Misc
installConfigFileReloader();
installCommandRegistrationCallback();
Expand All @@ -81,6 +89,8 @@ public boolean loadConfig() {
boolean ok = true;

ok &= generalConfigCooked.refresh();
ok &= mobsConfigCooked.refresh();
ok &= bossConfigCooked.refresh();

Rule newJsonRule = jsonRule;
try {
Expand Down Expand Up @@ -115,14 +125,16 @@ public void addGeneralConfig(ConfigSchema schema) {
}

public void addMobConfig(ConfigSchema schema) {

CoreOptions.Mobs.visit(schema);
}

public void addBossConfig(ConfigSchema schema) {

CoreOptions.Boss.visit(schema);
}

public abstract ConfigSchema.Bakery generalConfigBakery();
public abstract ConfigSchema.Bakery mobsConfigBakery();
public abstract ConfigSchema.Bakery bossConfigBakery();

public abstract void installConfigFileReloader();
public abstract void installCommandRegistrationCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -79,7 +80,7 @@ public static <RULE extends SerializableRuleSpec<RULE>> void dump(RuleSpec<RULE>
Path outPath = dumpDir.resolve(filename + ".json");
ApathyHell.instance.log.info("Dumping rule to " + outPath);
JsonObject json = ApathyHell.instance.writeRule(ruleSpec);
Files.writeString(outPath, GSON.toJson(json));
Files.write(outPath, GSON.toJson(json).getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
ApathyHell.instance.log.error("Problem dumping rule to " + filename, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,44 @@
import java.util.Map;

public class ConfigSchema {
List<Object> entries = new ArrayList<>();
private final Map<String, List<ConfigProperty<?>>> entries = new LinkedHashMap<>();

private static final String SECTIONLESS = "\ud83d\udc09";
private String currentSection = SECTIONLESS;

public void section(String sectionName) {
entries.add(sectionName);
currentSection = sectionName == null ? SECTIONLESS : sectionName;
}

public void option(ConfigProperty<?>... options) {
entries.addAll(Arrays.asList(options));
getSection(currentSection).addAll(Arrays.asList(options));
}

public void section(String sectionName, ConfigProperty<?>... options) {
section(sectionName);
option(options);
}

///

public List<ConfigProperty<?>> getSection(String name) {
return entries.computeIfAbsent(name, __ -> new ArrayList<>());
}

///

public interface Visitor {
default void visitSection(String section) {}
default <T> void visitOption(ConfigProperty<T> option) {}
}

public void accept(Visitor visitor) {
entries.forEach((section, options) -> {
if(!SECTIONLESS.equals(section)) visitor.visitSection(section);
options.forEach(visitor::visitOption);
});
}

public Map<String, ConfigProperty<?>> propertiesByName() {
Map<String, ConfigProperty<?>> props = new LinkedHashMap<>();
accept(new Visitor() {
Expand All @@ -33,17 +56,7 @@ public <T> void visitOption(ConfigProperty<T> option) {
return props;
}

public void accept(Visitor visitor) {
for(Object thing : entries) {
if(thing instanceof String) visitor.visitSection((String) thing);
else if(thing instanceof ConfigProperty<?>) visitor.visitOption((ConfigProperty<?>) thing);
}
}

public interface Visitor {
default void visitSection(String section) {}
default <T> void visitOption(ConfigProperty<T> option) {}
}
///

public interface Bakery {
CookedConfig cook(ConfigSchema schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ public <T> void visitOption(ConfigProperty<T> option) {
first = false;
}

//silly special-case
if(!option.name().equals("configVersion")) {
if(!option.name().equals("configVersion")) { //silly special-case
T defaultValue = option.defaultValue();
String writtenDefaultValue = option.write(defaultValue);
out.add("# Default: " + writtenDefaultValue);
}

T currentValue = get(option);
String writtenCurrentValue = option.write(currentValue);
if(writtenCurrentValue.isEmpty()) writtenCurrentValue = "<empty>";
out.add(option.name() + ": " + writtenCurrentValue);

out.add("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void installConfigFileReloader() {
ResourceManagerHelper.get(PackType.SERVER_DATA).registerReloadListener(new SimpleSynchronousResourceReloadListener() {
@Override
public ResourceLocation getFabricId() {
return id("reload-config");
return new ResourceLocation(MODID, "reload-config");
}

@Override
Expand All @@ -54,4 +54,14 @@ public void installPlayerSetManagerTicker() {
public ConfigSchema.Bakery generalConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("general.cfg"));
}

@Override
public ConfigSchema.Bakery mobsConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("mobs.cfg"));
}

@Override
public ConfigSchema.Bakery bossConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("boss.cfg"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,21 @@ public void installPlayerSetManagerTicker() {
});
}

//TODO, write a forge config bakery
@Override
public ConfigSchema.Bakery generalConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("general.cfg")); //TODO, write a forge config bakery
return new CrummyConfig.Bakery(configPath.resolve("general.cfg"));
}

//TODO, write a forge config bakery
@Override
public ConfigSchema.Bakery mobsConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("mobs.cfg"));
}

//TODO, write a forge config bakery
@Override
public ConfigSchema.Bakery bossConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("boss.cfg"));
}
}

0 comments on commit 4f5addf

Please sign in to comment.