Skip to content

Commit

Permalink
Add attribute exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed Jun 30, 2024
1 parent 9e3d6a6 commit b20ae72
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/github/nickid2018/genwiki/RemapSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public static void remapSettings(GenWikiMode mode, RemapProgram remapProgram) {
}
)
);
remapProgram.addPostTransform(
"net.minecraft.world.entity.ai.attributes.Attribute",
ExtendAccessTransform.FIELD
);
remapProgram.addInjectEntries(new IncludeJarPackages("io.github.nickid2018.genwiki.autovalue"));
} else {
remapProgram.addPostTransform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import net.minecraft.world.item.alchemy.Potion;

import java.util.Comparator;
Expand All @@ -28,6 +30,10 @@ public class EntityDataExtractor {
public static final BooleanWikiData EFFECT_INSTANTENOUS = new BooleanWikiData();
public static final StringListWikiData EFFECT_CANNOT_AFFECT = new StringListWikiData();

public static final StringWikiData ATTRIBUTE_SENTIMENT = new StringWikiData();
public static final DoubleNumberWikiData ATTRIBUTE_RANGE = new DoubleNumberWikiData();
public static final NumberWikiData ATTRIBUTE_DEFAULT_VALUE = new NumberWikiData();

public static final PotionEffectWikiData POTION_EFFECT = new PotionEffectWikiData();

@SneakyThrows
Expand Down Expand Up @@ -93,11 +99,26 @@ public static void extractEntityData(MinecraftServer serverObj) {
}
}

for (ResourceKey<Attribute> attributeKey : BuiltInRegistries.ATTRIBUTE.registryKeySet()) {
String attributeID = attributeKey.location().getPath();
Attribute attribute = BuiltInRegistries.ATTRIBUTE.get(attributeKey);
ATTRIBUTE_SENTIMENT.put(attributeID, attribute.sentiment.name());
ATTRIBUTE_DEFAULT_VALUE.put(attributeID, attribute.defaultValue);
if (attribute instanceof RangedAttribute rangedAttribute) {
double minValue = rangedAttribute.getMinValue();
double maxValue = rangedAttribute.getMaxValue();
ATTRIBUTE_RANGE.put(attributeID, minValue, maxValue);
}
}

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");
WikiData.write(ATTRIBUTE_SENTIMENT, "attribute_sentiment.txt");
WikiData.write(ATTRIBUTE_RANGE, "attribute_range.txt");
WikiData.write(ATTRIBUTE_DEFAULT_VALUE, "attribute_default_value.txt");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.nickid2018.genwiki.autovalue.wikidata;

import it.unimi.dsi.fastutil.floats.FloatFloatImmutablePair;
import it.unimi.dsi.fastutil.floats.FloatFloatPair;
import it.unimi.dsi.fastutil.doubles.DoubleDoubleImmutablePair;
import it.unimi.dsi.fastutil.doubles.DoubleDoublePair;
import it.unimi.dsi.fastutil.objects.Object2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;

Expand All @@ -12,19 +12,27 @@
public class DoubleNumberWikiData implements WikiData {

private boolean enableFallback = false;
private float fallback1 = 0;
private float fallback2 = 0;
private double fallback1 = 0;
private double fallback2 = 0;
private boolean fallbackNil = false;
private boolean useFirstKeyComparator = true;

private static final Comparator<FloatFloatPair> FIRST_KEY_COMPARATOR = (o1, o2) -> Float.compare(o2.firstFloat(), o1.firstFloat());
private static final Comparator<FloatFloatPair> SECOND_KEY_COMPARATOR = (o1, o2) -> Float.compare(o2.secondFloat(), o1.secondFloat());
private static final Comparator<DoubleDoublePair> FIRST_KEY_COMPARATOR = (o1, o2) -> Double.compare(
o2.firstDouble(),
o1.firstDouble()
);
private static final Comparator<DoubleDoublePair> SECOND_KEY_COMPARATOR = (o1, o2) -> Double.compare(
o2.secondDouble(),
o1.secondDouble()
);

private static final Comparator<FloatFloatPair> FIRST_FIRST = FIRST_KEY_COMPARATOR.thenComparing(SECOND_KEY_COMPARATOR);
private static final Comparator<FloatFloatPair> FIRST_SECOND = SECOND_KEY_COMPARATOR.thenComparing(FIRST_KEY_COMPARATOR);
private static final Comparator<DoubleDoublePair> FIRST_FIRST = FIRST_KEY_COMPARATOR.thenComparing(
SECOND_KEY_COMPARATOR);
private static final Comparator<DoubleDoublePair> FIRST_SECOND = SECOND_KEY_COMPARATOR.thenComparing(
FIRST_KEY_COMPARATOR);

private final Object2ObjectAVLTreeMap<FloatFloatPair, Set<String>> groups = new Object2ObjectAVLTreeMap<>(
(o1, o2) -> useFirstKeyComparator ? FIRST_FIRST.compare(o1, o2) : FIRST_SECOND.compare(o1, o2)
private final Object2ObjectAVLTreeMap<DoubleDoublePair, Set<String>> groups = new Object2ObjectAVLTreeMap<>(
(o1, o2) -> useFirstKeyComparator ? FIRST_FIRST.compare(o1, o2) : FIRST_SECOND.compare(o1, o2)
);

public DoubleNumberWikiData setUseFirstKeyComparator(boolean useFirstKeyComparator) {
Expand All @@ -37,20 +45,20 @@ public DoubleNumberWikiData setFallbackNil(boolean fallbackNil) {
return this;
}

public DoubleNumberWikiData setFallback(float fallback1, float fallback2) {
public DoubleNumberWikiData setFallback(double fallback1, double fallback2) {
enableFallback = true;
this.fallback1 = fallback1;
this.fallback2 = fallback2;
return this;
}

public void put(String id, float value1, float value2) {
public void put(String id, double value1, double value2) {
if (enableFallback && value1 == fallback1)
return;
groups.computeIfAbsent(new FloatFloatImmutablePair(value1, value2), k -> new TreeSet<>()).add(id);
groups.computeIfAbsent(new DoubleDoubleImmutablePair(value1, value2), k -> new TreeSet<>()).add(id);
}

private String formatValue(float value) {
private String formatValue(double value) {
String formatted = String.format("%.3f", value);
if (formatted.contains("."))
while (formatted.endsWith("0"))
Expand All @@ -68,14 +76,15 @@ public String output(int indent) {
builder.append(tab).append("['__fallback'] = nil,\n\n");
else
builder.append(tab).append("['__fallback'] = {")
.append(formatValue(fallback1)).append(", ").append(formatValue(fallback2)).append("},\n\n");
for (Object2ObjectMap.Entry<FloatFloatPair, Set<String>> entry : groups.object2ObjectEntrySet()) {
FloatFloatPair value = entry.getKey();
String formatted1 = formatValue(value.firstFloat());
String formatted2 = formatValue(value.secondFloat());
.append(formatValue(fallback1)).append(", ").append(formatValue(fallback2)).append("},\n\n");
for (Object2ObjectMap.Entry<DoubleDoublePair, Set<String>> entry : groups.object2ObjectEntrySet()) {
DoubleDoublePair value = entry.getKey();
String formatted1 = formatValue(value.firstDouble());
String formatted2 = formatValue(value.secondDouble());
builder.append(tab).append("-- <").append(formatted1).append(", ").append(formatted2).append(">\n");
for (String id : entry.getValue())
builder.append(tab).append("['").append(id).append("'] = {").append(formatted1).append(", ").append(formatted2).append("},\n");
builder.append(tab).append("['").append(id).append("'] = {").append(formatted1).append(", ").append(
formatted2).append("},\n");
builder.append("\n");
}
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.github.nickid2018.genwiki.autovalue.wikidata;

import it.unimi.dsi.fastutil.floats.*;
import it.unimi.dsi.fastutil.doubles.Double2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.doubles.Double2ObjectMap;

import java.util.Set;
import java.util.TreeSet;

public class NumberWikiData implements WikiData {

private final Float2ObjectMap<Set<String>> groups = new Float2ObjectAVLTreeMap<>();
private final Double2ObjectMap<Set<String>> groups = new Double2ObjectAVLTreeMap<>();

private boolean enableFallback = false;
private float fallback = 0;
Expand All @@ -18,13 +19,13 @@ public NumberWikiData setFallback(float fallback) {
return this;
}

public void put(String id, float value) {
public void put(String id, double value) {
if (enableFallback && value == fallback)
return;
groups.computeIfAbsent(value, k -> new TreeSet<>()).add(id);
}

public static String formatValue(float value) {
public static String formatValue(double value) {
String formatted = String.format("%.3f", value);
if (formatted.contains("."))
while (formatted.endsWith("0"))
Expand All @@ -39,8 +40,8 @@ public String output(int indent) {
String tab = "\t".repeat(indent);
if (enableFallback)
builder.append(tab).append("['__fallback'] = ").append(formatValue(fallback)).append(",\n\n");
for (Float2ObjectMap.Entry<Set<String>> entry : groups.float2ObjectEntrySet()) {
float value = entry.getFloatKey();
for (Double2ObjectMap.Entry<Set<String>> entry : groups.double2ObjectEntrySet()) {
double value = entry.getDoubleKey();
String formatted = formatValue(value);
builder.append(tab).append("-- ").append(formatted).append("\n");
for (String id : entry.getValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.core.Registry;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.alchemy.Potion;
Expand All @@ -18,4 +19,5 @@ public class BuiltInRegistries {
public static final Registry<MobEffect> MOB_EFFECT = SneakyUtil.sneakyNotNull();
public static final Registry<Potion> POTION = SneakyUtil.sneakyNotNull();
public static final DefaultedRegistry<Block> BLOCK = SneakyUtil.sneakyNotNull();
public static final Registry<Attribute> ATTRIBUTE = SneakyUtil.sneakyNotNull();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.minecraft.world.entity.ai.attributes;

import io.github.nickid2018.util.SneakyUtil;

public class Attribute {

public final double defaultValue = SneakyUtil.sneakyInt();
public Sentiment sentiment = Sentiment.POSITIVE;

public enum Sentiment {
POSITIVE,
NEUTRAL,
NEGATIVE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.minecraft.world.entity.ai.attributes;

public class RangedAttribute extends Attribute {

public double getMinValue() {
throw new RuntimeException();
}

public double getMaxValue() {
throw new RuntimeException();
}
}

0 comments on commit b20ae72

Please sign in to comment.