Skip to content

Commit f59ac08

Browse files
committed
Add wood API
This adds a KubeJS API that permits KubeJS to register new woodtypes that GTCEu will generate recipes and materials for TODO: Add examples
1 parent a7bab49 commit f59ac08

File tree

3 files changed

+308
-1
lines changed

3 files changed

+308
-1
lines changed

src/main/java/com/gregtechceu/gtceu/data/recipe/misc/WoodMachineRecipes.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.gregtechceu.gtceu.data.material.GTMaterials;
1414
import com.gregtechceu.gtceu.data.recipe.VanillaRecipeHelper;
1515
import com.gregtechceu.gtceu.data.recipe.WoodTypeEntry;
16+
import com.gregtechceu.gtceu.integration.kjs.GTCEuStartupEvents;
17+
import com.gregtechceu.gtceu.integration.kjs.events.RegisterWoodsKubeEvent;
1618

1719
import net.minecraft.data.recipes.RecipeOutput;
1820
import net.minecraft.resources.ResourceLocation;
@@ -25,6 +27,7 @@
2527

2628
import org.jetbrains.annotations.NotNull;
2729

30+
import java.util.ArrayList;
2831
import java.util.Arrays;
2932
import java.util.List;
3033
import java.util.function.Consumer;
@@ -44,6 +47,7 @@ public static void init(RecipeOutput provider) {
4447
}
4548

4649
private static List<WoodTypeEntry> DEFAULT_ENTRIES;
50+
private static List<WoodTypeEntry> CUSTOM_ENTRIES;
4751

4852
private static List<WoodTypeEntry> getDefaultEntries() {
4953
if (DEFAULT_ENTRIES == null) {
@@ -306,7 +310,17 @@ private static List<WoodTypeEntry> getDefaultEntries() {
306310
.registerMaterialInfo(false, true, true, true, true, true, true, true, true, true)
307311
.build());
308312
}
309-
return DEFAULT_ENTRIES;
313+
if (CUSTOM_ENTRIES == null) {
314+
CUSTOM_ENTRIES = new ArrayList<WoodTypeEntry>();
315+
var evt = new RegisterWoodsKubeEvent();
316+
GTCEuStartupEvents.REGISTER_WOODS.post(evt);
317+
CUSTOM_ENTRIES = new ArrayList<WoodTypeEntry>(evt.woods);
318+
}
319+
320+
List<WoodTypeEntry> entries = new ArrayList<WoodTypeEntry>();
321+
entries.addAll(DEFAULT_ENTRIES);
322+
entries.addAll(CUSTOM_ENTRIES);
323+
return entries;
310324
}
311325

