From d4549643fcdbeefbe5e149c474e410d127f7877e Mon Sep 17 00:00:00 2001 From: OakLoaf Date: Fri, 1 Nov 2024 16:46:33 +0000 Subject: [PATCH] The definition of AwfulBukkitHacks --- .../bukkit/nms/v1_21_3/AwfulBukkitHacks.java | 42 ++++++++++++++++++- .../terra/bukkit/nms/v1_21_3/Reflection.java | 19 +++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/AwfulBukkitHacks.java b/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/AwfulBukkitHacks.java index 676c404eb..57a992670 100644 --- a/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/AwfulBukkitHacks.java +++ b/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/AwfulBukkitHacks.java @@ -2,9 +2,10 @@ import com.dfsek.terra.bukkit.nms.v1_21_3.config.VanillaBiomeProperties; -import com.google.common.collect.ImmutableMap; import net.minecraft.core.Holder; import net.minecraft.core.Holder.Reference; +import net.minecraft.core.HolderSet; +import net.minecraft.core.HolderSet.Named; import net.minecraft.core.MappedRegistry; import net.minecraft.core.RegistrationInfo; import net.minecraft.core.registries.Registries; @@ -18,9 +19,11 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import com.dfsek.terra.bukkit.world.BukkitPlatformBiome; import com.dfsek.terra.registry.master.ConfigRegistry; @@ -98,13 +101,48 @@ public static void registerBiomes(ConfigRegistry configRegistry) { () -> LOGGER.error("No vanilla biome: {}", vb))); resetTags(biomeRegistry); - ImmutableMap.copyOf(collect).forEach(biomeRegistry::bindTag); + bindTags(biomeRegistry, collect); +// ImmutableMap.copyOf(collect).forEach(biomeRegistry::bindTag); } catch(SecurityException | IllegalArgumentException exception) { throw new RuntimeException(exception); } } + private static void bindTags(MappedRegistry registry, Map, List>> tagEntries) { + Map, List>> map = new IdentityHashMap<>(); + Reflection.MAPPED_REGISTRY.getByKey(registry).values().forEach(entry -> map.put(entry, new ArrayList<>())); + tagEntries.forEach((tag, entries) -> { + for (Holder holder : entries) { +// if (!holder.canSerializeIn(registry.asLookup())) { +// throw new IllegalStateException("Can't create named set " + tag + " containing value " + holder + " from outside registry " + this); +// } + + if (!(holder instanceof Holder.Reference reference)) { + throw new IllegalStateException("Found direct holder " + holder + " value in tag " + tag); + } + + map.get(reference).add(tag); + } + }); +// Set> set = Sets.difference(registry.tags.keySet(), tagEntries.keySet()); +// if (!set.isEmpty()) { +// LOGGER.warn( +// "Not all defined tags for registry {} are present in data pack: {}", +// registry.key(), +// set.stream().map(tag -> tag.location().toString()).sorted().collect(Collectors.joining(", ")) +// ); +// } + + Map, HolderSet.Named> map2 = new IdentityHashMap<>(registry.getTags().collect(Collectors.toMap( + Named::key, + (named) -> named + ))); + tagEntries.forEach((tag, entries) -> Reflection.HOLDER_SET.invokeBind(map2.computeIfAbsent(tag, key -> Reflection.MAPPED_REGISTRY.invokeCreateTag(registry, key)), entries)); + map.forEach(Reflection.HOLDER_REFERENCE::invokeBindTags); + Reflection.MAPPED_REGISTRY.setAllTags(registry, Reflection.MAPPED_REGISTRY_TAG_SET.invokeFromMap(map2)); + } + private static void resetTags(MappedRegistry registry) { registry.getTags().forEach(entryList -> Reflection.HOLDER_SET.invokeBind(entryList, List.of())); Reflection.MAPPED_REGISTRY.getByKey(registry).values().forEach(entry -> Reflection.HOLDER_REFERENCE.invokeBindTags(entry, Set.of())); diff --git a/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/Reflection.java b/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/Reflection.java index 044fb479f..8be29690c 100644 --- a/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/Reflection.java +++ b/platforms/bukkit/nms/v1_21_3/src/main/java/com/dfsek/terra/bukkit/nms/v1_21_3/Reflection.java @@ -17,6 +17,7 @@ import xyz.jpenilla.reflectionremapper.proxy.annotation.FieldSetter; import xyz.jpenilla.reflectionremapper.proxy.annotation.MethodName; import xyz.jpenilla.reflectionremapper.proxy.annotation.Proxies; +import xyz.jpenilla.reflectionremapper.proxy.annotation.Static; import java.util.Collection; import java.util.List; @@ -25,6 +26,7 @@ public class Reflection { public static final MappedRegistryProxy MAPPED_REGISTRY; + public static final MappedRegistryTagSetProxy MAPPED_REGISTRY_TAG_SET; public static final StructureManagerProxy STRUCTURE_MANAGER; public static final ReferenceProxy REFERENCE; @@ -41,6 +43,7 @@ public class Reflection { Reflection.class.getClassLoader()); MAPPED_REGISTRY = reflectionProxyFactory.reflectionProxy(MappedRegistryProxy.class); + MAPPED_REGISTRY_TAG_SET = reflectionProxyFactory.reflectionProxy(MappedRegistryTagSetProxy.class); STRUCTURE_MANAGER = reflectionProxyFactory.reflectionProxy(StructureManagerProxy.class); REFERENCE = reflectionProxyFactory.reflectionProxy(ReferenceProxy.class); CHUNKMAP = reflectionProxyFactory.reflectionProxy(ChunkMapProxy.class); @@ -55,8 +58,21 @@ public interface MappedRegistryProxy { @FieldGetter("byKey") Map, Reference> getByKey(MappedRegistry instance); + @FieldSetter("allTags") + void setAllTags(MappedRegistry instance, Object obj); + @FieldSetter("frozen") void setFrozen(MappedRegistry instance, boolean frozen); + + @MethodName("createTag") + HolderSet.Named invokeCreateTag(MappedRegistry instance, TagKey tag); + } + + @Proxies(className = "net.minecraft.core.MappedRegistry$TagSet") + public interface MappedRegistryTagSetProxy { + @MethodName("fromMap") + @Static + Object invokeFromMap(Map, HolderSet.Named> map); } @@ -71,6 +87,9 @@ public interface StructureManagerProxy { public interface ReferenceProxy { @MethodName("bindValue") void invokeBindValue(Reference instance, T value); + + @MethodName("bindTags") + void invokeBindTags(Reference instance, Collection> tags); } @Proxies(ChunkMap.class)