mirror of
https://github.com/xSquishyLiam/mc-GeyserModelEnginePackGenerator-extension.git
synced 2025-12-20 15:29:24 +00:00
sub folder support
This commit is contained in:
@@ -5,41 +5,33 @@ import org.geysermc.event.subscribe.Subscribe;
|
||||
import org.geysermc.geyser.api.event.lifecycle.GeyserLoadResourcePacksEvent;
|
||||
import org.geysermc.geyser.api.event.lifecycle.GeyserPreInitializeEvent;
|
||||
import org.geysermc.geyser.api.extension.Extension;
|
||||
import re.imc.geysermodelenginepackgenerator.generator.Entity;
|
||||
import re.imc.geysermodelenginepackgenerator.util.ZipUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ExtensionMain implements Extension {
|
||||
|
||||
private File source;
|
||||
|
||||
Path generatedPackZip;
|
||||
|
||||
@Subscribe
|
||||
public void onLoad(GeyserPreInitializeEvent event) {
|
||||
source = dataFolder().resolve("input").toFile();
|
||||
source.mkdirs();
|
||||
|
||||
File[] files = source.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
String id = "modelengine:" + file.getName().toLowerCase();
|
||||
GeyserUtils.addCustomEntity(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Subscribe
|
||||
public void onPackLoad(GeyserLoadResourcePacksEvent event) {
|
||||
|
||||
File generatedPack = dataFolder().resolve("generated_pack").toFile();
|
||||
|
||||
GeneratorMain.startGenerate(source, generatedPack);
|
||||
|
||||
Path generatedPackZip = dataFolder().resolve("generated_pack.zip");
|
||||
generatedPackZip = dataFolder().resolve("generated_pack.zip");
|
||||
|
||||
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(generatedPackZip))) {
|
||||
// 压缩文件夹
|
||||
@@ -48,6 +40,19 @@ public class ExtensionMain implements Extension {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (String entity : GeneratorMain.entityMap.keySet()) {
|
||||
String id = "modelengine:" + entity;
|
||||
GeyserUtils.addCustomEntity(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Subscribe
|
||||
public void onPackLoad(GeyserLoadResourcePacksEvent event) {
|
||||
event.resourcePacks().add(generatedPackZip);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ public class GeneratorMain {
|
||||
public static final Map<String, Geometry> geometryMap = new HashMap<>();
|
||||
public static final Map<String, Texture> textureMap = new HashMap<>();
|
||||
public static final Gson GSON = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
|
||||
@@ -35,6 +34,57 @@ public class GeneratorMain {
|
||||
startGenerate(source, output);
|
||||
}
|
||||
|
||||
public static void generateFromFolder(String currentPath, File folder) {
|
||||
if (folder.listFiles() == null) {
|
||||
return;
|
||||
}
|
||||
String modelId = folder.getName().toLowerCase();
|
||||
|
||||
Entity entity = new Entity(modelId);
|
||||
boolean canAdd = false;
|
||||
for (File e : folder.listFiles()) {
|
||||
if (e.isDirectory()) {
|
||||
generateFromFolder(currentPath + folder.getName() + "/", e);
|
||||
}
|
||||
if (e.getName().endsWith(".png")) {
|
||||
canAdd = true;
|
||||
textureMap.put(modelId, new Texture(modelId, currentPath, e.toPath()));
|
||||
}
|
||||
if (e.getName().equals("config.properties")) {
|
||||
try {
|
||||
entity.getProperties().load(new FileReader(e));
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (e.getName().endsWith(".json")) {
|
||||
try {
|
||||
String json = Files.readString(e.toPath());
|
||||
if (isAnimationFile(json)) {
|
||||
Animation animation = new Animation();
|
||||
animation.setPath(currentPath);
|
||||
animation.load(json);
|
||||
animation.setModelId(modelId);
|
||||
animationMap.put(modelId, animation);
|
||||
}
|
||||
|
||||
if (isGeometryFile(json)) {
|
||||
Geometry geometry = new Geometry();
|
||||
geometry.load(json);
|
||||
geometry.setPath(currentPath);
|
||||
geometry.setModelId(modelId);
|
||||
geometryMap.put(modelId, geometry);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (canAdd) {
|
||||
entity.setPath(currentPath);
|
||||
entityMap.put(modelId, entity);
|
||||
}
|
||||
}
|
||||
public static void startGenerate(File source, File output) {
|
||||
|
||||
|
||||
@@ -43,43 +93,7 @@ public class GeneratorMain {
|
||||
if (file1.listFiles() == null) {
|
||||
continue;
|
||||
}
|
||||
String modelId = file1.getName().toLowerCase();
|
||||
|
||||
Entity entity = new Entity(modelId);
|
||||
entityMap.put(modelId, entity);
|
||||
|
||||
for (File e : file1.listFiles()) {
|
||||
if (e.getName().endsWith(".png")) {
|
||||
textureMap.put(modelId, new Texture(modelId, e.toPath()));
|
||||
}
|
||||
if (e.getName().equals("config.properties")) {
|
||||
try {
|
||||
entity.getProperties().load(new FileReader(e));
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (e.getName().endsWith(".json")) {
|
||||
try {
|
||||
String json = Files.readString(e.toPath());
|
||||
if (isAnimationFile(json)) {
|
||||
Animation animation = new Animation();
|
||||
animation.load(json);
|
||||
animation.setModelId(modelId);
|
||||
animationMap.put(modelId, animation);
|
||||
}
|
||||
|
||||
if (isGeometryFile(json)) {
|
||||
Geometry geometry = new Geometry();
|
||||
geometry.load(json);
|
||||
geometry.setModelId(modelId);
|
||||
geometryMap.put(modelId, geometry);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
generateFromFolder("", file1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,18 +103,19 @@ public class GeneratorMain {
|
||||
File texturesFolder = new File(output, "textures/entity");
|
||||
|
||||
|
||||
File manifestFile = new File(output, "manifest.json");
|
||||
boolean generateManifest = false;
|
||||
if (!entityFolder.exists()) {
|
||||
generateManifest = true;
|
||||
}
|
||||
File[] files = entityFolder.listFiles();
|
||||
if (files == null || files.length != entityMap.size()) {
|
||||
if (!manifestFile.exists() || files == null || files.length != entityMap.size()) {
|
||||
generateManifest = true;
|
||||
}
|
||||
|
||||
if (generateManifest) {
|
||||
output.mkdirs();
|
||||
Path path = new File(output, "manifest.json").toPath();
|
||||
Path path = manifestFile.toPath();
|
||||
if (path.toFile().exists()) {
|
||||
try {
|
||||
JsonObject manifest = new JsonParser().parse(Files.readString(path)).getAsJsonObject();
|
||||
@@ -124,56 +139,63 @@ public class GeneratorMain {
|
||||
modelsFolder.mkdirs();
|
||||
texturesFolder.mkdirs();
|
||||
|
||||
for (Map.Entry<String, Animation> stringAnimationEntry : animationMap.entrySet()) {
|
||||
stringAnimationEntry.getValue().modify();
|
||||
Geometry geo = geometryMap.get(stringAnimationEntry.getKey());
|
||||
for (Map.Entry<String, Animation> entry : animationMap.entrySet()) {
|
||||
entry.getValue().modify();
|
||||
Geometry geo = geometryMap.get(entry.getKey());
|
||||
if (geo != null) {
|
||||
stringAnimationEntry.getValue().addHeadBind(geo);
|
||||
entry.getValue().addHeadBind(geo);
|
||||
}
|
||||
Path path = animationsFolder.toPath().resolve(stringAnimationEntry.getKey() + ".animation.json");
|
||||
Path path = animationsFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".animation.json");
|
||||
path.toFile().getParentFile().mkdirs();
|
||||
|
||||
if (path.toFile().exists()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Files.writeString(path, GSON.toJson(stringAnimationEntry.getValue().getJson()), StandardCharsets.UTF_8);
|
||||
Files.writeString(path, GSON.toJson(entry.getValue().getJson()), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Geometry> stringGeometryEntry : geometryMap.entrySet()) {
|
||||
stringGeometryEntry.getValue().modify();
|
||||
Path path = modelsFolder.toPath().resolve(stringGeometryEntry.getKey() + ".geo.json");
|
||||
for (Map.Entry<String, Geometry> entry : geometryMap.entrySet()) {
|
||||
entry.getValue().modify();
|
||||
Path path = modelsFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".geo.json");
|
||||
path.toFile().getParentFile().mkdirs();
|
||||
|
||||
if (path.toFile().exists()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Files.writeString(path, GSON.toJson(stringGeometryEntry.getValue().getJson()), StandardCharsets.UTF_8);
|
||||
Files.writeString(path, GSON.toJson(entry.getValue().getJson()), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Texture> stringTextureEntry : textureMap.entrySet()) {
|
||||
Path path = texturesFolder.toPath().resolve(stringTextureEntry.getKey() + ".png");
|
||||
for (Map.Entry<String, Texture> entry : textureMap.entrySet()) {
|
||||
Path path = texturesFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".png");
|
||||
path.toFile().getParentFile().mkdirs();
|
||||
|
||||
if (path.toFile().exists()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Files.copy(stringTextureEntry.getValue().getPath(), path, StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(entry.getValue().getOriginalPath(), path, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Entity> stringEntityEntry : entityMap.entrySet()) {
|
||||
stringEntityEntry.getValue().modify();
|
||||
Path path = entityFolder.toPath().resolve(stringEntityEntry.getKey() + ".entity.json");
|
||||
for (Map.Entry<String, Entity> entry : entityMap.entrySet()) {
|
||||
entry.getValue().modify();
|
||||
Path path = entityFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".entity.json");
|
||||
path.toFile().getParentFile().mkdirs();
|
||||
if (path.toFile().exists()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Files.writeString(path, stringEntityEntry.getValue().getJson(), StandardCharsets.UTF_8);
|
||||
Files.writeString(path, entry.getValue().getJson(), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ public class Animation {
|
||||
String modelId;
|
||||
JsonObject json;
|
||||
|
||||
String path;
|
||||
|
||||
public void load(String json) {
|
||||
this.json = new JsonParser().parse(json).getAsJsonObject();
|
||||
}
|
||||
|
||||
@@ -28,14 +28,15 @@ public class Entity {
|
||||
"default": "%geometry%"
|
||||
},
|
||||
"animations": {
|
||||
"default": "animation.%entity_id%.idle",
|
||||
"default": "animation.%entity_id%",
|
||||
"look_at_target": "%look_at_target%"
|
||||
|
||||
},
|
||||
"scripts": {
|
||||
"animate": [
|
||||
"default",
|
||||
"look_at_target"
|
||||
"look_at_target",
|
||||
"spawn"
|
||||
]
|
||||
},
|
||||
"render_controllers": [
|
||||
@@ -51,6 +52,8 @@ public class Entity {
|
||||
String json;
|
||||
boolean hasHeadAnimation = false;
|
||||
|
||||
String path;
|
||||
|
||||
Properties properties = new Properties();
|
||||
|
||||
public Entity(String modelId) {
|
||||
@@ -60,7 +63,7 @@ public class Entity {
|
||||
public void modify() {
|
||||
json = TEMPLATE.replace("%entity_id%", modelId)
|
||||
.replace("%geometry%", "geometry.modelengine_" + modelId)
|
||||
.replace("%texture%", "textures/entity/" + modelId)
|
||||
.replace("%texture%", "textures/entity/" + path + modelId)
|
||||
.replace("%look_at_target%", "animation." + modelId + ".look_at_target")
|
||||
.replace("%material%", properties.getProperty("material", "entity_alphatest"))
|
||||
.replace("%render_controller%", properties.getProperty("render_controller", "controller.render.default"))
|
||||
|
||||
@@ -19,6 +19,8 @@ public class Geometry {
|
||||
|
||||
String modelId;
|
||||
JsonObject json;
|
||||
|
||||
String path;
|
||||
public void load(String json) {
|
||||
this.json = new JsonParser().parse(json).getAsJsonObject();
|
||||
}
|
||||
|
||||
@@ -12,5 +12,8 @@ import java.nio.file.Path;
|
||||
public class Texture {
|
||||
|
||||
String modelId;
|
||||
Path path;
|
||||
|
||||
String path;
|
||||
Path originalPath;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user