Skip to content

Commit

Permalink
Merge pull request #2 from Leo-768/master
Browse files Browse the repository at this point in the history
Add Potion Effect
  • Loading branch information
Nickid2018 authored Apr 25, 2024
2 parents a1a7582 + edb5da0 commit a658cf3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
* `mob_effect_category`:状态效果类别。
* `mob_effect_color`:状态效果颜色。
* `mob_effect_instantenous`:瞬时状态效果。
- 其他
* `postion effect`:药水效果。

### 生成数据

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
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;


static {
Expand All @@ -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);
}
Expand All @@ -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<EntityType<?>>")
Expand Down Expand Up @@ -130,10 +158,47 @@ public static void extractEntityData(Object serverObj) {
EFFECT_CANNOT_AFFECT.get(effectID).sort(Comparator.naturalOrder());
}

@SourceClass("Registry<Potion>")
Object potionEffectRegistry = InjectedProcess.getRegistry("POTION");
@SourceClass("Set<ResourceKey<Potion>>")
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<MobEffectInstance>")
List<?> effectInstances = (List<?>) POTION_GET_EFFECTS.invoke(potionEffect);
for (Object effectInstance : effectInstances) {
@SourceClass("Holder<MobEffect>")
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,27 @@ public String output(int indent) {
for (Map.Entry<String, List<AttributeModifier>> 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");
}
return builder.toString();
}

private record AttributeModifier(String attribute, double amount, String operation) {
public static String printAttributeModifiers(int indent, Iterable<AttributeModifier> 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) {
}
}
Original file line number Diff line number Diff line change
@@ -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<String, PotionEffect> 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<String, PotionEffect> 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<EffectInstance> effects, List<AttributeModifiersWikiData.AttributeModifier> attributeModifiers) {}
}

0 comments on commit a658cf3

Please sign in to comment.