Skip to content

Commit

Permalink
Add new generators (#3914)
Browse files Browse the repository at this point in the history
* Add new generators

* run spotless apply
  • Loading branch information
ImMorpheus authored Nov 11, 2023
1 parent 8b6bc57 commit 5309c06
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.vanilla.generator;

import com.github.javaparser.utils.Log;
import com.google.common.base.CaseFormat;
import com.mojang.datafixers.util.Pair;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.WildcardTypeName;
Expand All @@ -50,21 +51,33 @@
import net.minecraft.world.damagesource.DamageScaling;
import net.minecraft.world.entity.Display;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.animal.Fox;
import net.minecraft.world.entity.animal.TropicalFish;
import net.minecraft.world.entity.animal.horse.Markings;
import net.minecraft.world.entity.animal.horse.Variant;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.item.ArmorMaterials;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.FireworkRocketItem;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.level.DataPackConfig;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.block.state.properties.BambooLeaves;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import org.tinylog.Logger;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -198,6 +211,20 @@ private static List<Generator> generators(final Context context) {
CriteriaTriggers.class,
"CRITERIA"
),
new MapEntriesValidator<>(
"world.gamerule",
"GameRules",
GameRules.class,
"GAME_RULE_TYPES",
map -> {
final Map<ResourceLocation, Object> out = new HashMap<>(map.size());
map.forEach((BiConsumer<Object, Object>) (k, v) -> {
var key = (GameRules.Key<?>) k;
out.put(new ResourceLocation("sponge:" + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, key.getId())), v);
});
return out;
}
),
new EnumEntriesValidator<>(
"item",
"FireworkShapes",
Expand All @@ -219,6 +246,55 @@ private static List<Generator> generators(final Context context) {
"getName",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"ArmorMaterials",
ArmorMaterials.class,
"getName",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"BambooLeavesTypes",
BambooLeaves.class,
"getSerializedName",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"DyeColors",
DyeColor.class,
"getName",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"FoxTypes",
Fox.Type.class,
"getSerializedName",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"HorseColors",
Variant.class,
"getSerializedName",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"HorseStyles",
Markings.class,
"name",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"InstrumentTypes",
NoteBlockInstrument.class,
"getSerializedName",
"sponge"
),
new EnumEntriesValidator<>(
"item",
"ItemRarities",
Expand All @@ -233,6 +309,13 @@ private static List<Generator> generators(final Context context) {
"getName",
"sponge"
),
new EnumEntriesValidator<>(
"data.type",
"TropicalFishShapes",
TropicalFish.Pattern.class,
"getSerializedName",
"sponge"
),
new RegistryEntriesGenerator<>(
"data.type",
"ArtTypes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,37 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

class MapEntriesValidator<V> implements Generator {

private final String relativePackageName;
private final String targetClassSimpleName;
private final Class<?> clazz;
private final String registry;
private final Function<Map<?, ?>, Map<ResourceLocation, ?>> mapping;

MapEntriesValidator(
final String targetRelativePackage,
final String targetClassSimpleName,
final Class<?> clazz,
final String registry
) {
this(targetRelativePackage, targetClassSimpleName, clazz, registry, map -> (Map<ResourceLocation, ?>) map);
}

MapEntriesValidator(
final String targetRelativePackage,
final String targetClassSimpleName,
final Class<?> clazz,
final String registry,
final Function<Map<?, ?>, Map<ResourceLocation, ?>> mapping
) {
this.relativePackageName = targetRelativePackage;
this.targetClassSimpleName = targetClassSimpleName;
this.clazz = clazz;
this.registry = registry;
this.mapping = mapping;
}

@Override
Expand All @@ -87,7 +100,7 @@ public void generate(final Context ctx) {
if (!(tmp instanceof Map<?, ?>)) {
throw new IllegalStateException("registry field is not a map");
}
map = (Map<ResourceLocation, ?>) tmp;
map = this.mapping.apply((Map<?, ?>) tmp);
} catch (Exception e) {
throw new IllegalStateException("Failed to retrieve registry field in class " + this.clazz.getName(), e);
}
Expand Down

0 comments on commit 5309c06

Please sign in to comment.