move tagutil into common

This commit is contained in:
dfsek 2022-06-22 06:10:41 -07:00
parent 3b156586dc
commit 2ae4f80351
4 changed files with 4 additions and 114 deletions

View File

@ -1,6 +1,6 @@
package com.dfsek.terra.fabric.mixin.lifecycle;
import com.dfsek.terra.fabric.util.TagUtil;
import com.dfsek.terra.mod.util.TagUtil;
import com.dfsek.terra.mod.util.MinecraftUtil;
import net.minecraft.server.DataPackContents;

View File

@ -1,107 +0,0 @@
package com.dfsek.terra.fabric.util;
import com.dfsek.terra.mod.util.MinecraftUtil;
import com.dfsek.terra.mod.util.PresetUtil;
import com.google.common.collect.ImmutableMap;
import net.minecraft.tag.TagKey;
import net.minecraft.tag.WorldPresetTags;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.WorldPreset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class TagUtil {
private static final Logger logger = LoggerFactory.getLogger(TagUtil.class);
private TagUtil() {
}
private static <T> Map<TagKey<T>, List<RegistryEntry<T>>> tagsToMutableMap(Registry<T> registry) {
return registry
.streamTagsAndEntries()
.collect(HashMap::new,
(map, pair) ->
map.put(pair.getFirst(), new ArrayList<>(pair.getSecond().stream().toList())),
HashMap::putAll);
}
public static void registerWorldPresetTags(Registry<WorldPreset> registry) {
logger.info("Doing preset tag garbage....");
Map<TagKey<WorldPreset>, List<RegistryEntry<WorldPreset>>> collect = tagsToMutableMap(registry);
PresetUtil
.getPresets()
.forEach(id -> MinecraftUtil
.getEntry(registry, id)
.ifPresentOrElse(
preset -> collect
.computeIfAbsent(WorldPresetTags.NORMAL, tag -> new ArrayList<>())
.add(preset),
() -> logger.error("Preset {} does not exist!", id)));
registry.clearTags();
registry.populateTags(ImmutableMap.copyOf(collect));
}
public static void registerBiomeTags(Registry<Biome> registry) {
logger.info("Doing biome tag garbage....");
Map<TagKey<Biome>, List<RegistryEntry<Biome>>> collect = tagsToMutableMap(registry);
MinecraftUtil
.getTerraBiomeMap()
.forEach((vb, terraBiomes) ->
MinecraftUtil
.getEntry(registry, vb)
.ifPresentOrElse(
vanilla -> terraBiomes
.forEach(tb -> MinecraftUtil
.getEntry(registry, tb)
.ifPresentOrElse(
terra -> {
logger.debug(
vanilla.getKey()
.orElseThrow()
.getValue() +
" (vanilla for " +
terra.getKey()
.orElseThrow()
.getValue() +
": " +
vanilla.streamTags()
.toList());
vanilla.streamTags()
.forEach(
tag -> collect
.computeIfAbsent(
tag,
t -> new ArrayList<>())
.add(terra));
},
() -> logger.error(
"No such biome: {}",
tb))),
() -> logger.error("No vanilla biome: {}", vb)));
registry.clearTags();
registry.populateTags(ImmutableMap.copyOf(collect));
if(logger.isDebugEnabled()) {
registry.streamEntries()
.map(e -> e.registryKey().getValue() + ": " +
e.streamTags().reduce("", (s, t) -> t.id() + ", " + s, String::concat))
.forEach(logger::debug);
}
}
}

View File

@ -1,9 +1,10 @@
package com.dfsek.terra.forge.mixin.lifecycle;
import com.dfsek.terra.forge.util.BiomeUtil;
import com.dfsek.terra.forge.util.TagUtil;
import com.dfsek.terra.mod.util.MinecraftUtil;
import com.dfsek.terra.mod.util.TagUtil;
import net.minecraft.server.DataPackContents;
import net.minecraft.util.registry.DynamicRegistryManager;
import net.minecraft.util.registry.Registry;

View File

@ -1,8 +1,4 @@
package com.dfsek.terra.forge.util;
import com.dfsek.terra.mod.util.MinecraftUtil;
import com.dfsek.terra.mod.util.PresetUtil;
package com.dfsek.terra.mod.util;
import com.google.common.collect.ImmutableMap;
import net.minecraft.tag.TagKey;