312326
public static void registerMaterialInfo() {

src/main/java/com/gregtechceu/gtceu/integration/kjs/GTCEuStartupEvents.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ public interface GTCEuStartupEvents {
1515
EventHandler MATERIAL_MODIFICATION = GROUP.startup("materialModification",
1616
() -> MaterialModificationKubeEvent.class);
1717
EventHandler CRAFTING_COMPONENTS = GROUP.startup("craftingComponents", () -> CraftingComponentsKubeEvent.class);
18+
19+
EventHandler REGISTER_WOODS = GROUP.startup("registerWoods", () -> RegisterWoodsKubeEvent.class);
1820
}
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
package com.gregtechceu.gtceu.integration.kjs.events;
2+
3+
import com.gregtechceu.gtceu.GTCEu;
4+
import com.gregtechceu.gtceu.data.recipe.WoodTypeEntry;
5+
6+
import net.minecraft.world.item.Item;
7+
import net.minecraft.world.item.Items;
8+
9+
import dev.latvian.mods.kubejs.event.EventResult;
10+
import dev.latvian.mods.kubejs.event.KubeEvent;
11+
import dev.latvian.mods.rhino.util.HideFromJS;
12+
13+
import java.util.ArrayList;
14+
15+
public class RegisterWoodsKubeEvent implements KubeEvent {
16+
17+
public RegisterWoodsKubeEvent() {
18+
this.woods = new ArrayList<>();
19+
this.wrapped = new ArrayList<>();
20+
}
21+
22+
@HideFromJS
23+
public ArrayList<WoodTypeEntry> woods;
24+
@HideFromJS
25+
public ArrayList<Wrapped> wrapped;
26+
27+
public class Wrapped {
28+
29+
RegisterWoodsKubeEvent evt;
30+
String modId;
31+
String woodName;
32+
33+
@HideFromJS
34+
Wrapped(String modId, String woodName) {
35+
this.modId = modId;
36+
this.woodName = woodName;
37+
}
38+
39+
// public Wrapped id(KubeResourceLocation _id) {
40+
// String namespace = id.wrapped().getNamespace();
41+
// // if (namespace.equals("kubejs")) {
42+
// // namespace = this.type.id.getNamespace();
43+
// // }
44+
// var idWithoutType = ResourceLocation.fromNamespaceAndPath(namespace,
45+
// _id.wrapped().getPath());
46+
// var id = idWithoutType.withPrefix(this.type.id.getPath() + "/");
47+
// return this;
48+
// }
49+
50+
private String _recipeId;
51+
private Item _plank;
52+
private Item _strippedLog;
53+
private Item _strippedWood;
54+
private Item _wood;
55+
private Item _log;
56+
private Item _door;
57+
private Item _trapdoor;
58+
private Item _slab;
59+
private Item _fence;
60+
private Item _fenceGate;
61+
private Item _stairs;
62+
private Item _boat;
63+
private Item _chestBoat;
64+
private Item _sign;
65+
private Item _hangingSign;
66+
private Item _button;
67+
private Item _pressurePlate;
68+
69+
public Wrapped recipeId(String recipeId) {
70+
this._recipeId = recipeId;
71+
return this;
72+
}
73+
74+
public Wrapped plank(Item plank) {
75+
this._plank = plank;
76+
return this;
77+
}
78+
79+
public Wrapped strippedLog(Item strippedLog) {
80+
this._strippedLog = strippedLog;
81+
return this;
82+
}
83+
84+
public Wrapped strippedWood(Item strippedWood) {
85+
this._strippedWood = strippedWood;
86+
return this;
87+
}
88+
89+
public Wrapped wood(Item wood) {
90+
this._wood = wood;
91+
return this;
92+
}
93+
94+
public Wrapped log(Item log) {
95+
this._log = log;
96+
return this;
97+
}
98+
99+
public Wrapped door(Item door) {
100+
this._door = door;
101+
return this;
102+
}
103+
104+
public Wrapped trapdoor(Item trapdoor) {
105+
this._trapdoor = trapdoor;
106+
return this;
107+
}
108+
109+
public Wrapped slab(Item slab) {
110+
this._slab = slab;
111+
return this;
112+
}
113+
114+
public Wrapped fence(Item fence) {
115+
this._fence = fence;
116+
return this;
117+
}
118+
119+
public Wrapped fenceGate(Item fenceGate) {
120+
this._fenceGate = fenceGate;
121+
return this;
122+
}
123+
124+
public Wrapped stairs(Item stairs) {
125+
this._stairs = stairs;
126+
return this;
127+
}
128+
129+
public Wrapped boat(Item boat) {
130+
this._boat = boat;
131+
return this;
132+
}
133+
134+
public Wrapped chestBoat(Item chestBoat) {
135+
this._chestBoat = chestBoat;
136+
return this;
137+
}
138+
139+
public Wrapped sign(Item sign) {
140+
this._sign = sign;
141+
return this;
142+
}
143+
144+
public Wrapped hangingSign(Item hangingSign) {
145+
this._hangingSign = hangingSign;
146+
return this;
147+
}
148+
149+
public Wrapped button(Item button) {
150+
this._button = button;
151+
return this;
152+
}
153+
154+
public Wrapped pressurePlate(Item pressurePlate) {
155+
this._pressurePlate = pressurePlate;
156+
return this;
157+
}
158+
159+
@HideFromJS
160+
public WoodTypeEntry toEntry() throws IllegalArgumentException {
161+
if (this.modId == null) {
162+
throw new IllegalArgumentException("Need a modId");
163+
}
164+
if (this.woodName == null) {
165+
throw new IllegalArgumentException("Need a woodName");
166+
}
167+
if (this._recipeId == null) {
168+
throw new IllegalArgumentException("Need a recipeId");
169+
}
170+
171+
if (this._plank == null) {
172+
throw new IllegalArgumentException("Need a plank");
173+
}
174+
175+
if (this._strippedLog == null) {
176+
throw new IllegalArgumentException("Need a strippedLog");
177+
}
178+
179+
if (this._strippedWood == null) {
180+
throw new IllegalArgumentException("Need a strippedWood");
181+
}
182+
183+
if (this._wood == null) {
184+
throw new IllegalArgumentException("Need a wood");
185+
}
186+
187+
if (this._log == null) {
188+
throw new IllegalArgumentException("Need a log");
189+
}
190+
191+
if (this._door == null) {
192+
throw new IllegalArgumentException("Need a door");
193+
}
194+
195+
if (this._trapdoor == null) {
196+
throw new IllegalArgumentException("Need a trapdoor");
197+
}
198+
199+
if (this._slab == null) {
200+
throw new IllegalArgumentException("Need a slab");
201+
}
202+
203+
if (this._fence == null) {
204+
throw new IllegalArgumentException("Need a fence");
205+
}
206+
207+
if (this._fenceGate == null) {
208+
throw new IllegalArgumentException("Need a fenceGate");
209+
}
210+
211+
if (this._stairs == null) {
212+
throw new IllegalArgumentException("Need a stairs");
213+
}
214+
215+
if (this._boat == null) {
216+
throw new IllegalArgumentException("Need a boat");
217+
}
218+
219+
if (this._chestBoat == null) {
220+
throw new IllegalArgumentException("Need a chestBoat");
221+
}
222+
223+
if (this._sign == null) {
224+
throw new IllegalArgumentException("Need a sign");
225+
}
226+
227+
if (this._hangingSign == null) {
228+
throw new IllegalArgumentException("Need a hangingSign");
229+
}
230+
231+
if (this._button == null) {
232+
throw new IllegalArgumentException("Need a button");
233+
}
234+
235+
if (this._pressurePlate == null) {
236+
throw new IllegalArgumentException("Need a pressurePlate");
237+
}
238+
239+
var builder = new WoodTypeEntry.Builder(this.modId, this.woodName)
240+
.planks(this._plank, this._recipeId + "_planks")
241+
.log(Items.OAK_LOG).removeCharcoalRecipe()
242+
.strippedLog(this._strippedLog)
243+
.wood(this._wood)
244+
.strippedWood(this._strippedWood)
245+
.door(this._door, this._recipeId + "_door")
246+
.trapdoor(this._trapdoor, this._recipeId + "_trapdoor")
247+
.slab(this._slab, this._recipeId + "_slab")
248+
.fence(this._fence, this._recipeId + "_fence")
249+
.fenceGate(this._fenceGate, this._recipeId + "_fence_gate")
250+
.stairs(this._stairs, this._recipeId + "_stairs")
251+
.boat(this._boat, this._recipeId + "_boat")
252+
.chestBoat(this._chestBoat, this._recipeId + "_chest_boat")
253+
.sign(this._sign, this._recipeId + "_sign")
254+
.hangingSign(this._hangingSign, this._recipeId + "_hanging_sign")
255+
.button(this._button, this._recipeId + "_button")
256+
.pressurePlate(this._pressurePlate, this._recipeId + "_pressure_plate")
257+
.registerAllMaterialInfo()
258+
.build();
259+
return builder;
260+
}
261+
262+
boolean wasRegisted = false;
263+
264+
@HideFromJS
265+
public void register() {
266+
if (!this.wasRegisted) {
267+
try {
268+
woods.add(this.toEntry());
269+
} catch (Exception e) {
270+
GTCEu.LOGGER.error(e);
271+
272+
}
273+
} else
274+
GTCEu.LOGGER.warn("Tried registering a wood type twice!");
275+
}
276+
};
277+
278+
public Wrapped register(String modId, String woodName) {
279+
var wrap = new Wrapped(modId, woodName);
280+
this.wrapped.add(wrap);
281+
return wrap;
282+
}
283+
284+
@Override
285+
public void afterPosted(EventResult result) {
286+
for (var wrapped : this.wrapped) {
287+
wrapped.register();
288+
}
289+
KubeEvent.super.afterPosted(result);
290+
}
291+
}

0 commit comments

Comments
 (0)