Skip to content

Commit

Permalink
The definition of AwfulBukkitHacks
Browse files Browse the repository at this point in the history
  • Loading branch information
OakLoaf committed Nov 1, 2024
1 parent a87ad8c commit d454964
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 <T> void bindTags(MappedRegistry<T> registry, Map<TagKey<T>, List<Holder<T>>> tagEntries) {
Map<Holder.Reference<T>, List<TagKey<T>>> map = new IdentityHashMap<>();
Reflection.MAPPED_REGISTRY.getByKey(registry).values().forEach(entry -> map.put(entry, new ArrayList<>()));
tagEntries.forEach((tag, entries) -> {
for (Holder<T> 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<T> reference)) {
throw new IllegalStateException("Found direct holder " + holder + " value in tag " + tag);
}

map.get(reference).add(tag);
}
});
// Set<TagKey<T>> 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<TagKey<T>, HolderSet.Named<T>> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -55,8 +58,21 @@ public interface MappedRegistryProxy {
@FieldGetter("byKey")
<T> Map<ResourceKey<T>, Reference<T>> getByKey(MappedRegistry<T> instance);

@FieldSetter("allTags")
<T> void setAllTags(MappedRegistry<T> instance, Object obj);

@FieldSetter("frozen")
void setFrozen(MappedRegistry<?> instance, boolean frozen);

@MethodName("createTag")
<T> HolderSet.Named<T> invokeCreateTag(MappedRegistry<T> instance, TagKey<T> tag);
}

@Proxies(className = "net.minecraft.core.MappedRegistry$TagSet")
public interface MappedRegistryTagSetProxy {
@MethodName("fromMap")
@Static
<T> Object invokeFromMap(Map<TagKey<T>, HolderSet.Named<T>> map);
}


Expand All @@ -71,6 +87,9 @@ public interface StructureManagerProxy {
public interface ReferenceProxy {
@MethodName("bindValue")
<T> void invokeBindValue(Reference<T> instance, T value);

@MethodName("bindTags")
<T> void invokeBindTags(Reference<T> instance, Collection<TagKey<T>> tags);
}

@Proxies(ChunkMap.class)
Expand Down

0 comments on commit d454964

Please # to comment.