Skip to content

Commit

Permalink
update for 24w18a
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed May 8, 2024
1 parent a658cf3 commit 1ab1e9c
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 95 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/github/nickid2018/genwiki/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class Constants {
public static final String INJECT_SERVER_PROPERTIES = "net.minecraft.server.dedicated.DedicatedServerProperties";
public static final String INJECT_SERVER_PROPERTIES_METHOD = "getDatapackConfig";
public static final String INJECT_SERVER_PROPERTIES_METHOD_DESC = "(Ljava/lang/String;Ljava/lang/String;)Lnet/minecraft/world/level/DataPackConfig;";
public static final String INJECT_CHUNK_STATISTICS_METHOD = "tickChildren";
public static final String INJECT_CHUNK_STATISTICS_METHOD_DESC = "(Ljava/util/function/BooleanSupplier;)V";
public static final String INJECT_METHOD = "tickChildren";
public static final String INJECT_METHOD_DESC = "(Ljava/util/function/BooleanSupplier;)V";
public static final String INJECT_REGION_FILE = "net.minecraft.world.level.chunk.storage.RegionFile";
public static final String INJECT_REGION_FILE_METHOD = "write";
public static final String INJECT_REGION_FILE_METHOD_DESC = "(Lnet/minecraft/world/level/ChunkPos;Ljava/nio/ByteBuffer;)V";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void extractBiomeData(Object serverObj) {

for (Object level : levels) {
@SourceClass("Registry<Biome>")
Object biomeRegistry = InjectedProcess.getSyncRegistry(level, "BIOME");
Object biomeRegistry = InjectedProcess.getLevelSyncRegistry(level, "BIOME");
@SourceClass("Set<ResourceKey<Biome>>")
Set<?> biomeKeySet = InjectedProcess.getRegistryKeySet(biomeRegistry);
for (@SourceClass("ResourceKey<Biome>") Object biomeKey : biomeKeySet) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.github.nickid2018.genwiki.autovalue;

import io.github.nickid2018.genwiki.autovalue.wikidata.*;
import io.github.nickid2018.genwiki.autovalue.wikidata.NumberWikiData;
import io.github.nickid2018.genwiki.autovalue.wikidata.PairMapWikiData;
import io.github.nickid2018.genwiki.autovalue.wikidata.StringListWikiData;
import io.github.nickid2018.genwiki.autovalue.wikidata.WikiData;
import io.github.nickid2018.genwiki.inject.InjectedProcess;
import io.github.nickid2018.genwiki.inject.SourceClass;
import lombok.SneakyThrows;
Expand All @@ -22,13 +25,10 @@ public class EnchantmentDataExtractor {

public static final MethodHandle ENCHANTMENT_GET_WEIGHT;
public static final MethodHandle ENCHANTMENT_GET_MAX_LEVEL;
public static final MethodHandle ENCHANTMENT_IS_TREASURE_ONLY;
public static final MethodHandle ENCHANTMENT_IS_TRADEABLE;
public static final MethodHandle ENCHANTMENT_IS_DISCOVERABLE;
public static final MethodHandle ENCHANTMENT_IS_COMPATIBLE_WITH;
public static final MethodHandle ENCHANTMENT_ARE_COMPATIBLE;
public static final MethodHandle ENCHANTMENT_GET_MIN_COST;
public static final MethodHandle ENCHANTMENT_GET_MAX_COST;
public static final MethodHandle ENCHANTMENT_CAN_ENCHANT;
public static final MethodHandle ENCHANTMENT_IS_SUPPORTED_ITEM;
public static final MethodHandle ENCHANTMENT_IS_PRIMARY_ITEM;
public static final MethodHandle ITEM_GET_DEFAULT_INSTANCE;
public static final MethodHandle ITEM_STACK_IS;
Expand All @@ -43,15 +43,22 @@ public class EnchantmentDataExtractor {

ENCHANTMENT_GET_WEIGHT = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("getWeight"));
ENCHANTMENT_GET_MAX_LEVEL = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("getMaxLevel"));
ENCHANTMENT_IS_TREASURE_ONLY = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("isTreasureOnly"));
ENCHANTMENT_IS_TRADEABLE = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("isTradeable"));
ENCHANTMENT_IS_DISCOVERABLE = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("isDiscoverable"));
ENCHANTMENT_IS_COMPATIBLE_WITH = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("isCompatibleWith", ENCHANTMENT_CLASS));
ENCHANTMENT_ARE_COMPATIBLE = lookup.unreflect(ENCHANTMENT_CLASS.getMethod(
"areCompatible",
InjectedProcess.HOLDER_CLASS,
InjectedProcess.HOLDER_CLASS
));

