Skip to content

Commit

Permalink
Better Stats API
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Feb 17, 2025
1 parent 8eb8e44 commit 561e6fa
Show file tree
Hide file tree
Showing 32 changed files with 1,567 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.papermc.generator;

import com.google.common.reflect.TypeToken;
import io.papermc.generator.types.GeneratedKeyType;
import io.papermc.generator.types.GeneratedTagKeyType;
import io.papermc.generator.types.SourceGenerator;
import io.papermc.generator.types.goal.MobGoalGenerator;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.statistic.CustomStatistic;
import io.papermc.paper.statistic.StatisticType;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -52,6 +55,8 @@ public interface Generators {
simpleKey("AttributeKeys", Attribute.class, Registries.ATTRIBUTE, RegistryKey.ATTRIBUTE, false),
simpleKey("FluidKeys", Fluid.class, Registries.FLUID, RegistryKey.FLUID, false),
simpleKey("SoundEventKeys", Sound.class, Registries.SOUND_EVENT, RegistryKey.SOUND_EVENT, false),
simpleKey("CustomStatisticKeys", CustomStatistic.class, Registries.CUSTOM_STAT, RegistryKey.CUSTOM_STAT, false),
simpleKey("StatisticTypeKeys", new TypeToken<StatisticType<?>>() {}, Registries.STAT_TYPE, RegistryKey.STAT_TYPE, false),

// data-driven
simpleKey("BiomeKeys", Biome.class, Registries.BIOME, RegistryKey.BIOME, true),
Expand Down Expand Up @@ -87,6 +92,10 @@ public interface Generators {
};

private static <T, A> SourceGenerator simpleKey(final String className, final Class<A> apiType, final ResourceKey<? extends Registry<T>> registryKey, final RegistryKey<A> apiRegistryKey, final boolean publicCreateKeyMethod) {
return simpleKey(className, TypeToken.of(apiType), registryKey, apiRegistryKey, publicCreateKeyMethod);
}

private static <T, A> SourceGenerator simpleKey(final String className, final TypeToken<A> apiType, final ResourceKey<? extends Registry<T>> registryKey, final RegistryKey<A> apiRegistryKey, final boolean publicCreateKeyMethod) {
return new GeneratedKeyType<>(className, apiType, "io.papermc.paper.registry.keys", registryKey, apiRegistryKey, publicCreateKeyMethod);
}

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

import com.google.common.collect.Sets;
import com.google.common.reflect.TypeToken;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
Expand Down Expand Up @@ -78,12 +79,12 @@ public class GeneratedKeyType<T, A> extends SimpleGenerator {
@return a new typed key
""";

private final Class<A> apiType;
private final TypeToken<A> apiType;
private final ResourceKey<? extends Registry<T>> registryKey;
private final RegistryKey<A> apiRegistryKey;
private final boolean publicCreateKeyMethod;

public GeneratedKeyType(final String keysClassName, final Class<A> apiType, final String pkg, final ResourceKey<? extends Registry<T>> registryKey, final RegistryKey<A> apiRegistryKey, final boolean publicCreateKeyMethod) {
public GeneratedKeyType(final String keysClassName, final TypeToken<A> apiType, final String pkg, final ResourceKey<? extends Registry<T>> registryKey, final RegistryKey<A> apiRegistryKey, final boolean publicCreateKeyMethod) {
super(keysClassName, pkg);
this.apiType = apiType;
this.registryKey = registryKey;
Expand All @@ -102,7 +103,7 @@ private MethodSpec.Builder createMethod(final TypeName returnType) {
.returns(returnType);
if (this.publicCreateKeyMethod) {
create.addAnnotation(EXPERIMENTAL_API_ANNOTATION); // TODO remove once not experimental
create.addJavadoc(CREATE_JAVADOC, this.apiType, this.registryKey.location().toString());
create.addJavadoc(CREATE_JAVADOC, this.apiType.getRawType(), this.registryKey.location().toString());
}
return create;
}
Expand All @@ -127,7 +128,7 @@ private TypeSpec.Builder keyHolderType() {

@Override
protected TypeSpec getTypeSpec() {
final TypeName typedKey = ParameterizedTypeName.get(TypedKey.class, this.apiType);
final TypeName typedKey = ParameterizedTypeName.get(TypedKey.class, this.apiType.getType());

final TypeSpec.Builder typeBuilder = this.keyHolderType();
final MethodSpec.Builder createMethod = this.createMethod(typedKey);
Expand Down
Loading

0 comments on commit 561e6fa

Please sign in to comment.