-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCodes.java
451 lines (405 loc) · 22.5 KB
/
Codes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package org.polaris2023.utils;
import org.polaris2023.processor.InitProcessor;
import javax.annotation.processing.Filer;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.Writer;
public enum Codes {
ModelProvider(
"""
package %%package%%;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SlabBlock;
import net.minecraft.world.level.block.StairBlock;
import net.neoforged.neoforge.registries.DeferredHolder;
import org.polaris2023.wild_wind.common.init.ModInitializer;
import org.polaris2023.wild_wind.util.interfaces.IModel;
import java.nio.file.Path;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
public final class %%classname%% implements DataProvider, IModel<%%classname%%> {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().setLenient().create();
private static final Map<String, String> SPAWN_EGG = Map.of("parent", "minecraft:item/template_spawn_egg");
private PackOutput output;
private String modid;
private Path assetsDir;
private final ConcurrentHashMap<ResourceLocation, Object> MODELS =
new ConcurrentHashMap<>();// object is Bean or map, by gson
private final ConcurrentHashMap<ResourceLocation, Object> BLOCKSTATES =
new ConcurrentHashMap<>();
private <T extends Item> %%classname%% basicItem(Supplier<T> item) {
ResourceLocation key = BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/");
MODELS.put(key, Map.of("parent", "minecraft:item/generated", "textures", Map.of(
"layer0", key.toString()
)));
return this;
}
private <T extends Item> %%classname%% basicItem(Supplier<T> item, String prefix) {
MODELS.put(BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/" + prefix + "_"), Map.of("parent", "minecraft:item/generated", "textures", Map.of(
"layer0", BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/").toString()
)));
return this;
}
private <T extends Item> %%classname%% basicItem(Supplier<T> item, Map<String, Object> display) {
ResourceLocation key = BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/");
MODELS.put(key, Map.of("parent", "minecraft:item/generated", "textures", Map.of(
"layer0", key.toString()
), "display", display));
return this;
}
private <T extends Item> %%classname%% basicItem(Supplier<T> item, Map<String, Object> display, String prefix) {
MODELS.put(BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/" + prefix + "_"), Map.of("parent", "minecraft:item/generated", "textures", Map.of(
"layer0", BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/").toString()
), "display", display));
return this;
}
private <T extends Item> %%classname%% basicItem(Supplier<T> item, Map<String, Object> display, List<Object> overrides) {
ResourceLocation key = BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/");
MODELS.put(key, Map.of("parent", "minecraft:item/generated", "textures", Map.of(
"layer0", key.toString()
), "display", display, "overrides", overrides));
return this;
}
private <T extends Item> %%classname%% basicItem(Supplier<T> item, Map<String, Object> display, List<Object> overrides, String prefix) {
MODELS.put(BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/" + prefix + "_"), Map.of("parent", "minecraft:item/generated", "textures", Map.of(
"layer0", BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/").toString()
), "display", display, "overrides", overrides));
return this;
}
private <T extends BlockItem> %%classname%% basicBlockItem(Supplier<T> blockItem) {
ResourceLocation key = BuiltInRegistries.ITEM.getKey(blockItem.get());
ResourceLocation blockKey = key.withPrefix("block/");
ResourceLocation itemKey = key.withPrefix("item/");
MODELS.put(itemKey, Map.of("parent", blockKey.toString()));
return this;
}
private <T extends Block> %%classname%% cubeAll(Supplier<T> block) {
ResourceLocation key = BuiltInRegistries.BLOCK.getKey(block.get());
ResourceLocation blockKey = key.withPrefix("block/");
MODELS.put(blockKey, Map.of(
"parent", "minecraft:block/cube_all",
"textures", Map.of(
"all", blockKey.toString()
)
));
BLOCKSTATES.put(key, Map.of(
"variants", Map.of(
"", Map.of(
"model", blockKey.toString()
)
)
));
return this;
}
private <T extends Block> %%classname%% stairsBlock(Supplier<T> block, String bottom, String side, String top) {
ResourceLocation key = BuiltInRegistries.BLOCK.getKey(block.get());
ResourceLocation blockKey = key.withPrefix("block/");
ResourceLocation inner = blockKey.withSuffix("_inner");
ResourceLocation outer = blockKey.withSuffix("_outer");
MODELS.put(blockKey, Map.of(
"parent", "minecraft:block/stairs",
"textures", Map.of(
"bottom", bottom.isEmpty() ? blockKey.toString() : bottom,
"side", side.isEmpty() ? blockKey.toString() : side,
"top", top.isEmpty() ? blockKey.toString() : top
)));
MODELS.put(inner, Map.of(
"parent", "minecraft:block/inner_stairs",
"textures", Map.of(
"bottom", bottom.isEmpty() ? blockKey.toString() : bottom,
"side", side.isEmpty() ? blockKey.toString() : side,
"top", top.isEmpty() ? blockKey.toString() : top
)));
MODELS.put(outer, Map.of(
"parent", "minecraft:block/outer_stairs",
"textures", Map.of(
"bottom", bottom.isEmpty() ? blockKey.toString() : bottom,
"side", side.isEmpty() ? blockKey.toString() : side,
"top", top.isEmpty() ? blockKey.toString() : top
)));
BLOCKSTATES.put(key, Map.of(
"variants", Map.of(
"facing=east,half=bottom,shape=inner_left", Map.of(
"model", inner.toString(),
"uvlock", true,
"y", 270
),
"facing=east,half=bottom,shape=inner_right", Map.of(
"model", inner.toString()
),
"facing=east,half=bottom,shape=outer_left", Map.of(
"model", outer.toString(),
"uvlock", true,
"y", 270
),
"facing=east,half=bottom,shape=outer_right", Map.of(
"model", outer.toString()
),
"facing=east,half=bottom,shape=straight", Map.of(
"model", blockKey.toString()
),
"facing=east,half= top,shape=inner_left", Map.of(
"model", inner.toString(),
"uvlock", true,
"x", 180
),
"facing=east,half=top,shape=inner_right", Map.of(
"model", inner.toString(),
"uvlock", true,
"x", 180,
"y", 90
),
"facing=east,half=top,shape=outer_left", Map.of(
"model", outer.toString(),
"uvlock", true,
"z", 180
),
"facing=east,half=top,shape=outer_right", Map.of(
"model", outer.toString(),
"uvlock", true,
"x", 180,
"y", 90
),
"facing=east,half= top,shape=straight", Map.of(
"model", blockKey.toString(),
"uvlock", true,
"x", 180
)
)
));
return this;
}
private <T extends Block> %%classname%% slabBlock(Supplier<T> block, String bottom, String side, String top) {
ResourceLocation key = BuiltInRegistries.BLOCK.getKey(block.get()).withPrefix("block/");
MODELS.put(key, Map.of(
"parent", "minecraft:block/slab",
"textures", Map.of(
"bottom", bottom.isEmpty() ? key.toString() : bottom,
"side", side.isEmpty() ? key.toString() : side,
"top", top.isEmpty() ? key.toString() : top
)));
MODELS.put(key.withSuffix("_top"), Map.of(
"parent", "minecraft:block/slab_top",
"textures", Map.of(
"bottom", bottom.isEmpty() ? key.toString() : bottom,
"side", side.isEmpty() ? key.toString() : side,
"top", top.isEmpty() ? key.toString() : top
)));
return this;
}
private <T extends Block> %%classname%% buttonBlock(Supplier<T> block, String texture) {
ResourceLocation key = BuiltInRegistries.BLOCK.getKey(block.get()).withPrefix("block/");
MODELS.put(key, Map.of(
"parent", "minecraft:block/button",
"textures", Map.of("texture", texture.isEmpty() ? key.toString(): texture)));
MODELS.put(key.withSuffix("_inventory"), Map.of(
"parent", "minecraft:block/button_inventory",
"textures", Map.of("texture", texture.isEmpty() ? key.toString(): texture)));
MODELS.put(key.withSuffix("_pressed"), Map.of(
"parent", "minecraft:block/button_pressed",
"textures", Map.of("texture", texture.isEmpty() ? key.toString(): texture)));
return this;
}
private <T extends Item> %%classname%% parentItem(
Supplier<T> supplier,
String parent,
String... textures
) {
ResourceLocation key = BuiltInRegistries.ITEM.getKey(supplier.get()).withPrefix("item/");
Map<String, String> texturesMap = new HashMap<>();
for (int i = 0; i < textures.length - 1; i+=2) {
texturesMap.put(textures[i], textures[i + 1]);
}
Map map = new HashMap<>();
map.put("parent", parent);
if (!texturesMap.isEmpty()) {
map.put("textures", texturesMap);
}
MODELS.put(key, map);
return this;
}
private <T extends SpawnEggItem> %%classname%% spawnEggItem(Supplier<T> item) {
MODELS.put(BuiltInRegistries.ITEM.getKey(item.get()).withPrefix("item/"), SPAWN_EGG);
return this;
}
@Override
public void init() {
%%init%%;
}
@Override
public %%classname%% setOutput(PackOutput output) {
this.output = output;
assetsDir = output.getOutputFolder(PackOutput.Target.RESOURCE_PACK);
return this;
}
@Override
public CompletableFuture<?> run(CachedOutput output) {
init();
CompletableFuture<?>[] futures = new CompletableFuture[MODELS.size() + BLOCKSTATES.size()];
int i = 0;
for (Map.Entry<ResourceLocation, Object> entry : MODELS.entrySet()) {
ResourceLocation key = entry.getKey();
Object object = entry.getValue();
Path itemModel = assetsDir.resolve(key.getNamespace()).resolve("models").resolve(key.getPath() + ".json");
JsonElement jsonTree = GSON.toJsonTree(object);
futures[i] = DataProvider.saveStable(output, jsonTree, itemModel);
i++;
}
for(Map.Entry<ResourceLocation, Object> entry : BLOCKSTATES.entrySet()) {
ResourceLocation key = entry.getKey();
Object object = entry.getValue();
Path states = assetsDir.resolve(key.getNamespace()).resolve("blockstates").resolve(key.getPath() + ".json");
JsonElement jsonTree = GSON.toJsonTree(object);
futures[i] = DataProvider.saveStable(output, jsonTree, states);
i++;
}
return CompletableFuture.allOf(futures);
}
@Override
public String getName() {
return "Model Provider by " + modid;
}
@Override
public %%classname%% setModid(String modid) {
this.modid = modid;
return this;
}
}
""".stripIndent(),
"org.polaris2023.wild_wind.datagen.custom",
"ModelProviderWildWind"
),
LanguageProvider("""
package %%package%%;
import com.google.gson.Gson;
import java.lang.Object;
import java.lang.String;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import org.polaris2023.wild_wind.util.interfaces.ILanguage;
import net.neoforged.neoforge.registries.DeferredHolder;
import java.util.function.Supplier;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.entity.EntityType;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.sounds.SoundEvent;
public final class %%classname%% implements ILanguage<%%classname%%>, DataProvider {
private static final Gson GSON = new com.google.gson.GsonBuilder().setLenient().setPrettyPrinting().create();
private PackOutput output;
private final ConcurrentHashMap<String, ConcurrentHashMap<String, String>> languages = new ConcurrentHashMap<>();
private String targetLanguage = targetLanguage = "en_us";
private Path languageDir;
private String modid;
public %%classname%% setOutput(PackOutput output) {
this.output = output;
languageDir = output
.getOutputFolder(PackOutput.Target.RESOURCE_PACK)
.resolve(modid)
.resolve("lang");
return this;
}
public CompletableFuture<?> run(CachedOutput cachedOutput) {
init();
final CompletableFuture<?>[] futures = new CompletableFuture[languages.size()];
int i = 0;
for (var entry : languages.entrySet()) {
String lang = entry.getKey();
ConcurrentHashMap<String, String> kv = entry.getValue();
Path json = languageDir.resolve(lang + ".json");
com.google.gson.JsonElement jsonTree = GSON.toJsonTree(kv);
futures[i] = DataProvider.saveStable(cachedOutput, jsonTree, json);
i++;
}
return CompletableFuture.allOf(futures);
}
public %%classname%% setTargetLanguage(String targetLanguage) {
this.targetLanguage = targetLanguage;
return this;
}
public String getName() {
return "Language Setting by " + modid;
}
public %%classname%% add(Object r, String value) {
if (r instanceof String string) {
if (!languages.containsKey(targetLanguage)) languages.put(targetLanguage, new ConcurrentHashMap<>());
languages.get(targetLanguage).put(string, value);
return this;
}
return switch (r) {
case DeferredHolder<?, ?> holder -> add(holder.get(), value);
case Supplier<?> supplier -> add(supplier.get(), value);
case Item item -> add(item.getDescriptionId(), value);
case Block block -> add(block.getDescriptionId(), value);
case SoundEvent sound -> add("sound." + sound.getLocation().toString().replace(":", "."), value);
case EntityType<?> type -> add(type.getDescriptionId(), value);
case TranslatableContents contents -> add(contents.getKey(), value);
case MobEffect effect -> add(effect.getDescriptionId(), value);
case CreativeModeTab tab -> tab.getDisplayName().getContents() instanceof TranslatableContents contents ?
add(contents, value) :
this;
case ItemStack stack -> add(stack.getDescriptionId(), value);
default -> throw new IllegalStateException("Unexpected value: " + r);
};
}
public %%classname%% setModid(String modid) {
this.modid = modid;
return this;
}
public void init() {
%%init%%;
}
}
""".stripIndent(),
"org.polaris2023.wild_wind.datagen.custom",
"LanguageProviderWildWind"
);
private final String code;
private final String packageName;
private final String classname;
Codes(String code, String packageName, String classname) {
this.code = code;
this.packageName = packageName;
this.classname = classname;
}
public String code() {
return code;
}
public void saveAndAddServiceCode(Filer filer, String services_className, Object init) {
try {
String qName = "%s.%s".formatted(packageName, classname);
JavaFileObject sourceFile = filer.createSourceFile(qName);
try(Writer writer = sourceFile.openWriter()) {
writer.write(code
.replace("%%classname%%", classname)
.replace("%%package%%", packageName)
.replace("%%init%%", init.toString()));
}
InitProcessor.add(services_className, qName);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}