Skip to content

Commit d7674a5

Browse files
committed
Re-add tunnel datagen code, recipes
1 parent c21696c commit d7674a5

File tree

10 files changed

+261
-10
lines changed

10 files changed

+261
-10
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ minecraft {
185185

186186
mods {
187187
compactmachines {
188+
source sourceSets.main
189+
source sourceSets.api
188190
source sourceSets.datagen
189191
}
190192
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package dev.compactmods.machines.api.core;
22

3+
import net.minecraft.resources.ResourceLocation;
4+
35
public abstract class Constants {
46
public static final String MOD_ID = "compactmachines";
7+
8+
public static final ResourceLocation TUNNEL_ID = new ResourceLocation(MOD_ID, "tunnel");
59
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package dev.compactmods.machines.api.tunnels.recipe;
2+
3+
import com.google.common.collect.Lists;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonObject;
6+
import dev.compactmods.machines.api.core.Constants;
7+
import dev.compactmods.machines.api.tunnels.TunnelDefinition;
8+
import net.minecraft.advancements.Advancement;
9+
import net.minecraft.advancements.AdvancementRewards;
10+
import net.minecraft.advancements.CriterionTriggerInstance;
11+
import net.minecraft.advancements.RequirementsStrategy;
12+
import net.minecraft.advancements.critereon.RecipeUnlockedTrigger;
13+
import net.minecraft.data.recipes.FinishedRecipe;
14+
import net.minecraft.resources.ResourceLocation;
15+
import net.minecraft.tags.Tag;
16+
import net.minecraft.world.item.Item;
17+
import net.minecraft.world.item.crafting.Ingredient;
18+
import net.minecraft.world.item.crafting.RecipeSerializer;
19+
import net.minecraft.world.level.ItemLike;
20+
21+
import javax.annotation.Nullable;
22+
import java.util.List;
23+
import java.util.function.Consumer;
24+
25+
public class TunnelRecipeBuilder {
26+
27+
private final List<Ingredient> ingredients = Lists.newArrayList();
28+
private final Advancement.Builder advancement = Advancement.Builder.advancement();
29+
private String group;
30+
private int count;
31+
private ResourceLocation tunnelType;
32+
33+
private TunnelRecipeBuilder(TunnelDefinition definition, int count) {
34+
this.tunnelType = definition.getRegistryName();
35+
this.count = count;
36+
}
37+
38+
public static TunnelRecipeBuilder tunnel(TunnelDefinition definition) {
39+
return new TunnelRecipeBuilder(definition, 1);
40+
}
41+
42+
public static TunnelRecipeBuilder tunnel(TunnelDefinition definition, int count) {
43+
return new TunnelRecipeBuilder(definition, count);
44+
}
45+
46+
public TunnelRecipeBuilder requires(Tag<Item> itemTag) {
47+
return this.requires(Ingredient.of(itemTag));
48+
}
49+
50+
public TunnelRecipeBuilder requires(ItemLike item) {
51+
return this.requires(Ingredient.of(item));
52+
}
53+
54+
public TunnelRecipeBuilder requires(Ingredient ingredient) {
55+
this.ingredients.add(ingredient);
56+
return this;
57+
}
58+
59+
public TunnelRecipeBuilder unlockedBy(String criterionName, CriterionTriggerInstance crit) {
60+
this.advancement.addCriterion(criterionName, crit);
61+
return this;
62+
}
63+
64+
public TunnelRecipeBuilder group(String groupName) {
65+
this.group = groupName;
66+
return this;
67+
}
68+
69+
public void save(Consumer<FinishedRecipe> consumer) {
70+
this.ensureValid(tunnelType);
71+
this.advancement
72+
.parent(new ResourceLocation("recipes/root"))
73+
.addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(TunnelRecipeHelper.getRecipeId(tunnelType)))
74+
.rewards(AdvancementRewards.Builder.recipe(tunnelType))
75+
.requirements(RequirementsStrategy.OR);
76+
77+
consumer.accept(new TunnelRecipeBuilder.Result(
78+
this.tunnelType,
79+
this.count,
80+
this.group == null ? "" : this.group, this.ingredients,
81+
this.advancement,
82+
new ResourceLocation(tunnelType.getNamespace(), "recipes/" + tunnelType.getPath())
83+
));
84+
}
85+
86+
private void ensureValid(ResourceLocation recipeId) {
87+
if (this.advancement.getCriteria().isEmpty()) {
88+
throw new IllegalStateException("No way of obtaining recipe " + recipeId);
89+
}
90+
}
91+
92+
public static class Result implements FinishedRecipe {
93+
private final ResourceLocation tunnelType;
94+
private final int count;
95+
private final String group;
96+
private final List<Ingredient> ingredients;
97+
private final Advancement.Builder advancement;
98+
private final ResourceLocation advancementId;
99+
100+
public Result(ResourceLocation tunnelType, int count, String group, List<Ingredient> ingredients, Advancement.Builder adv, ResourceLocation advId) {
101+
this.tunnelType = tunnelType;
102+
this.count = count;
103+
this.group = group;
104+
this.ingredients = ingredients;
105+
this.advancement = adv;
106+
this.advancementId = advId;
107+
}
108+
109+
@Override
110+
public void serializeRecipeData(JsonObject output) {
111+
if (!this.group.isEmpty()) {
112+
output.addProperty("group", this.group);
113+
}
114+
115+
JsonArray jsonarray = new JsonArray();
116+
117+
for(Ingredient ingredient : this.ingredients) {
118+
jsonarray.add(ingredient.toJson());
119+
}
120+
121+
output.add("ingredients", jsonarray);
122+
123+
JsonObject result = new JsonObject();
124+
result.addProperty("item", Constants.TUNNEL_ID.toString());
125+
if (this.count > 1) {
126+
result.addProperty("count", this.count);
127+
}
128+
129+
JsonObject definition = new JsonObject();
130+
definition.addProperty("id", this.tunnelType.toString());
131+
JsonObject nbt = new JsonObject();
132+
nbt.add("definition", definition);
133+
134+
result.add("nbt", nbt);
135+
136+
output.add("result", result);
137+
}
138+
139+
@Override
140+
public ResourceLocation getId() {
141+
return TunnelRecipeHelper.getRecipeId(tunnelType);
142+
}
143+
144+
@Override
145+
public RecipeSerializer<?> getType() {
146+
return RecipeSerializer.SHAPELESS_RECIPE;
147+
}
148+
149+
@Nullable
150+
@Override
151+
public JsonObject serializeAdvancement() {
152+
return this.advancement.serializeToJson();
153+
}
154+
155+
@Nullable
156+
@Override
157+
public ResourceLocation getAdvancementId() {
158+
return this.advancementId;
159+
}
160+
}
161+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dev.compactmods.machines.api.tunnels.recipe;
2+
3+
import net.minecraft.resources.ResourceLocation;
4+
5+
import javax.annotation.Nonnull;
6+
7+
public abstract class TunnelRecipeHelper {
8+
public static ResourceLocation getRecipeId(@Nonnull ResourceLocation tunnelType) {
9+
return new ResourceLocation(tunnelType.getNamespace(), "tunnels/" + tunnelType.getPath());
10+
}
11+
}

src/datagen/java/dev/compactmods/machines/datagen/RecipeGenerator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package dev.compactmods.machines.datagen;
22

3-
import java.util.Objects;
4-
import java.util.function.Consumer;
3+
import dev.compactmods.machines.api.tunnels.recipe.TunnelRecipeBuilder;
54
import dev.compactmods.machines.config.EnableVanillaRecipesConfigCondition;
65
import dev.compactmods.machines.core.Registration;
6+
import dev.compactmods.machines.core.Tunnels;
77
import net.minecraft.data.DataGenerator;
88
import net.minecraft.data.recipes.FinishedRecipe;
99
import net.minecraft.data.recipes.RecipeProvider;
@@ -16,6 +16,9 @@
1616
import net.minecraftforge.common.Tags;
1717
import net.minecraftforge.common.crafting.ConditionalRecipe;
1818

19+
import java.util.Objects;
20+
import java.util.function.Consumer;
21+
1922
public class RecipeGenerator extends RecipeProvider {
2023
public RecipeGenerator(DataGenerator generatorIn) {
2124
super(generatorIn);
@@ -42,6 +45,14 @@ protected void buildCraftingRecipes(Consumer<FinishedRecipe> consumer) {
4245
.unlockedBy("picked_up_ender_eye", RecipeProvider.has(Items.ENDER_EYE))
4346
.save(consumer);
4447

48+
TunnelRecipeBuilder.tunnel(Tunnels.ITEM_TUNNEL_DEF.get(), 2)
49+
.requires(Tags.Items.CHESTS)
50+
.requires(Items.ENDER_PEARL)
51+
.requires(Items.REDSTONE)
52+
.requires(Items.OBSERVER)
53+
.unlockedBy("observer", RecipeProvider.has(Items.OBSERVER))
54+
.save(consumer);
55+
4556
addMachineRecipes(consumer);
4657
}
4758

src/generated/resources/.cache/cache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ f4bbe5fccc3b629f578958c90b84cce5776618d0 data/compactmachines/advancements/claim
4242
26f679025def0ce9b0a9ec9d0e5f6ee83ea50bc3 data/compactmachines/advancements/how_did_you_get_here.json
4343
d611c78e0b2b590bdbd5b17c37b295fb91739397 data/compactmachines/advancements/recipes/compactmachines/personal_shrinking_device.json
4444
fd7ccf69e4445ba0a2d7b2cd405ebe2ae85be240 data/compactmachines/advancements/recipes/compactmachines/wall.json
45+
4cd4f95ab9c34f71663f434ab2c44a5e6714367a data/compactmachines/advancements/recipes/item.json
4546
2e9d5509d633970b1dd01bd99b535fd107e6e91a data/compactmachines/advancements/root.json
4647
263f05c26cc9e33912e3066c13f04ce060446e34 data/compactmachines/dimension/compact_world.json
4748
3855c655bb8b27076189ac2342d8279264e3fbcf data/compactmachines/dimension_type/compact_world.json
@@ -59,7 +60,8 @@ b613629629931df8e56830f394af5bf22e995e55 data/compactmachines/recipes/machine_la
5960
f75a1729536d4385bab2459216980501e0916ca5 data/compactmachines/recipes/machine_small.json
6061
47e99c608cf5e67d771f545f4d23de7908eb779d data/compactmachines/recipes/machine_tiny.json
6162
670e2e5741a431ddb0bddfa185052b3ee4896d38 data/compactmachines/recipes/personal_shrinking_device.json
63+
07aa4910701983983ae386b987e0bd34ae2618dd data/compactmachines/recipes/tunnels/item.json
6264
8b5b4db0092b3cdc213c7e1044b7bb9f254ea545 data/compactmachines/recipes/wall.json
6365
e24d9d47cc4cb734609cdf48a51979b4361901d0 data/compactmachines/worldgen/biome/machine.json
64-
40812eea3eddfef948da8f16d0b64db55563e74c data/minecraft/tags/blocks/mineable/pickaxe.json
65-
40812eea3eddfef948da8f16d0b64db55563e74c data/minecraft/tags/blocks/needs_iron_tool.json
66+
1a029ef9181520c3b2280c9a3f8f42a580333502 data/minecraft/tags/blocks/mineable/pickaxe.json
67+
1a029ef9181520c3b2280c9a3f8f42a580333502 data/minecraft/tags/blocks/needs_iron_tool.json
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"parent": "minecraft:recipes/root",
3+
"rewards": {
4+
"recipes": [
5+
"compactmachines:item"
6+
]
7+
},
8+
"criteria": {
9+
"observer": {
10+
"trigger": "minecraft:inventory_changed",
11+
"conditions": {
12+
"items": [
13+
{
14+
"items": [
15+
"minecraft:observer"
16+
]
17+
}
18+
]
19+
}
20+
},
21+
"has_the_recipe": {
22+
"trigger": "minecraft:recipe_unlocked",
23+
"conditions": {
24+
"recipe": "compactmachines:tunnels/item"
25+
}
26+
}
27+
},
28+
"requirements": [
29+
[
30+
"observer",
31+
"has_the_recipe"
32+
]
33+
]
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"type": "minecraft:crafting_shapeless",
3+
"ingredients": [
4+
{
5+
"tag": "forge:chests"
6+
},
7+
{
8+
"item": "minecraft:ender_pearl"
9+
},
10+
{
11+
"item": "minecraft:redstone"
12+
},
13+
{
14+
"item": "minecraft:observer"
15+
}
16+
],
17+
"result": {
18+
"item": "compactmachines:tunnel",
19+
"count": 2,
20+
"nbt": {
21+
"definition": {
22+
"id": "compactmachines:item"
23+
}
24+
}
25+
}
26+
}

src/generated/resources/data/minecraft/tags/blocks/mineable/pickaxe.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"replace": false,
33
"values": [
44
"compactmachines:wall",
5-
"compactmachines:machine_normal",
5+
"compactmachines:machine_large",
66
"compactmachines:machine_maximum",
77
"compactmachines:machine_giant",
8-
"compactmachines:machine_small",
98
"compactmachines:machine_tiny",
10-
"compactmachines:machine_large"
9+
"compactmachines:machine_small",
10+
"compactmachines:machine_normal"
1111
]
1212
}

src/generated/resources/data/minecraft/tags/blocks/needs_iron_tool.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"replace": false,
33
"values": [
44
"compactmachines:wall",
5-
"compactmachines:machine_normal",
5+
"compactmachines:machine_large",
66
"compactmachines:machine_maximum",
77
"compactmachines:machine_giant",
8-
"compactmachines:machine_small",
98
"compactmachines:machine_tiny",
10-
"compactmachines:machine_large"
9+
"compactmachines:machine_small",
10+
"compactmachines:machine_normal"
1111
]
1212
}

0 commit comments

Comments
 (0)