Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public enum DataGenType {

BANNER_PATTERNS("banner_pattern", new GenericResourceGenerator("banner_pattern")),
BIOMES("worldgen/biome", new GenericResourceGenerator("worldgen/biome")),
BLOCK_STATE_PROVIDERS("worldgen/block_state_provider", new GenericResourceGenerator("worldgen/block_state_provider")),
BLOCK_TRANSFORMERS("block_transformer", new GenericResourceGenerator("block_transformer")),
CAT_VARIANTS("cat_variant", new GenericResourceGenerator("cat_variant")),
CAT_SOUND_VARIANTS("cat_sound_variant", new GenericResourceGenerator("cat_sound_variant")),
CHAT_TYPES("chat_type", new GenericResourceGenerator("chat_type")),
Expand All @@ -58,6 +60,7 @@ public enum DataGenType {
COW_VARIANTS("cow_variant", new GenericResourceGenerator("cow_variant")),
COW_SOUND_VARIANTS("cow_sound_variant", new GenericResourceGenerator("cow_sound_variant")),
DAMAGE_TYPES("damage_type", new GenericResourceGenerator("damage_type")),
DECORATED_POT_PATTERNS("decorated_pot_pattern", new GenericResourceGenerator("decorated_pot_pattern")),
DIALOGS("dialog", new GenericResourceGenerator("dialog")),
DIMENSION_TYPES("dimension_type", new GenericResourceGenerator("dimension_type")),
ENCHANTMENTS("enchantment", new GenericResourceGenerator("enchantment")),
Expand All @@ -67,6 +70,7 @@ public enum DataGenType {
PAINTING_VARIANTS("painting_variant", new GenericResourceGenerator("painting_variant")),
PIG_VARIANTS("pig_variant", new GenericResourceGenerator("pig_variant")),
PIG_SOUND_VARIANTS("pig_sound_variant", new GenericResourceGenerator("pig_sound_variant")),
PREDICATES("predicate", new GeneratedResourceGenerator("predicate")),
SULFUR_CUBE_ARCHETYPE("sulfur_cube_archetype", new GenericResourceGenerator("sulfur_cube_archetype")),
TIMELINE("timeline", new GenericResourceGenerator("timeline")),
TRIM_MATERIALS("trim_material", new GenericResourceGenerator("trim_material")),
Expand Down Expand Up @@ -98,4 +102,4 @@ public String getFileName() {
public DataGenerator getGenerator() {
return generator;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.minestom.datagen;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import net.minecraft.SharedConstants;
Expand All @@ -10,6 +11,7 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;

Expand Down Expand Up @@ -55,22 +57,22 @@ protected void addDefaultable(JsonObject jsonObject, String key, double value, d

protected JsonObject mergePath(Path directory) {
final JsonObject result = new JsonObject();
try {
Files.walk(directory).filter(Files::isRegularFile)
try (var paths = Files.walk(directory)) {
paths.filter(Files::isRegularFile)
.forEach(path -> {
try {
JsonObject blockLootTable = DataGen.GSON.fromJson(new JsonReader(Files.newBufferedReader(path)), JsonObject.class);
try (var reader = Files.newBufferedReader(path);
var jsonReader = new JsonReader(reader)) {
JsonElement value = DataGen.GSON.fromJson(jsonReader, JsonElement.class);
final String fileName = directory.relativize(path).toString();
final String tableName = (File.separatorChar == '\\' ? fileName.replace('\\', '/') : fileName)
.replace(".json", "");
result.add("minecraft:" + tableName, blockLootTable);
result.add("minecraft:" + tableName, value);
} catch (IOException e) {
LOGGER.error("Failed to read block loot table located at '" + path + "'.", e);
e.printStackTrace();
throw new UncheckedIOException("Failed to read generated resource '" + path + "'", e);
}
});
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException("Failed to read generated resource directory '" + directory + "'", e);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,12 @@ private void writeState(Identifier location, Block block, Map<String, SoundType>
appendState(blockJson, state, "occludes", blockState.canOcclude(), boolean.class);
appendState(blockJson, state, "requiresTool", blockState.requiresCorrectToolForDrops(), boolean.class);

appendState(blockJson, state, "blocksMotion", blockState.blocksMotion(), boolean.class);
appendState(blockJson, state, "flammable", isFlammable(blockState), boolean.class);
appendState(blockJson, state, "air", blockState.isAir(), false, boolean.class);
appendState(blockJson, state, "liquid", blockState.liquid(), false, boolean.class);
appendState(blockJson, state, "fluid", !blockState.getFluidState().isEmpty(), false, boolean.class);
appendState(blockJson, state, "replaceable", blockState.canBeReplaced(), false, boolean.class);
appendState(blockJson, state, "solid", blockState.isSolid(), boolean.class);
appendState(blockJson, state, "solidBlocking", blockState.blocksMotion(), boolean.class);
appendState(blockJson, state, "lightBlock", blockState.getLightDampening(), int.class);
// Sounds
SoundType soundType = blockState.getSoundType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.minestom.generators;

import com.google.gson.JsonObject;
import net.minestom.datagen.DataGenerator;
import org.jetbrains.annotations.NotNull;

public final class GeneratedResourceGenerator extends DataGenerator {
private final String name;

public GeneratedResourceGenerator(@NotNull String name) {
this.name = name;
}

@Override
public JsonObject generate() {
var result = mergePath(DATA_FOLDER.resolve(name));
if (result.isEmpty()) {
throw new IllegalStateException("Generated resource directory is empty: " + name);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ public Object generate() throws Exception {
// only collect valid files
if (content.length() > 0 && fileName.endsWith(".json")) {
var key = "minecraft:" + fileName.substring(0, fileName.length() - 5);
var jsonObject = gson.fromJson(content.toString(), JsonObject.class);
exclusions.forEach(jsonObject::remove);
var json = gson.fromJson(content.toString(), JsonElement.class);
if (json instanceof JsonObject jsonObject) {
exclusions.forEach(jsonObject::remove);
}

result.add(key, jsonObject);
result.add(key, json);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public JsonObject generate() {
}

private static HolderLookup.Provider applyPendingComponents() {
HolderLookup.Provider lookup = VanillaRegistries.createLookup();
HolderLookup.Provider lookup = VanillaRegistries.createWorldLookup();
var registry = BuiltInRegistries.DATA_COMPONENT_INITIALIZERS.build(lookup);
registry.forEach(DataComponentInitializers.PendingComponents::apply);
return lookup;
Expand Down
26 changes: 14 additions & 12 deletions DataGenerator/src/main/java/net/minestom/utils/ResourceUtils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package net.minestom.utils;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.jar.JarFile;

Expand All @@ -16,16 +17,22 @@ private ResourceUtils() {}
* Get a resource listing from within a jar file
* @param clazz The class to use the {@link ClassLoader} of
* @param path The path within the classpath to list resources from
* @return An array containing all the path of all resources within the specified directory
* @return An array containing the path of all resources within the specified directory,
* relative to it and including any nested directories
* @throws URISyntaxException
* @throws IOException
*/
public static String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException {
var dirURL = clazz.getClassLoader().getResource(path);

// list use File#list in case path is just a regular file
// walk the directory in case path is just a regular file
if (dirURL != null && dirURL.getProtocol().equals("file")) {
return new File(dirURL.toURI()).list();
var root = Path.of(dirURL.toURI());
try (var files = Files.walk(root)) {
return files.filter(Files::isRegularFile)
.map(file -> root.relativize(file).toString())
.toArray(String[]::new);
}
}

if (dirURL == null) {
Expand All @@ -43,17 +50,12 @@ public static String[] getResourceListing(Class clazz, String path) throws URISy
try (var jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8))) {
// get all files within the jar
var entries = jar.entries();
var result = new HashSet<>();
var result = new HashSet<String>();

while(entries.hasMoreElements()) {
var name = entries.nextElement().getName();
if (name.startsWith(path)) {
var entry = name.substring(path.length());
int checkSubdir = entry.indexOf("/");
if (checkSubdir >= 0) {
entry = entry.substring(0, checkSubdir);
}
result.add(entry);
if (name.startsWith(path) && !name.endsWith("/")) {
result.add(name.substring(path.length()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ metadata.format.version = "1.1"

[versions]
gson = "2.14.0"
minecraft = "26.2"
minecraft = "26.3"

loom = "1.16-SNAPSHOT"
nmcp = "1.6.2"
Expand Down