ENCHANTMENT_GET_MIN_COST = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("getMinCost", int.class));
ENCHANTMENT_GET_MAX_COST = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("getMaxCost", int.class));
ENCHANTMENT_CAN_ENCHANT = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("canEnchant", ITEM_STACK_CLASS));
ENCHANTMENT_IS_PRIMARY_ITEM = lookup.unreflect(ENCHANTMENT_CLASS.getMethod("isPrimaryItem", ITEM_STACK_CLASS));
ENCHANTMENT_IS_SUPPORTED_ITEM = lookup.unreflect(ENCHANTMENT_CLASS.getMethod(
"isSupportedItem",
ITEM_STACK_CLASS
));
ENCHANTMENT_IS_PRIMARY_ITEM = lookup.unreflect(ENCHANTMENT_CLASS.getMethod(
"isPrimaryItem",
ITEM_STACK_CLASS
));

ITEM_GET_DEFAULT_INSTANCE = lookup.unreflect(ITEM_CLASS.getMethod("getDefaultInstance"));
ITEM_STACK_IS = lookup.unreflect(ITEM_STACK_CLASS.getMethod("is", TAG_KEY_CLASS));
Expand All @@ -62,7 +69,6 @@ public class EnchantmentDataExtractor {

public static final NumberWikiData ENCHANTMENT_WEIGHT = new NumberWikiData();
public static final NumberWikiData ENCHANTMENT_MAX_LEVEL = new NumberWikiData();
public static final StringWikiData ENCHANTMENT_FLAG = new StringWikiData();
public static final StringListWikiData ENCHANTMENT_INCOMPATIBLE = new StringListWikiData();
public static final PairMapWikiData<Integer, Integer> ENCHANTMENT_COST = new PairMapWikiData<>();
public static final StringListWikiData ENCHANTMENT_SUPPORT_ITEMS = new StringListWikiData();
Expand All @@ -74,19 +80,19 @@ private static String getRegistryName(Object enchantment) {
}

@SneakyThrows
private static Object getEnchantmentObject(Object enchantment) {
return InjectedProcess.REGISTRY_GET.invoke(InjectedProcess.getRegistry("ENCHANTMENT"), enchantment);
private static Object getEnchantmentObject(Object registry, Object enchantment) {
return InjectedProcess.REGISTRY_GET.invoke(registry, enchantment);
}

@SneakyThrows
public static void extractEnchantmentData() {
public static void extractEnchantmentData(Object server) {
@SourceClass("Registry<Enchantment>")
Object enchantmentRegistry = InjectedProcess.getRegistry("ENCHANTMENT");
Object enchantmentRegistry = InjectedProcess.getServerSyncRegistry(server, "ENCHANTMENT");
@SourceClass("Set<ResourceKey<Enchantment>>")
Set<?> enchantmentKeySet = InjectedProcess.getRegistryKeySet(enchantmentRegistry);
Map<String, ?> enchantmentMap = enchantmentKeySet.stream().collect(Collectors.toMap(
EnchantmentDataExtractor::getRegistryName,
EnchantmentDataExtractor::getEnchantmentObject
EnchantmentDataExtractor::getRegistryName,
enchantment -> getEnchantmentObject(enchantmentRegistry, enchantment)
));

for (Map.Entry<String, ?> entry : enchantmentMap.entrySet()) {
Expand All @@ -97,31 +103,22 @@ public static void extractEnchantmentData() {
int maxLevel = (int) ENCHANTMENT_GET_MAX_LEVEL.invoke(enchantment);
ENCHANTMENT_MAX_LEVEL.put(name, maxLevel);

boolean treasureOnly = (boolean) ENCHANTMENT_IS_TREASURE_ONLY.invoke(enchantment);
boolean tradeable = (boolean) ENCHANTMENT_IS_TRADEABLE.invoke(enchantment);
boolean discoverable = (boolean) ENCHANTMENT_IS_DISCOVERABLE.invoke(enchantment);
List<String> flagSet = new ArrayList<>();
if (treasureOnly)
flagSet.add("TREASURE");
if (!tradeable)
flagSet.add("UNTRADEABLE");
if (!discoverable)
flagSet.add("UNDISCOVERABLE");
String flag = flagSet.isEmpty() ? "ALL_CONDITIONS" : String.join("_", flagSet);
ENCHANTMENT_FLAG.put(name, flag);

List<String> incompatibles = enchantmentMap.entrySet().stream()
.filter(e -> e.getValue() != enchantment)
.filter(e -> {
Object other = e.getValue();
try {
return !(boolean) ENCHANTMENT_IS_COMPATIBLE_WITH.invoke(enchantment, other);
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
})
.map(Map.Entry::getKey).sorted()
.collect(Collectors.toList());
List<String> incompatibles = enchantmentMap
.entrySet().stream()
.filter(e -> e.getValue() != enchantment)
.filter(e -> {
Object other = e.getValue();
try {
return !(boolean) ENCHANTMENT_ARE_COMPATIBLE.invoke(
InjectedProcess.wrapAsHolder(enchantmentRegistry, enchantment),
InjectedProcess.wrapAsHolder(enchantmentRegistry, other)
);
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
})
.map(Map.Entry::getKey).sorted()
.collect(Collectors.toList());
ENCHANTMENT_INCOMPATIBLE.put(name, incompatibles);

for (int i = 1; i <= maxLevel; i++) {
Expand All @@ -146,7 +143,7 @@ public static void extractEnchantmentData() {
for (Map.Entry<String, ?> enchantmentEntry : enchantmentMap.entrySet()) {
Object enchantment = enchantmentEntry.getValue();
String name = enchantmentEntry.getKey();
boolean support = (boolean) ENCHANTMENT_CAN_ENCHANT.invoke(enchantment, itemStack);
boolean support = (boolean) ENCHANTMENT_IS_SUPPORTED_ITEM.invoke(enchantment, itemStack);
if (support) {
ENCHANTMENT_SUPPORT_ITEMS.putNew(name, itemID);
boolean isPrimary = (boolean) ENCHANTMENT_IS_PRIMARY_ITEM.invoke(enchantment, itemStack);
Expand All @@ -167,7 +164,6 @@ public static void extractEnchantmentData() {

WikiData.write(ENCHANTMENT_WEIGHT, "enchantment_weight.txt");
WikiData.write(ENCHANTMENT_MAX_LEVEL, "enchantment_max_level.txt");
WikiData.write(ENCHANTMENT_FLAG, "enchantment_flag.txt");
WikiData.write(ENCHANTMENT_INCOMPATIBLE, "enchantment_incompatible.txt");
WikiData.write(ENCHANTMENT_COST, "enchantment_cost.txt");
WikiData.write(ENCHANTMENT_SUPPORT_ITEMS, "enchantment_support_items.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static void extractItemData(Object serverObj) {

Object serverOverworld = InjectedProcess.SERVER_OVERWORLD.invoke(serverObj);
@SourceClass("RegistryAccess")
Object registryAccess = InjectedProcess.REGISTRY_ACCESS.invoke(serverOverworld);
Object registryAccess = InjectedProcess.LEVEL_REGISTRY_ACCESS.invoke(serverOverworld);
BUILD_TAB_CONTENTS.invoke(InjectedProcess.featureFlagSet, true, registryAccess);

@SourceClass("Registry<CreativeModeTab>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ public class InjectedProcess {
public static final MethodHandle GET_ALL_LEVELS;

public static final MethodHandle SERVER_OVERWORLD;
public static final MethodHandle REGISTRY_ACCESS;
public static final MethodHandle SERVER_REGISTRY_ACCESS;
public static final MethodHandle LEVEL_REGISTRY_ACCESS;
public static final MethodHandle HOLDER_VALUE;
public static final MethodHandle HOLDER_UNWRAP_KEY;
public static final MethodHandle EITHER_LEFT;
public static final MethodHandle GET_HOLDER;
public static final MethodHandle WRAP_AS_HODLER;

public static final Path NULL_PATH;
public static final Object BLOCK_POS_ZERO;
Expand All @@ -81,12 +83,14 @@ public class InjectedProcess {
SERVER_OVERWORLD = lookup.unreflect(MINECRAFT_SERVER_CLASS.getMethod("overworld"));
GET_ALL_LEVELS = lookup.unreflect(MINECRAFT_SERVER_CLASS.getMethod("getAllLevels"));
Class<?> levelClass = Class.forName("net.minecraft.world.level.Level");
REGISTRY_ACCESS = lookup.unreflect(levelClass.getMethod("registryAccess"));
SERVER_REGISTRY_ACCESS = lookup.unreflect(MINECRAFT_SERVER_CLASS.getMethod("registryAccess"));
LEVEL_REGISTRY_ACCESS = lookup.unreflect(levelClass.getMethod("registryAccess"));

HOLDER_CLASS = Class.forName("net.minecraft.core.Holder");
HOLDER_VALUE = lookup.unreflect(HOLDER_CLASS.getMethod("value"));
HOLDER_UNWRAP_KEY = lookup.unreflect(HOLDER_CLASS.getMethod("unwrapKey"));
GET_HOLDER = lookup.unreflect(registryClass.getMethod("getHolder", RESOURCE_KEY_CLASS));
WRAP_AS_HODLER = lookup.unreflect(registryClass.getMethod("wrapAsHolder", Object.class));

EITHER_CLASS = Class.forName("com.mojang.datafixers.util.Either");
EITHER_LEFT = lookup.unreflect(EITHER_CLASS.getMethod("left"));
Expand Down Expand Up @@ -152,29 +156,46 @@ public static Object getRegistry(String name) {
}

@SneakyThrows
public static Object getSyncRegistry(Object level, String name) {
return REGISTRY_OR_THROW.invoke(REGISTRY_ACCESS.invoke(level), REGISTRY_KEY.get(name));
public static Object getLevelSyncRegistry(Object level, String name) {
return REGISTRY_OR_THROW.invoke(LEVEL_REGISTRY_ACCESS.invoke(level), REGISTRY_KEY.get(name));
}

@SneakyThrows
public static Object getServerSyncRegistry(Object server, String name) {
return REGISTRY_OR_THROW.invoke(SERVER_REGISTRY_ACCESS.invoke(server), REGISTRY_KEY.get(name));
}

@SneakyThrows
public static String holderToString(Object holder) {
return getResourceLocationPath(RESOURCE_KEY_LOCATION.invoke(((Optional<?>) HOLDER_UNWRAP_KEY.invoke(holder)).get()));
}

@SneakyThrows
public static Object getHolder(Object registry, Object resourceKey) {
return ((Optional<?>) GET_HOLDER.invoke(registry, resourceKey)).get();
}

@SneakyThrows
public static Object wrapAsHolder(Object registry, Object obj) {
return WRAP_AS_HODLER.invoke(registry, obj);
}

@SuppressWarnings("unused")
@SneakyThrows
public static String preprocessDataPacks() {
log.info("Writing data packs...");

Object featureFlagRegistry = Class.forName("net.minecraft.world.flag.FeatureFlags").getField("REGISTRY").get(null);
Object featureFlagRegistry = Class.forName("net.minecraft.world.flag.FeatureFlags").getField("REGISTRY").get(
null);
Class<?> featureFlagRegistryClass = Class.forName("net.minecraft.world.flag.FeatureFlagRegistry");
Field names = featureFlagRegistryClass.getDeclaredField("names");
Method fromNames = featureFlagRegistryClass.getDeclaredMethod("fromNames", Iterable.class);
names.setAccessible(true);
Map<?, ?> namesObj = (Map<?, ?>) names.get(featureFlagRegistry);
Set<?> namesIterable = namesObj.keySet();

String ret = namesObj.keySet().stream().map(RESOURCE_LOCATION_PATH::invoke).map(Object::toString).collect(Collectors.joining(","));
String ret = namesObj.keySet().stream().map(RESOURCE_LOCATION_PATH::invoke).map(Object::toString).collect(
Collectors.joining(","));
featureFlagSet = fromNames.invoke(featureFlagRegistry, namesIterable);

return ret;
Expand All @@ -193,7 +214,7 @@ public static void extractDataInjection(Object server) {
ItemDataExtractor.extractItemData(server);
EntityDataExtractor.extractEntityData(server);
BiomeDataExtractor.extractBiomeData(server);
EnchantmentDataExtractor.extractEnchantmentData();
EnchantmentDataExtractor.extractEnchantmentData(server);

throw new RuntimeException("Program exited, wiki data has been written.");
}
Expand Down
Loading

0 comments on commit 1ab1e9c

Please sign in to comment.