This commit is contained in:
Sebastian Hartte
2020-07-12 16:54:39 +02:00
parent 1bd4bd00a8
commit 006316603d
256 changed files with 812 additions and 600 deletions
@@ -0,0 +1,29 @@
package appeng.forge.data;
import appeng.forge.data.providers.loot.BlockDropProvider;
import appeng.forge.data.providers.recipes.SlabStairRecipes;
import appeng.forge.data.providers.tags.ConventionTagProvider;
import net.minecraft.data.DataGenerator;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
public class AE2DataGenerators {
public static void dump() {
Path output = Paths.get("../src/generated/resources");
DataGenerator generator = new DataGenerator(output, Collections.emptyList());
generator.install(new BlockDropProvider(output));
generator.install(new SlabStairRecipes(output));
generator.install(new ConventionTagProvider(output));
try {
generator.run();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,14 @@
package appeng.forge.data.providers;
import net.minecraft.data.DataProvider;
import appeng.api.AEApi;
import appeng.api.definitions.IBlocks;
import appeng.api.definitions.IItems;
import appeng.api.definitions.IMaterials;
public interface IAE2DataProvider extends DataProvider {
IBlocks BLOCKS = AEApi.instance().definitions().blocks();
IItems ITEMS = AEApi.instance().definitions().items();
IMaterials MATERIALS = AEApi.instance().definitions().materials();
}
@@ -0,0 +1,100 @@
package appeng.forge.data.providers.loot;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nonnull;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import net.minecraft.block.Block;
import net.minecraft.data.DataCache;
import net.minecraft.data.DataProvider;
import net.minecraft.data.server.BlockLootTableGenerator;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.loot.*;
import net.minecraft.loot.condition.SurvivesExplosionLootCondition;
import net.minecraft.loot.context.LootContextTypes;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.loot.entry.LeafEntry;
import net.minecraft.loot.function.ApplyBonusLootFunction;
import net.minecraft.loot.function.SetCountLootFunction;
import net.minecraft.util.Identifier;
import appeng.core.AppEng;
import appeng.forge.data.providers.IAE2DataProvider;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
public class BlockDropProvider extends BlockLootTableGenerator implements IAE2DataProvider {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private final Path outputFolder;
private Map<Block, Function<Block, LootTable.Builder>> overrides = ImmutableMap.<Block, Function<Block, LootTable.Builder>>builder()
.put(BLOCKS.matrixFrame().block(), $ -> LootTable.builder())
.put(BLOCKS.quartzOre().block(),
b -> dropsWithSilkTouch(BLOCKS.quartzOre().block(),
applyExplosionDecay(BLOCKS.quartzOre().block(),
ItemEntry.builder(MATERIALS.certusQuartzCrystal().item())
.apply(SetCountLootFunction.builder(UniformLootTableRange.between(1.0F, 2.0F)))
.apply(ApplyBonusLootFunction.uniformBonusCount(Enchantments.FORTUNE)))))
.put(BLOCKS.quartzOreCharged().block(),
b -> dropsWithSilkTouch(BLOCKS.quartzOreCharged().block(),
applyExplosionDecay(BLOCKS.quartzOreCharged().block(),
ItemEntry.builder(MATERIALS.certusQuartzCrystalCharged().item())
.apply(SetCountLootFunction.builder(UniformLootTableRange.between(1.0F, 2.0F)))
.apply(ApplyBonusLootFunction.uniformBonusCount(Enchantments.FORTUNE)))))
.build();
public BlockDropProvider(Path outputFolder) {
this.outputFolder = outputFolder;
}
@Override
public void run(DataCache cache) throws IOException {
for (Map.Entry<RegistryKey<Block>, Block> entry : Registry.BLOCK.getEntries()) {
LootTable.Builder builder;
Identifier id = entry.getKey().getValue();
if (id.getNamespace().equals(AppEng.MOD_ID)) {
builder = overrides.getOrDefault(entry.getValue(), this::defaultBuilder).apply(entry.getValue());
DataProvider.writeToPath(GSON, cache, toJson(builder), getPath(outputFolder, id));
}
}
}
private LootTable.Builder defaultBuilder(Block block) {
LeafEntry.Builder<?> entry = ItemEntry.builder(block);
LootPool.Builder pool = LootPool.builder().rolls(ConstantLootTableRange.create(1)).with(entry)
.conditionally(SurvivesExplosionLootCondition.builder());
return LootTable.builder().pool(pool);
}
private Path getPath(Path root, Identifier id) {
return root.resolve("data/" + id.getNamespace() + "/loot_tables/blocks/" + id.getPath() + ".json");
}
public JsonElement toJson(LootTable.Builder builder) {
return LootManager.toJson(finishBuilding(builder));
}
@Nonnull
public LootTable finishBuilding(LootTable.Builder builder) {
return builder.type(LootContextTypes.BLOCK).build();
}
@Nonnull
@Override
public String getName() {
return AppEng.MOD_NAME + " Block Drops";
}
}
@@ -0,0 +1,96 @@
package appeng.forge.data.providers.recipes;
import appeng.api.definitions.IBlockDefinition;
import appeng.core.AppEng;
import appeng.forge.data.providers.IAE2DataProvider;
import com.google.gson.JsonObject;
import net.minecraft.block.Block;
import net.minecraft.data.DataCache;
import net.minecraft.data.server.recipe.RecipeJsonProvider;
import net.minecraft.data.server.recipe.ShapedRecipeJsonFactory;
import net.minecraft.data.server.recipe.SingleItemRecipeJsonFactory;
import net.minecraft.recipe.Ingredient;
import net.minecraft.util.Identifier;
import java.nio.file.Path;
import java.util.function.Consumer;
import static net.minecraft.data.server.RecipesProvider.conditionsFromItem;
import static net.minecraft.data.server.RecipesProvider.saveRecipe;
import static net.minecraft.data.server.RecipesProvider.saveRecipeAdvancement;
public class SlabStairRecipes implements IAE2DataProvider {
IBlockDefinition[][] blocks = {{BLOCKS.skyStoneBlock(), BLOCKS.skyStoneSlab(), BLOCKS.skyStoneStairs()},
{BLOCKS.smoothSkyStoneBlock(), BLOCKS.smoothSkyStoneSlab(), BLOCKS.smoothSkyStoneStairs()},
{BLOCKS.skyStoneBrick(), BLOCKS.skyStoneBrickSlab(), BLOCKS.skyStoneBrickStairs()},
{BLOCKS.skyStoneSmallBrick(), BLOCKS.skyStoneSmallBrickSlab(), BLOCKS.skyStoneSmallBrickStairs()},
{BLOCKS.fluixBlock(), BLOCKS.fluixSlab(), BLOCKS.fluixStairs()},
{BLOCKS.quartzBlock(), BLOCKS.quartzSlab(), BLOCKS.quartzStairs()},
{BLOCKS.chiseledQuartzBlock(), BLOCKS.chiseledQuartzSlab(), BLOCKS.chiseledQuartzStairs()},
{BLOCKS.quartzPillar(), BLOCKS.quartzPillarSlab(), BLOCKS.quartzPillarStairs()},};
private final Path outputPath;
private final Consumer<RecipeJsonProvider> consumer;
private DataCache cache;
public SlabStairRecipes(Path outputPath) {
this.outputPath = outputPath;
this.consumer = this::provideRecipe;
}
public void run(DataCache cache) {
this.cache = cache;
for (IBlockDefinition[] block : blocks) {
slabRecipe(block[0], block[1]);
stairRecipe(block[0], block[2]);
}
}
private void slabRecipe(IBlockDefinition block, IBlockDefinition slabs) {
Block inputBlock = block.block();
Block outputBlock = slabs.block();
ShapedRecipeJsonFactory.create(slabs.block(), 6).pattern("###").input('#', inputBlock)
.criterion(criterionName(block), conditionsFromItem(inputBlock))
.offerTo(consumer, new Identifier(AppEng.MOD_ID, "shaped/slabs/" + block.identifier()));
SingleItemRecipeJsonFactory.create(Ingredient.ofItems(inputBlock), outputBlock, 2)
.create(criterionName(block), conditionsFromItem(inputBlock))
.offerTo(consumer, new Identifier(AppEng.MOD_ID, "block_cutter/slabs/" + slabs.identifier()));
}
private void stairRecipe(IBlockDefinition block, IBlockDefinition stairs) {
Block inputBlock = block.block();
Block outputBlock = stairs.block();
ShapedRecipeJsonFactory.create(outputBlock, 4).pattern("# ").pattern("## ").pattern("###")
.input('#', inputBlock).criterion(criterionName(block), conditionsFromItem(inputBlock))
.offerTo(consumer, new Identifier(AppEng.MOD_ID, "shaped/stairs/" + block.identifier()));
SingleItemRecipeJsonFactory.create(Ingredient.ofItems(inputBlock), outputBlock)
.create(criterionName(block), conditionsFromItem(inputBlock))
.offerTo(consumer, new Identifier(AppEng.MOD_ID, "block_cutter/stairs/" + stairs.identifier()));
}
private void provideRecipe(RecipeJsonProvider recipeJsonProvider) {
saveRecipe(cache, recipeJsonProvider.toJson(), outputPath.resolve("data/" + recipeJsonProvider.getRecipeId().getNamespace() + "/recipes/" + recipeJsonProvider.getRecipeId().getPath() + ".json"));
JsonObject jsonObject = recipeJsonProvider.toAdvancementJson();
if (jsonObject != null) {
saveRecipeAdvancement(cache, jsonObject, outputPath.resolve("data/" + recipeJsonProvider.getRecipeId().getNamespace() + "/advancements/" + recipeJsonProvider.getAdvancementId().getPath() + ".json"));
}
}
private String criterionName(IBlockDefinition block) {
return String.format("has_%s", block.identifier());
}
@Override
public String getName() {
return AppEng.MOD_NAME + " Slabs and Stairs";
}
}
@@ -0,0 +1,112 @@
package appeng.forge.data.providers.tags;
import appeng.core.AppEng;
import net.minecraft.block.Blocks;
import net.minecraft.item.Items;
import java.io.IOException;
import java.nio.file.Path;
public class ConventionTagProvider extends TagProvider {
public ConventionTagProvider(Path outputPath) {
super(outputPath);
}
@Override
protected void generate() throws IOException {
// Dyes
addItemTag("white_dyes", Items.WHITE_DYE);
addItemTag("orange_dyes", Items.ORANGE_DYE);
addItemTag("magenta_dyes", Items.MAGENTA_DYE);
addItemTag("light_blue_dyes", Items.LIGHT_BLUE_DYE);
addItemTag("yellow_dyes", Items.YELLOW_DYE);
addItemTag("lime_dyes", Items.LIME_DYE);
addItemTag("pink_dyes", Items.PINK_DYE);
addItemTag("gray_dyes", Items.GRAY_DYE);
addItemTag("light_gray_dyes", Items.LIGHT_GRAY_DYE);
addItemTag("cyan_dyes", Items.CYAN_DYE);
addItemTag("purple_dyes", Items.PURPLE_DYE);
addItemTag("blue_dyes", Items.BLUE_DYE);
addItemTag("brown_dyes", Items.BROWN_DYE);
addItemTag("green_dyes", Items.GREEN_DYE);
addItemTag("red_dyes", Items.RED_DYE);
addItemTag("black_dyes", Items.BLACK_DYE);
addItemTag("iron_ingots", Items.IRON_INGOT);
addItemTag("iron_ores", Items.IRON_ORE);
addItemTag("gold_ingots", Items.GOLD_INGOT);
addItemTag("gold_ores", Items.GOLD_ORE);
addItemTag("glowstone_dusts", Items.GLOWSTONE_DUST);
addItemTag("wooden_rods", Items.STICK);
addItemTag("nether_quartz_ores", Items.NETHER_QUARTZ_ORE);
addItemTag("nether_quartz_crystals", Items.QUARTZ);
addItemTag("sand_blocks", Items.SAND, Items.RED_SAND);
addItemTag("diamonds", Items.DIAMOND);
addItemTag("wooden_chests", Items.CHEST, Items.TRAPPED_CHEST);
addItemTag("wheat_crops", Items.WHEAT);
addItemTag("redstone_dusts", Items.REDSTONE);
addItemTag("ender_pearls", Items.ENDER_PEARL);
addItemTag("terracotta_blocks", Items.TERRACOTTA,
Items.WHITE_TERRACOTTA,
Items.ORANGE_TERRACOTTA,
Items.MAGENTA_TERRACOTTA,
Items.LIGHT_BLUE_TERRACOTTA,
Items.YELLOW_TERRACOTTA,
Items.LIME_TERRACOTTA,
Items.PINK_TERRACOTTA,
Items.GRAY_TERRACOTTA,
Items.LIGHT_GRAY_TERRACOTTA,
Items.CYAN_TERRACOTTA,
Items.PURPLE_TERRACOTTA,
Items.BLUE_TERRACOTTA,
Items.BROWN_TERRACOTTA,
Items.GREEN_TERRACOTTA,
Items.RED_TERRACOTTA,
Items.BLACK_TERRACOTTA
);
addItemTag("glass_blocks", Items.GLASS,
Items.WHITE_STAINED_GLASS,
Items.ORANGE_STAINED_GLASS,
Items.MAGENTA_STAINED_GLASS,
Items.LIGHT_BLUE_STAINED_GLASS,
Items.YELLOW_STAINED_GLASS,
Items.LIME_STAINED_GLASS,
Items.PINK_STAINED_GLASS,
Items.GRAY_STAINED_GLASS,
Items.LIGHT_GRAY_STAINED_GLASS,
Items.CYAN_STAINED_GLASS,
Items.PURPLE_STAINED_GLASS,
Items.BLUE_STAINED_GLASS,
Items.BROWN_STAINED_GLASS,
Items.GREEN_STAINED_GLASS,
Items.RED_STAINED_GLASS,
Items.BLACK_STAINED_GLASS
);
addBlockTag("glass_blocks", Blocks.GLASS,
Blocks.WHITE_STAINED_GLASS,
Blocks.ORANGE_STAINED_GLASS,
Blocks.MAGENTA_STAINED_GLASS,
Blocks.LIGHT_BLUE_STAINED_GLASS,
Blocks.YELLOW_STAINED_GLASS,
Blocks.LIME_STAINED_GLASS,
Blocks.PINK_STAINED_GLASS,
Blocks.GRAY_STAINED_GLASS,
Blocks.LIGHT_GRAY_STAINED_GLASS,
Blocks.CYAN_STAINED_GLASS,
Blocks.PURPLE_STAINED_GLASS,
Blocks.BLUE_STAINED_GLASS,
Blocks.BROWN_STAINED_GLASS,
Blocks.GREEN_STAINED_GLASS,
Blocks.RED_STAINED_GLASS,
Blocks.BLACK_STAINED_GLASS
);
}
@Override
public String getName() {
return AppEng.MOD_NAME + " Convention Tags";
}
}
@@ -0,0 +1,84 @@
package appeng.forge.data.providers.tags;
import appeng.core.AppEng;
import appeng.forge.data.providers.IAE2DataProvider;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.block.Block;
import net.minecraft.data.DataCache;
import net.minecraft.data.DataProvider;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public abstract class TagProvider implements IAE2DataProvider {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
protected static final String CONVENTION_NAMESPACE = "c";
protected static final String TYPE_ITEMS = "items";
protected static final String TYPE_BLOCKS = "blocks";
private final Path outputPath;
private DataCache cache;
protected TagProvider(Path outputPath) {
this.outputPath = outputPath;
}
@Override
public void run(DataCache cache) throws IOException {
this.cache = cache;
try {
generate();
} finally {
this.cache = null;
}
}
protected abstract void generate() throws IOException;
protected void addItemTag(String name, ItemConvertible... items) throws IOException {
List<String> itemIds = Arrays.stream(items)
.map(ItemConvertible::asItem)
.map(Registry.ITEM::getId)
.map(Identifier::toString)
.collect(Collectors.toList());
writeTagFile(CONVENTION_NAMESPACE, TYPE_ITEMS, name, itemIds);
}
protected void addBlockTag(String name, Block... blocks) throws IOException {
List<String> itemIds = Arrays.stream(blocks)
.map(Registry.BLOCK::getId)
.map(Identifier::toString)
.collect(Collectors.toList());
writeTagFile(CONVENTION_NAMESPACE, TYPE_BLOCKS, name, itemIds);
}
protected void writeTagFile(String namespace, String tagType, String tagName, List<String> entries) throws IOException {
JsonObject rootObj = new JsonObject();
JsonArray valuesArr = new JsonArray();
for (String entry : entries) {
valuesArr.add(entry);
}
rootObj.add("values", valuesArr);
Path path = outputPath.resolve("data/" + namespace + "/tags/" + tagType + "/" + tagName + ".json");
DataProvider.writeToPath(GSON, this.cache, rootObj, path);
}
@Override
public String getName() {
return AppEng.MOD_NAME + " Convention Tags";
}
}