Skip to content

Commit

Permalink
auto import transient types
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulu13022002 committed Sep 28, 2024
1 parent 70a9cd6 commit 00ee0fb
Show file tree
Hide file tree
Showing 29 changed files with 151 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ ij_java_use_fq_class_names = false
[Paper-Server/src/main/resources/data/**/*.json]
indent_size = 2

[{paper-generator/generatedApi/**/*.java, paper-generator/generatedServer/**/*.java}]
[paper-generator/generated{Api,Server}/**/*.java]
ij_java_imports_layout = $*,|,*
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public final class PaperRegistries {
entry(Registries.MENU, RegistryKey.MENU, MenuType.class, CraftMenuType::new),

// data-driven
entry(Registries.STRUCTURE, RegistryKey.STRUCTURE, Structure.class, CraftStructure::new).delayed(),
entry(Registries.STRUCTURE, RegistryKey.STRUCTURE, org.bukkit.generator.structure.Structure.class, CraftStructure::new).delayed(),
entry(Registries.TRIM_MATERIAL, RegistryKey.TRIM_MATERIAL, TrimMaterial.class, CraftTrimMaterial::new).delayed(),
entry(Registries.TRIM_PATTERN, RegistryKey.TRIM_PATTERN, TrimPattern.class, CraftTrimPattern::new).delayed(),
entry(Registries.DAMAGE_TYPE, RegistryKey.DAMAGE_TYPE, DamageType.class, CraftDamageType::new).delayed(),
entry(Registries.WOLF_VARIANT, RegistryKey.WOLF_VARIANT, Wolf.Variant.class, CraftWolf.CraftVariant::new).delayed(),
writable(Registries.ENCHANTMENT, RegistryKey.ENCHANTMENT, Enchantment.class, CraftEnchantment::new, PaperEnchantmentRegistryEntry.PaperBuilder::new).withSerializationUpdater(FieldRename.ENCHANTMENT_RENAME).delayed(),
writable(Registries.ENCHANTMENT, RegistryKey.ENCHANTMENT, org.bukkit.enchantments.Enchantment.class, CraftEnchantment::new, PaperEnchantmentRegistryEntry.PaperBuilder::new).withSerializationUpdater(FieldRename.ENCHANTMENT_RENAME).delayed(),
entry(Registries.JUKEBOX_SONG, RegistryKey.JUKEBOX_SONG, JukeboxSong.class, CraftJukeboxSong::new).delayed(),
entry(Registries.BANNER_PATTERN, RegistryKey.BANNER_PATTERN, PatternType.class, CraftPatternType::new).delayed(),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.papermc.generator;

import io.papermc.generator.rewriter.types.registry.definition.RegistryDefinitionRewriters;
import io.papermc.generator.registry.RegistryBootstrapper;
import io.papermc.generator.types.SourceGenerator;
import io.papermc.generator.types.craftblockdata.CraftBlockDataGenerators;
import io.papermc.generator.types.craftblockdata.CraftBlockDataBootstrapper;
import io.papermc.generator.types.goal.MobGoalGenerator;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -15,10 +15,10 @@
public interface Generators {

List<SourceGenerator> API = Collections.unmodifiableList(Util.make(new ArrayList<>(), list -> {
RegistryDefinitionRewriters.bootstrap(list);
RegistryBootstrapper.bootstrap(list);
list.add(new MobGoalGenerator("VanillaGoal", "com.destroystokyo.paper.entity.ai"));
// todo extract fields for registry based api
}));

List<SourceGenerator> SERVER = Collections.unmodifiableList(Util.make(new ArrayList<>(), CraftBlockDataGenerators::bootstrap));
List<SourceGenerator> SERVER = Collections.unmodifiableList(Util.make(new ArrayList<>(), CraftBlockDataBootstrapper::bootstrap));
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.papermc.generator;

import io.papermc.generator.registry.RegistryBootstrapper;
import io.papermc.generator.registry.RegistryEntries;
import io.papermc.generator.rewriter.registration.PatternSourceSetRewriter;
import io.papermc.generator.rewriter.types.Types;
import io.papermc.generator.rewriter.types.registry.EnumRegistryRewriter;
import io.papermc.generator.rewriter.types.registry.FeatureFlagRewriter;
import io.papermc.generator.rewriter.types.registry.RegistryFieldRewriter;
import io.papermc.generator.rewriter.types.registry.TagRewriter;
import io.papermc.generator.rewriter.types.Types;
import io.papermc.generator.rewriter.types.registry.definition.RegistryDefinitionRewriters;
import io.papermc.generator.rewriter.types.registry.definition.RegistryEntries;
import io.papermc.generator.rewriter.types.simple.BlockTypeRewriter;
import io.papermc.generator.rewriter.types.simple.CraftBlockDataMapping;
import io.papermc.generator.rewriter.types.simple.CraftBlockEntityStateMapping;
Expand All @@ -23,6 +23,7 @@
import io.papermc.typewriter.preset.EnumCloneRewriter;
import io.papermc.typewriter.preset.model.EnumValue;
import java.util.Locale;
import javax.lang.model.SourceVersion;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down Expand Up @@ -70,8 +71,6 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;

import javax.lang.model.SourceVersion;

import static io.papermc.generator.rewriter.registration.PaperPatternSourceSetRewriter.composite;
import static io.papermc.generator.rewriter.registration.RewriterHolder.holder;
import static io.papermc.generator.utils.Formatting.asCode;
Expand All @@ -82,9 +81,9 @@ public final class Rewriters {

public static void bootstrap(PatternSourceSetRewriter apiSourceSet, PatternSourceSetRewriter serverSourceSet) {
apiSourceSet
.register("Fluid", Fluid.class, new EnumRegistryRewriter<>(Registries.FLUID).nameAsKey())
.register("Fluid", Fluid.class, EnumRegistryRewriter.forLegacyEnum(Registries.FLUID))
.register("Sound", Sound.class, new EnumRegistryRewriter<>(Registries.SOUND_EVENT))
.register("Biome", Biome.class, new EnumRegistryRewriter<>(Registries.BIOME).nameAsKey())
.register("Biome", Biome.class, EnumRegistryRewriter.forLegacyEnum(Registries.BIOME))
.register("Attribute", Attribute.class, new EnumRegistryRewriter<>(Registries.ATTRIBUTE))
.register("PotionType", PotionType.class, new EnumRegistryRewriter<>(Registries.POTION))
.register("Art", Art.class, new EnumRegistryRewriter<>(Registries.PAINTING_VARIANT) {
Expand Down Expand Up @@ -198,6 +197,6 @@ protected String rewriteFieldName(Holder.Reference<net.minecraft.world.item.Juke
holder("CraftPotionUtil#upgradeable", new CraftPotionUtilRewriter("strong")),
holder("CraftPotionUtil#extendable", new CraftPotionUtilRewriter("long"))
));
RegistryDefinitionRewriters.bootstrap(apiSourceSet, serverSourceSet);
RegistryBootstrapper.bootstrap(apiSourceSet, serverSourceSet);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.papermc.generator.rewriter.types.registry.definition;
package io.papermc.generator.registry;

import io.papermc.generator.rewriter.registration.PatternSourceSetRewriter;
import io.papermc.generator.rewriter.types.Types;
import io.papermc.generator.rewriter.types.registry.PaperRegistriesRewriter;
import io.papermc.generator.rewriter.types.registry.RegistryEventsRewriter;
import io.papermc.generator.types.SourceGenerator;
import io.papermc.generator.types.registry.GeneratedKeyType;
import io.papermc.generator.types.registry.GeneratedTagKeyType;
import io.papermc.paper.registry.event.RegistryEvents;
import java.util.List;

public class RegistryDefinitionRewriters {
public class RegistryBootstrapper {

private static final String PAPER_REGISTRY_PACKAGE = "io.papermc.paper.registry";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package io.papermc.generator.rewriter.types.registry.definition;
package io.papermc.generator.registry;

import io.papermc.generator.utils.ClassHelper;
import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
import io.papermc.paper.registry.data.GameEventRegistryEntry;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import net.minecraft.core.Registry;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.registries.Registries;
Expand Down Expand Up @@ -34,6 +44,7 @@
import org.bukkit.Fluid;
import org.bukkit.GameEvent;
import org.bukkit.JukeboxSong;
import org.bukkit.Keyed;
import org.bukkit.MusicInstrument;
import org.bukkit.Particle;
import org.bukkit.Sound;
Expand All @@ -59,34 +70,24 @@
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

public final class RegistryEntries {

private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> registryKey, @Nullable Class<?> registryConstantClass, Class<?> apiClass, String implClass) {
return new RegistryEntry<>(registryKey, (RegistryField<T>) REGISTRY_KEY_TO_FIELD.get(registryKey), registryConstantClass, apiClass, implClass);
private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> registryKey, @Nullable Class<?> registryConstantClass, Class<? extends Keyed> apiClass, String implClass) {
return new RegistryEntry<>(registryKey, (RegistryKeyField<T>) REGISTRY_KEY_FIELDS.get(registryKey), registryConstantClass, apiClass, implClass);
}

// CraftBukkit entry where implementation start by "Craft"
private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> registryKey, @Nullable Class<?> registryConstantClass, Class<?> apiClass) {
private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> registryKey, @Nullable Class<?> registryConstantClass, Class<? extends Keyed> apiClass) {
return entry(registryKey, registryConstantClass, "Craft", apiClass);
}

private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> registryKey, @Nullable Class<?> registryConstantClass, String implPrefix, Class<?> apiClass) {
private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> registryKey, @Nullable Class<?> registryConstantClass, String implPrefix, Class<? extends Keyed> apiClass) {
String name = io.papermc.typewriter.utils.ClassHelper.retrieveFullNestedName(apiClass);
RegistryField<T> registryField = (RegistryField<T>) REGISTRY_KEY_TO_FIELD.get(registryKey);
RegistryKeyField<T> registryKeyField = (RegistryKeyField<T>) REGISTRY_KEY_FIELDS.get(registryKey);
String[] classes = name.split("\\.");
if (classes.length == 0) {
return new RegistryEntry<>(registryKey, registryField, registryConstantClass, apiClass, implPrefix.concat(apiClass.getSimpleName()));
return new RegistryEntry<>(registryKey, registryKeyField, registryConstantClass, apiClass, implPrefix.concat(apiClass.getSimpleName()));
}

StringBuilder implName = new StringBuilder(name.length() + implPrefix.length() * classes.length);
Expand All @@ -96,12 +97,12 @@ private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> reg
implName.append(implPrefix.concat(classes[i]));
}

return new RegistryEntry<>(registryKey, registryField, registryConstantClass, apiClass,implName.toString());
return new RegistryEntry<>(registryKey, registryKeyField, registryConstantClass, apiClass,implName.toString());
}

private static final Map<ResourceKey<? extends Registry<?>>, RegistryField<?>> REGISTRY_KEY_TO_FIELD;
private static final Map<ResourceKey<? extends Registry<?>>, RegistryKeyField<?>> REGISTRY_KEY_FIELDS;
static {
Map<ResourceKey<? extends Registry<?>>, RegistryField<?>> registryKeyToField = new IdentityHashMap<>();
Map<ResourceKey<? extends Registry<?>>, RegistryKeyField<?>> registryKeyFields = new IdentityHashMap<>();
try {
for (final Field field : Registries.class.getDeclaredFields()) {
if (!ResourceKey.class.isAssignableFrom(field.getType())) {
Expand All @@ -111,14 +112,14 @@ private static <T> RegistryEntry<T> entry(ResourceKey<? extends Registry<T>> reg
if (ClassHelper.isStaticConstant(field, Modifier.PUBLIC)) {
@Nullable Type elementType = ClassHelper.getNestedTypeParameter(field.getGenericType(), ResourceKey.class, Registry.class, null);
if (elementType != null) {
registryKeyToField.put(((ResourceKey<? extends Registry<?>>) field.get(null)), new RegistryField<>(ClassHelper.eraseType(elementType), field.getName()));
registryKeyFields.put(((ResourceKey<? extends Registry<?>>) field.get(null)), new RegistryKeyField<>(ClassHelper.eraseType(elementType), field.getName()));
}
}
}
} catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
REGISTRY_KEY_TO_FIELD = Collections.unmodifiableMap(registryKeyToField);
REGISTRY_KEY_FIELDS = Collections.unmodifiableMap(registryKeyFields);
}

public static final Set<Class<?>> REGISTRY_CLASS_NAME_BASED_ON_API = Set.of(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package io.papermc.generator.rewriter.types.registry.definition;
package io.papermc.generator.registry;

import io.papermc.generator.Main;
import io.papermc.generator.utils.ClassHelper;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
Expand All @@ -14,15 +10,20 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import org.bukkit.Keyed;
import org.checkerframework.checker.nullness.qual.Nullable;

public final class RegistryEntry<T> {

private final ResourceKey<? extends Registry<T>> registryKey;
private final RegistryField<T> registryField;
private final RegistryKeyField<T> registryKeyField;
private final Class<T> registryElementClass;
private final @Nullable Class<?> registryConstantClass;

private final Class<?> apiClass;
private final Class<? extends Keyed> apiClass; // TODO remove Keyed
private final String implClass;

private @Nullable Class<?> apiRegistryBuilder;
Expand All @@ -34,10 +35,10 @@ public final class RegistryEntry<T> {

private @Nullable Map<ResourceKey<T>, String> fieldNames;

public RegistryEntry(ResourceKey<? extends Registry<T>> registryKey, RegistryField<T> registryField, @Nullable Class<?> registryConstantClass, Class<?> apiClass, String implClass) {
public RegistryEntry(ResourceKey<? extends Registry<T>> registryKey, RegistryKeyField<T> registryKeyField, @Nullable Class<?> registryConstantClass, Class<? extends Keyed> apiClass, String implClass) {
this.registryKey = registryKey;
this.registryField = registryField;
this.registryElementClass = registryField.elementClass();
this.registryKeyField = registryKeyField;
this.registryElementClass = registryKeyField.elementClass();
this.registryConstantClass = registryConstantClass;
this.apiClass = apiClass;
this.implClass = implClass;
Expand All @@ -52,10 +53,10 @@ public Registry<T> registry() {
}

public String registryKeyField() {
return this.registryField.fieldName();
return this.registryKeyField.name();
}

public Class<?> apiClass() {
public Class<? extends Keyed> apiClass() {
return this.apiClass;
}

Expand Down Expand Up @@ -172,7 +173,7 @@ public <TO> Map<ResourceKey<T>, TO> getFields(Function<Field, @Nullable TO> tran
public String toString() {
return "RegistryEntry[" +
"registryKey=" + this.registryKey + ", " +
"registryField=" + this.registryField + ", " +
"registryKeyField=" + this.registryKeyField + ", " +
"apiClass=" + this.apiClass + ", " +
"implClass=" + this.implClass + ", " +
']';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.papermc.generator.registry;

public record RegistryKeyField<T>(Class<T> elementClass, String name) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.papermc.generator.types.SimpleGenerator;
import io.papermc.paper.generated.GeneratedFrom;
import io.papermc.typewriter.ClassNamed;
import io.papermc.typewriter.FileMetadata;
import io.papermc.typewriter.IndentUnit;
import io.papermc.typewriter.SourceFile;
import io.papermc.typewriter.SourceRewriter;
Expand All @@ -21,6 +22,7 @@
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;

@DefaultQualifier(NonNull.class)
public class PaperPatternSourceSetRewriter extends SourceSetRewriterImpl<PatternSourceSetRewriter> implements PatternSourceSetRewriter {
Expand All @@ -35,9 +37,15 @@ public class PaperPatternSourceSetRewriter extends SourceSetRewriterImpl<Pattern

@Deprecated
public PaperPatternSourceSetRewriter(Path alternateOutput) {
super(INDENT_UNIT);
this.alternateOutput = alternateOutput;
}

@VisibleForTesting
public FileMetadata getMetadata() {
return this.fileMetadata;
}

@Deprecated
public Path getAlternateOutput() {
return this.alternateOutput;
Expand All @@ -54,12 +62,12 @@ private static ReplaceOptionsLike getOptions(String pattern, @Nullable ClassName

@Override
public PatternSourceSetRewriter register(final String pattern, final ClassNamed targetClass, final SearchReplaceRewriter rewriter) {
return super.register(SourceFile.of(targetClass.topLevel(), INDENT_UNIT), rewriter.withOptions(getOptions(pattern, targetClass)).customName(pattern));
return super.register(SourceFile.of(targetClass.topLevel()), rewriter.withOptions(getOptions(pattern, targetClass)).customName(pattern));
}

@Override
public PatternSourceSetRewriter register(final ClassNamed mainClass, final CompositeRewriter rewriter) {
return super.register(SourceFile.of(mainClass, INDENT_UNIT), rewriter);
return super.register(SourceFile.of(mainClass), rewriter);
}

@Contract(value = "_ -> new", pure = true)
Expand All @@ -72,7 +80,7 @@ public static CompositeRewriter composite(final RewriterHolder... holders) {
@Override
public void apply(final Path output) throws IOException { // todo remove
for (Map.Entry<SourceFile, SourceRewriter> rewriter : this.rewrites.entrySet()) {
rewriter.getValue().writeToFile(output, this.alternateOutput, rewriter.getKey());
rewriter.getValue().writeToFile(output, this.alternateOutput, this.fileMetadata, rewriter.getKey());
}
}
}
Loading

0 comments on commit 00ee0fb

Please sign in to comment.