Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182package net.codexarchonic.infinity.util.config;
import net.codexarchonic.infinity.InfinityMod;import net.codexarchonic.infinity.dimensions.RandomNoisePreset;import net.codexarchonic.infinity.util.VersionMethods;import net.codexarchonic.infinity.util.core.CommonIO;import net.codexarchonic.infinity.util.core.NbtUtils;import net.minecraft.core.Registry;import net.minecraft.core.registries.Registries;import net.minecraft.nbt.*;import net.minecraft.server.MinecraftServer;import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;import org.jspecify.annotations.Nullable;
import java.util.*;
public interface SurfaceRuleScanner { static void scan(MinecraftServer server) { Map<String, CompoundTag> map = new HashMap<>(); Registry<NoiseGeneratorSettings> registry = VersionMethods.getRegistry(server.registryAccess(), Registries.NOISE_SETTINGS); registry.registryKeySet().forEach(key -> { //~ if >26 'location'->'identifier' if (!key.location().getNamespace().contains("infinity")) { Optional<NoiseGeneratorSettings> o = registry.getOptional(key); o.ifPresent(settings -> { Optional<Tag> c = NoiseGeneratorSettings.DIRECT_CODEC.encodeStart(NbtOps.INSTANCE, settings).result(); c.ifPresent(e -> { Tree t = new Tree(NbtUtils.getCompound(((CompoundTag) e), "surface_rule")); t.biomeLocations.keySet().forEach(biome -> { if (!map.containsKey(biome)) map.put(biome, t.wrappedRule(biome)); }); }); }); } }); map.forEach((biome, value) -> { String biomename = biome.substring(biome.lastIndexOf(":") + 1) + ".json"; String modname = biome.substring(0, biome.lastIndexOf(":")); String path = "config/infinity/modular/" + modname + "/surface_rule"; CommonIO.writeSurfaceRule(value, path, biomename); }); DataCollection.loggerOutput(map.size(), "surface rules"); }
class Tree{ ArrayList<TreeLeaf> registry; HashMap<String, ArrayList<Integer>> biomeLocations;
Tree(CompoundTag surfaceRule) { registry = new ArrayList<>(); biomeLocations = new HashMap<>(); TreeLeaf root = new TreeLeaf(new CompoundTag(), -1, null, false); add(surfaceRule, root); }
static CompoundTag conditionCase(CompoundTag if_true, CompoundTag then_run) { CompoundTag c = new CompoundTag(); c.put("if_true", if_true); c.put("then_run", then_run); c.putString("type", "minecraft:condition"); return c; }
void addBiomeLoc(String s, Integer i) { if (!biomeLocations.containsKey(s)) biomeLocations.put(s, new ArrayList<>()); biomeLocations.get(s).add(i); }
TreeLeaf addOfRule(CompoundTag rule, TreeLeaf where, boolean terminal) { TreeLeaf l = new TreeLeaf(rule, registry.size(), where, terminal); registry.add(l); return l; }
void add(CompoundTag rule, TreeLeaf where) { switch(NbtUtils.getString(rule,"type", "")) { case "condition", "minecraft:condition" -> { CompoundTag next = NbtUtils.getCompound(rule,"then_run"); CompoundTag c = NbtUtils.getCompound(rule, "if_true"); if (NbtUtils.getString(c, "type").contains("above_preliminary_surface")) { add(next, where); } else if (!NbtUtils.getString(c, "type").contains("biome")) { TreeLeaf l = addOfRule(c, where, false); add(next, l); } else if (NbtUtils.compoundHasList(c, "biome_is")) { TreeLeaf l = addOfRule(next, where, true); NbtUtils.getList(c, "biome_is", Tag.TAG_STRING).forEach(e -> addBiomeLoc(NbtUtils.getAsString(e), l.i)); } } case "sequence", "minecraft:sequence" -> { ListTag sq = NbtUtils.getList(rule, "sequence", Tag.TAG_COMPOUND); sq.forEach(e -> add((CompoundTag)e, where)); } default -> { if (!checkUnneededParts(rule)) { TreeLeaf l = addOfRule(rule, where, true); addBiomeLoc("minecraft:default", l.i); } } } }
static boolean checkUnneededParts(CompoundTag rule) { return NbtUtils.getString(rule, "type", "").contains("block") && (rule.toString().contains("minecraft:bedrock") || rule.toString().contains("minecraft:deepslate")); }
public CompoundTag wrappedRule(String biome) { CompoundTag c = new CompoundTag(); ListTag l = new ListTag(); l.add(StringTag.valueOf(biome)); c.put("biomes", l); c.put("rule", extractRule(biome)); return c; }
public CompoundTag extractRule(String biome) { if (!biomeLocations.containsKey(biome)) return null; else { CompoundTag comp = RandomNoisePreset.startingRule("sequence"); ListTag l = new ListTag(); try { biomeLocations.get(biome).forEach(i -> l.add(extractRule(i))); if (biomeLocations.containsKey("minecraft:default")) biomeLocations.get("minecraft:default").forEach(i -> l.add(extractRule(i))); else { InfinityMod.LOGGER.warn("Default locations unexpectedly missing when processing surface rules for biome {}", biome); } comp.put("sequence", l); } catch (Exception e) { throw new RuntimeException("Encountered an unexpected exception when processing surface rules for biome " + biome + "\n" + e.getMessage()); } biomeLocations.get(biome).forEach(i -> l.add(extractRule(i))); if (biomeLocations.containsKey("minecraft:default")) biomeLocations.get("minecraft:default").forEach(i -> l.add(extractRule(i))); comp.put("sequence", l); return comp; } }
TreeLeaf getParent(TreeLeaf l) { if (l.i_parent == -1) return l; return registry.get(l.i_parent); }
CompoundTag extractRule(int i) { TreeLeaf l = registry.get(i); assert l.is_terminal; if (l.i_parent == -1) return l.data; return extractRule(getParent(l), l.data); }
CompoundTag extractRule(TreeLeaf l, CompoundTag data) { CompoundTag newdata = conditionCase(l.data, data); if (l.i_parent == -1) return newdata; return extractRule(getParent(l), newdata); }
static class TreeLeaf{ CompoundTag data; int i; int i_parent; boolean is_terminal;
TreeLeaf(CompoundTag c, int num, @Nullable TreeLeaf where, boolean e) { data = c; i = num; if (where == null) { i_parent = -1; } else { i_parent = where.i; } is_terminal = e; } } }}