From 85452ed74e5c7f2de632cdcc6a0502f8f69955db Mon Sep 17 00:00:00 2001 From: Leo-768 Date: Thu, 25 Apr 2024 21:49:59 +0800 Subject: [PATCH 1/3] Add Potion Effect --- .../autovalue/EntityDataExtractor.java | 65 +++++++++++++++++++ .../wikidata/AttributeModifiersWikiData.java | 23 ++++--- .../wikidata/PotionEffectWikiData.java | 56 ++++++++++++++++ 3 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/PotionEffectWikiData.java diff --git a/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java b/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java index e34e6d6..5235fba 100644 --- a/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java +++ b/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java @@ -3,12 +3,14 @@ import io.github.nickid2018.genwiki.autovalue.wikidata.*; import io.github.nickid2018.genwiki.inject.InjectedProcess; import io.github.nickid2018.genwiki.inject.SourceClass; +import io.github.nickid2018.genwiki.util.LanguageUtils; import lombok.SneakyThrows; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.util.*; +import java.util.function.BiConsumer; public class EntityDataExtractor { @@ -32,7 +34,16 @@ public class EntityDataExtractor { public static final MethodHandle MOB_EFFECT_GET_CATEGORY; public static final MethodHandle MOB_EFFECT_GET_COLOR; public static final MethodHandle MOB_EFFECT_IS_INSTANTENOUS; + public static final MethodHandle MOB_EFFECT_CREATE_MODIFIERS; public static final MethodHandle MOB_EFFECT_INSTANCE_CTOR; + public static final MethodHandle MOB_EFFECT_INSTANCE_GET_EFFECT; + public static final MethodHandle MOB_EFFECT_INSTANCE_GET_DURATION; + public static final MethodHandle MOB_EFFECT_INSTANCE_GET_AMPLIFER; + public static final MethodHandle POTION_GET_NAME; + public static final MethodHandle POTION_GET_EFFECTS; + private static final MethodHandle ATTRIBUTE_MODIFIER_GET_AMOUNT; + private static final MethodHandle ATTRIBUTE_MODIFIER_GET_OPERATION; + public static final MethodHandle STRING_REPRESENTABLE_GET_SERIALIZED_NAME; static { @@ -56,8 +67,23 @@ public class EntityDataExtractor { MOB_EFFECT_GET_CATEGORY = lookup.unreflect(MOB_EFFECT_CLASS.getDeclaredMethod("getCategory")); MOB_EFFECT_GET_COLOR = lookup.unreflect(MOB_EFFECT_CLASS.getDeclaredMethod("getColor")); MOB_EFFECT_IS_INSTANTENOUS = lookup.unreflect(MOB_EFFECT_CLASS.getDeclaredMethod("isInstantenous")); + MOB_EFFECT_CREATE_MODIFIERS = lookup.unreflect(MOB_EFFECT_CLASS.getMethod("createModifiers", int.class, BiConsumer.class)); LIVING_ENTITY_ADD_EFFECT = lookup.unreflect(LIVING_ENTITY_CLASS.getMethod("addEffect", MOB_EFFECT_INSTANCE_CLASS)); MOB_EFFECT_INSTANCE_CTOR = lookup.findConstructor(MOB_EFFECT_INSTANCE_CLASS, MethodType.methodType(void.class, InjectedProcess.HOLDER_CLASS)); + MOB_EFFECT_INSTANCE_GET_EFFECT = lookup.unreflect(MOB_EFFECT_INSTANCE_CLASS.getMethod("getEffect")); + MOB_EFFECT_INSTANCE_GET_DURATION = lookup.unreflect(MOB_EFFECT_INSTANCE_CLASS.getMethod("getDuration")); + MOB_EFFECT_INSTANCE_GET_AMPLIFER = lookup.unreflect(MOB_EFFECT_INSTANCE_CLASS.getMethod("getAmplifier")); + + Class potionClass = Class.forName("net.minecraft.world.item.alchemy.Potion"); + POTION_GET_NAME = lookup.unreflect(potionClass.getMethod("getName", Optional.class, String.class)); + POTION_GET_EFFECTS = lookup.unreflect(potionClass.getMethod("getEffects")); + + Class attributeModifierClass = Class.forName("net.minecraft.world.entity.ai.attributes.AttributeModifier"); + ATTRIBUTE_MODIFIER_GET_AMOUNT = lookup.unreflect(attributeModifierClass.getMethod("amount")); + ATTRIBUTE_MODIFIER_GET_OPERATION = lookup.unreflect(attributeModifierClass.getMethod("operation")); + + STRING_REPRESENTABLE_GET_SERIALIZED_NAME = lookup.unreflect( + Class.forName("net.minecraft.util.StringRepresentable").getMethod("getSerializedName")); } catch (Exception e) { throw new RuntimeException(e); } @@ -70,6 +96,8 @@ public class EntityDataExtractor { public static final BooleanWikiData EFFECT_INSTANTENOUS = new BooleanWikiData(); public static final StringListWikiData EFFECT_CANNOT_AFFECT = new StringListWikiData(); + public static final PotionEffectWikiData POTION_EFFECT = new PotionEffectWikiData(); + @SneakyThrows public static void extractEntityData(Object serverObj) { @SourceClass("DefaultedRegistry>") @@ -130,10 +158,47 @@ public static void extractEntityData(Object serverObj) { EFFECT_CANNOT_AFFECT.get(effectID).sort(Comparator.naturalOrder()); } + @SourceClass("Registry") + Object potionEffectRegistry = InjectedProcess.getRegistry("POTION"); + @SourceClass("Set>") + Set potionEffectKeySet = InjectedProcess.getRegistryKeySet(potionEffectRegistry); + for (Object potionEffectKey : potionEffectKeySet) { + @SourceClass("ResourceLocation") + Object potionEffectLocation = InjectedProcess.RESOURCE_KEY_LOCATION.invoke(potionEffectKey); + String potionEffectID = InjectedProcess.getResourceLocationPath(potionEffectLocation); + @SourceClass("Potion") + Object potionEffect = InjectedProcess.REGISTRY_GET.invoke(potionEffectRegistry, potionEffectKey); + + POTION_EFFECT.put(potionEffectID, (String) POTION_GET_NAME.invoke((Optional) InjectedProcess.GET_HOLDER.invoke(potionEffectRegistry, potionEffectKey), "")); + @SourceClass("List") + List effectInstances = (List) POTION_GET_EFFECTS.invoke(potionEffect); + for (Object effectInstance : effectInstances) { + @SourceClass("Holder") + Object effect = MOB_EFFECT_INSTANCE_GET_EFFECT.invoke(effectInstance); + int amplifier = (int) MOB_EFFECT_INSTANCE_GET_AMPLIFER.invoke(effectInstance); + POTION_EFFECT.addEffect( + potionEffectID, + InjectedProcess.holderToString(effect), + amplifier, + (int) MOB_EFFECT_INSTANCE_GET_DURATION.invoke(effectInstance) + ); + MOB_EFFECT_CREATE_MODIFIERS.invoke(InjectedProcess.HOLDER_VALUE.invoke(effect), amplifier, LanguageUtils.sneakyExceptionBiConsumer((attribute, attributeModifier) -> { + String attributeID = InjectedProcess.holderToString(attribute); + POTION_EFFECT.addAttributeModifier( + potionEffectID, + attributeID, + (Double) ATTRIBUTE_MODIFIER_GET_AMOUNT.invoke(attributeModifier), + (String) STRING_REPRESENTABLE_GET_SERIALIZED_NAME.invoke(ATTRIBUTE_MODIFIER_GET_OPERATION.invoke(attributeModifier)) + ); + })); + } + } + WikiData.write(MOB_CATEGORY, "entity_mob_category.txt"); WikiData.write(EFFECT_CATEGORY, "mob_effect_category.txt"); WikiData.write(EFFECT_COLOR, "mob_effect_color.txt"); WikiData.write(EFFECT_INSTANTENOUS, "mob_effect_instantenous.txt"); WikiData.write(EFFECT_CANNOT_AFFECT, "mob_effect_cannot_affect.txt"); + WikiData.write(POTION_EFFECT, "potion_effect.txt"); } } diff --git a/src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/AttributeModifiersWikiData.java b/src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/AttributeModifiersWikiData.java index a56ca44..5c44702 100644 --- a/src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/AttributeModifiersWikiData.java +++ b/src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/AttributeModifiersWikiData.java @@ -35,13 +35,7 @@ public String output(int indent) { for (Map.Entry> slotTypeEntry : itemEntry.getValue().entrySet()) { String category = slotTypeEntry.getKey(); builder.append(tab).append("\t['").append(category).append("'] = {\n"); - for (AttributeModifier entry : slotTypeEntry.getValue()) { - builder.append(tab).append("\t\t{\n"); - builder.append(tab).append("\t\t\t['attribute'] = '").append(entry.attribute).append("',\n"); - builder.append(tab).append("\t\t\t['amount'] = ").append(formatValue(entry.amount)).append(",\n"); - builder.append(tab).append("\t\t\t['operation'] = '").append(entry.operation).append("',\n"); - builder.append(tab).append("\t\t},\n"); - } + builder.append(printAttributeModifiers(indent + 2, slotTypeEntry.getValue())); builder.append(tab).append("\t},\n"); } builder.append(tab).append("},\n"); @@ -49,6 +43,19 @@ public String output(int indent) { return builder.toString(); } - private record AttributeModifier(String attribute, double amount, String operation) { + public static String printAttributeModifiers(int indent, Iterable attributeModifiers) { + StringBuilder builder = new StringBuilder(); + String tab = "\t".repeat(indent); + for (AttributeModifier entry : attributeModifiers) { + builder.append(tab).append("{\n"); + builder.append(tab).append("\t['attribute'] = '").append(entry.attribute).append("',\n"); + builder.append(tab).append("\t['amount'] = ").append(formatValue(entry.amount)).append(",\n"); + builder.append(tab).append("\t['operation'] = '").append(entry.operation).append("',\n"); + builder.append(tab).append("},\n"); + } + return builder.toString(); + } + + public record AttributeModifier(String attribute, double amount, String operation) { } } diff --git a/src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/PotionEffectWikiData.java b/src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/PotionEffectWikiData.java new file mode 100644 index 0000000..271daf4 --- /dev/null +++ b/src/main/java/io/github/nickid2018/genwiki/autovalue/wikidata/PotionEffectWikiData.java @@ -0,0 +1,56 @@ +package io.github.nickid2018.genwiki.autovalue.wikidata; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class PotionEffectWikiData implements WikiData { + private final Map data = new TreeMap<>(); + + public void put(String id, String name) { + data.put(id, new PotionEffect(name, new ArrayList<>(), new ArrayList<>())); + } + + public void addEffect(String potionEffect, String id, int amplifier, int duration) { + data.get(potionEffect).effects.add(new EffectInstance(id, amplifier, duration)); + } + + public void addAttributeModifier(String potionEffect, String attribute, double amount, String operation) { + data.get(potionEffect).attributeModifiers.add(new AttributeModifiersWikiData.AttributeModifier(attribute, amount, operation)); + } + + @Override + public String output(int indent) { + StringBuilder builder = new StringBuilder(); + String tab = "\t".repeat(indent); + for (Map.Entry potionEntry : data.entrySet()) { + String id = potionEntry.getKey(); + PotionEffect potion = potionEntry.getValue(); + builder.append(tab).append("['").append(id).append("'] = {\n"); + builder.append(tab).append("\t['name'] = '").append(potion.name).append("',\n"); + if (!potion.effects.isEmpty()) { + builder.append(tab).append("\t['effects'] = {\n"); + for (EffectInstance effect : potion.effects) { + builder.append(tab).append("\t\t{\n"); + builder.append(tab).append("\t\t\t['id'] = '").append(effect.id).append("',\n"); + builder.append(tab).append("\t\t\t['amplifier'] = ").append(NumberWikiData.formatValue(effect.amplifier)).append(",\n"); + builder.append(tab).append("\t\t\t['duration'] = ").append(NumberWikiData.formatValue(effect.duration)).append(",\n"); + builder.append(tab).append("\t\t},\n"); + } + builder.append(tab).append("\t},\n"); + } + if (!potion.attributeModifiers.isEmpty()) { + builder.append(tab).append("\t['attributes'] = {\n"); + builder.append(AttributeModifiersWikiData.printAttributeModifiers(indent + 2, potion.attributeModifiers)); + builder.append(tab).append("\t},\n"); + } + builder.append(tab).append("},\n"); + } + return builder.toString(); + } + + + private record EffectInstance(String id, int amplifier, int duration) {} + private record PotionEffect(String name, List effects, List attributeModifiers) {} +} From c416ff9daefda1d98f06ec5b0c5cdf62f39be82c Mon Sep 17 00:00:00 2001 From: Leo-768 Date: Thu, 25 Apr 2024 22:00:26 +0800 Subject: [PATCH 2/3] Update README.MD --- README.MD | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.MD b/README.MD index e7da41c..0cd453e 100644 --- a/README.MD +++ b/README.MD @@ -57,6 +57,8 @@ * `mob_effect_category`:状态效果类别。 * `mob_effect_color`:状态效果颜色。 * `mob_effect_instantenous`:瞬时状态效果。 +- 其他 + * `postion effect`:药水效果。 ### 生成数据 From edb5da0338eb5b8ca141b6f044581b5c83f9cd0a Mon Sep 17 00:00:00 2001 From: Leo-768 Date: Thu, 25 Apr 2024 22:02:12 +0800 Subject: [PATCH 3/3] typo --- .../nickid2018/genwiki/autovalue/EntityDataExtractor.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java b/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java index 5235fba..f9309c2 100644 --- a/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java +++ b/src/main/java/io/github/nickid2018/genwiki/autovalue/EntityDataExtractor.java @@ -41,8 +41,8 @@ public class EntityDataExtractor { public static final MethodHandle MOB_EFFECT_INSTANCE_GET_AMPLIFER; public static final MethodHandle POTION_GET_NAME; public static final MethodHandle POTION_GET_EFFECTS; - private static final MethodHandle ATTRIBUTE_MODIFIER_GET_AMOUNT; - private static final MethodHandle ATTRIBUTE_MODIFIER_GET_OPERATION; + public static final MethodHandle ATTRIBUTE_MODIFIER_GET_AMOUNT; + public static final MethodHandle ATTRIBUTE_MODIFIER_GET_OPERATION; public static final MethodHandle STRING_REPRESENTABLE_GET_SERIALIZED_NAME;