port some generator fixes

- fix a regression for minecart entity type's class
- stable alphanumeric order for fields and data holder fields for CraftBlockData
- properly register bed and conduit block entity metas
This commit is contained in:
Lulu13022002
2025-06-10 17:04:49 +02:00
parent 7e68259ce9
commit 8d5be7ed9f
23 changed files with 447 additions and 437 deletions

View File

@@ -1,6 +1,5 @@
package io.papermc.generator;
import com.google.common.util.concurrent.MoreExecutors;
import com.mojang.logging.LogUtils;
import io.papermc.generator.rewriter.registration.PaperPatternSourceSetRewriter;
import io.papermc.generator.rewriter.registration.PatternSourceSetRewriter;
@@ -84,11 +83,11 @@ public class Main implements Callable<Integer> {
resourceManager,
layers,
pendingTags,
FeatureFlags.VANILLA_SET,
FeatureFlags.REGISTRY.allFlags(),
Commands.CommandSelection.DEDICATED,
0,
MoreExecutors.directExecutor(),
MoreExecutors.directExecutor()
Commands.LEVEL_GAMEMASTERS,
Runnable::run,
Runnable::run
).whenComplete((result, ex) -> {
if (ex != null) {
resourceManager.close();

View File

@@ -43,7 +43,7 @@ public class EnumRegistryRewriter<T> extends EnumRewriter<Holder.Reference<T>> {
@Override
protected Iterable<Holder.Reference<T>> getValues() {
return this.registry.get().listElements().sorted(Formatting.alphabeticKeyOrder(reference -> reference.key().location().getPath()))::iterator;
return this.registry.get().listElements().sorted(Formatting.HOLDER_ORDER)::iterator;
}
@Override

View File

@@ -71,7 +71,7 @@ public class RegistryFieldRewriter<T> extends SearchReplaceRewriter {
boolean isInterface = Objects.requireNonNull(this.fieldClass.knownClass()).isInterface();
Registry<T> registry = Main.REGISTRY_ACCESS.lookupOrThrow(this.registryKey);
this.experimentalKeys = Suppliers.memoize(() -> ExperimentalCollector.collectDataDrivenElementIds(registry));
Iterator<Holder.Reference<T>> referenceIterator = registry.listElements().filter(this::canPrintField).sorted(Formatting.alphabeticKeyOrder(reference -> reference.key().location().getPath())).iterator();
Iterator<Holder.Reference<T>> referenceIterator = registry.listElements().filter(this::canPrintField).sorted(Formatting.HOLDER_ORDER).iterator();
while (referenceIterator.hasNext()) {
Holder.Reference<T> reference = referenceIterator.next();

View File

@@ -57,7 +57,7 @@ public class RegistryTagRewriter<T> extends SearchReplaceRewriter {
@Override
protected void insert(SearchMetadata metadata, StringBuilder builder) {
Registry<T> registry = Main.REGISTRY_ACCESS.lookupOrThrow(this.registryKey);
Iterator<? extends TagKey<T>> keyIterator = registry.listTagIds().sorted(Formatting.alphabeticKeyOrder(reference -> reference.location().getPath())).iterator();
Iterator<? extends TagKey<T>> keyIterator = registry.listTagIds().sorted(Formatting.TAG_ORDER).iterator();
while (keyIterator.hasNext()) {
TagKey<T> tagKey = keyIterator.next();

View File

@@ -62,7 +62,7 @@ public class TagRewriter extends SearchReplaceRewriter {
builder.append('\n');
builder.append('\n');
Iterator<? extends TagKey<?>> keyIterator = registry.listTagIds().sorted(Formatting.alphabeticKeyOrder(tagKey -> tagKey.location().getPath())).iterator();
Iterator<? extends TagKey<?>> keyIterator = registry.listTagIds().sorted(Formatting.TAG_ORDER).iterator();
while (keyIterator.hasNext()) {
TagKey<?> tagKey = keyIterator.next();

View File

@@ -44,6 +44,7 @@ public class EntityTypeRewriter extends EnumRegistryRewriter<EntityType<?>> {
.put("LeashKnot", "LeashHitch")
.put("LightningBolt", "LightningStrike")
.put("Tnt", "TNTPrimed")
.put("Minecart", "RideableMinecart")
.put("ChestMinecart", "StorageMinecart")
.put("CommandBlockMinecart", "CommandMinecart")
.put("TntMinecart", "ExplosiveMinecart")

View File

@@ -30,7 +30,7 @@ public class MaterialRewriter {
@Override
protected Iterable<Holder.Reference<Block>> getValues() {
return BuiltInRegistries.BLOCK.listElements().filter(reference -> !reference.value().equals(net.minecraft.world.level.block.Blocks.AIR))
.sorted(Formatting.alphabeticKeyOrder(reference -> reference.key().location().getPath()))::iterator;
.sorted(Formatting.HOLDER_ORDER)::iterator;
}
@Override
@@ -86,7 +86,7 @@ public class MaterialRewriter {
@Override
protected Iterable<Holder.Reference<Item>> getValues() {
return BuiltInRegistries.ITEM.listElements().filter(reference -> BuiltInRegistries.BLOCK.getOptional(reference.key().location()).isEmpty() || reference.value().equals(net.minecraft.world.item.Items.AIR))
.sorted(Formatting.alphabeticKeyOrder(reference -> reference.key().location().getPath()))::iterator;
.sorted(Formatting.HOLDER_ORDER)::iterator;
}
@Override

View File

@@ -131,7 +131,7 @@ public class StatisticRewriter {
@Override
protected Iterable<Holder.Reference<StatType<?>>> getValues() {
return BuiltInRegistries.STAT_TYPE.listElements().filter(reference -> reference.value() != Stats.CUSTOM)
.sorted(Formatting.alphabeticKeyOrder(reference -> reference.key().location().getPath()))::iterator;
.sorted(Formatting.HOLDER_ORDER)::iterator;
}
@Override
@@ -157,7 +157,7 @@ public class StatisticRewriter {
@Override
protected Iterable<Holder.Reference<StatType<?>>> getValues() {
return BuiltInRegistries.STAT_TYPE.listElements().filter(reference -> reference.value() != Stats.CUSTOM)
.sorted(Formatting.alphabeticKeyOrder(reference -> reference.key().location().getPath()))::iterator;
.sorted(Formatting.HOLDER_ORDER)::iterator;
}
@Override

View File

@@ -6,9 +6,11 @@ import io.papermc.generator.utils.Formatting;
import it.unimi.dsi.fastutil.Pair;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.Property;
import org.jspecify.annotations.NullMarked;
@@ -16,11 +18,11 @@ import org.jspecify.annotations.NullMarked;
@NullMarked
public abstract class DataPropertyWriterBase implements DataPropertyMaker {
protected final Collection<? extends Property<?>> properties;
protected final Stream<? extends Property<?>> properties;
protected final Class<? extends Block> blockClass;
protected DataPropertyWriterBase(Collection<? extends Property<?>> properties, Class<? extends Block> blockClass) {
this.properties = properties;
this.properties = properties.stream().sorted(Comparator.comparing(Property::getName));
this.blockClass = blockClass;
}
@@ -31,12 +33,12 @@ public abstract class DataPropertyWriterBase implements DataPropertyMaker {
code.add("$T.of(\n", List.class);
}
code.indent();
Iterator<? extends Property<?>> it = this.properties.iterator();
while (it.hasNext()) {
Property<?> property = it.next();
Iterator<? extends Property<?>> properties = this.properties.iterator();
while (properties.hasNext()) {
Property<?> property = properties.next();
Pair<Class<?>, String> fieldName = PropertyWriter.referenceField(this.blockClass, property, fields);
code.add("$T.$L", fieldName.left(), fieldName.right());
if (it.hasNext()) {
if (properties.hasNext()) {
code.add(",");
}
code.add("\n");
@@ -47,13 +49,13 @@ public abstract class DataPropertyWriterBase implements DataPropertyMaker {
protected void createSyntheticMap(CodeBlock.Builder code, Class<?> indexClass, Map<Property<?>, Field> fields) {
// assume indexClass is an enum with its values matching the property names
code.add("$T.of(\n", Map.class).indent();
Iterator<? extends Property<?>> it = this.properties.iterator();
while (it.hasNext()) {
Property<?> property = it.next();
Iterator<? extends Property<?>> properties = this.properties.iterator();
while (properties.hasNext()) {
Property<?> property = properties.next();
String name = Formatting.formatKeyAsField(property.getName());
Pair<Class<?>, String> fieldName = PropertyWriter.referenceField(this.blockClass, property, fields);
code.add("$T.$L, $T.$L", indexClass, name, fieldName.left(), fieldName.right());
if (it.hasNext()) {
if (properties.hasNext()) {
code.add(",");
}
code.add("\n");

View File

@@ -84,7 +84,7 @@ public class GeneratedKeyType<T> extends SimpleGenerator {
MethodSpec.Builder createMethod = this.createMethod(typedKeyType);
boolean allExperimental = true;
for (Holder.Reference<T> reference : this.entry.registry().listElements().sorted(Formatting.alphabeticKeyOrder(reference -> reference.key().location().getPath())).toList()) {
for (Holder.Reference<T> reference : this.entry.registry().listElements().sorted(Formatting.HOLDER_ORDER).toList()) {
ResourceKey<T> key = reference.key();
String keyPath = key.location().getPath();
String fieldName = Formatting.formatKeyAsField(keyPath);

View File

@@ -73,7 +73,7 @@ public class GeneratedTagKeyType extends SimpleGenerator {
MethodSpec.Builder createMethod = this.createMethod(tagKeyType);
AtomicBoolean allExperimental = new AtomicBoolean(true);
this.entry.registry().listTagIds().sorted(Formatting.alphabeticKeyOrder(tagKey -> tagKey.location().getPath())).forEach(tagKey -> {
this.entry.registry().listTagIds().sorted(Formatting.TAG_ORDER).forEach(tagKey -> {
String fieldName = Formatting.formatKeyAsField(tagKey.location().getPath());
FieldSpec.Builder fieldBuilder = FieldSpec.builder(tagKeyType, fieldName, PUBLIC, STATIC, FINAL)
.initializer("$N(key($S))", createMethod.build(), tagKey.location().getPath())

View File

@@ -1,10 +1,11 @@
package io.papermc.generator.utils;
import java.util.Optional;
import net.minecraft.core.Holder;
import net.minecraft.tags.TagKey;
import org.apache.commons.lang3.math.NumberUtils;
import java.util.Comparator;
import java.util.Locale;
import java.util.OptionalInt;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
@@ -12,6 +13,7 @@ import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
public final class Formatting {
@@ -73,33 +75,36 @@ public final class Formatting {
return newName;
}
public static final Comparator<String> ALPHABETIC_KEY_ORDER = alphabeticKeyOrder(path -> path);
public static final Comparator<Holder.Reference<?>> HOLDER_ORDER = alphabeticKeyOrder(reference -> reference.key().location().getPath());
public static final Comparator<TagKey<?>> TAG_ORDER = alphabeticKeyOrder(tagKey -> tagKey.location().getPath());
public static <T> Comparator<T> alphabeticKeyOrder(Function<T, String> mapper) {
return (o1, o2) -> {
String path1 = mapper.apply(o1);
String path2 = mapper.apply(o2);
public static <T> Comparator<T> alphabeticKeyOrder(Function<T, String> pathConverter) {
return Comparator.comparing(pathConverter, (path1, path2) -> {
TrailingInt trailingInt1 = tryParseTrailingInt(path1);
TrailingInt trailingInt2 = tryParseTrailingInt(path2);
OptionalInt trailingInt1 = tryParseTrailingInt(path1);
OptionalInt trailingInt2 = tryParseTrailingInt(path2);
if (trailingInt1.isPresent() && trailingInt2.isPresent()) {
return Integer.compare(trailingInt1.getAsInt(), trailingInt2.getAsInt());
if (trailingInt1 != null && trailingInt2 != null &&
trailingInt1.prefix().equals(trailingInt2.prefix())) {
return Integer.compareUnsigned(trailingInt1.value(), trailingInt2.value());
}
return path1.compareTo(path2);
};
});
}
private static OptionalInt tryParseTrailingInt(String path) {
private static @Nullable TrailingInt tryParseTrailingInt(String path) {
int delimiterIndex = path.lastIndexOf('_');
if (delimiterIndex != -1) {
String score = path.substring(delimiterIndex + 1);
if (NumberUtils.isDigits(score)) {
return OptionalInt.of(Integer.parseInt(score));
String value = path.substring(delimiterIndex + 1);
if (NumberUtils.isDigits(value)) {
String prefix = path.substring(0, delimiterIndex);
return new TrailingInt(prefix, Integer.parseInt(value));
}
}
return OptionalInt.empty();
return null;
}
private record TrailingInt(String prefix, int value) {
}
private Formatting() {