mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-26 02:19:23 +00:00
修改资源创建指令
This commit is contained in:
@@ -6,16 +6,23 @@ import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
|
||||
import net.momirealms.craftengine.core.pack.ResourceLocation;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
|
||||
import net.momirealms.craftengine.core.plugin.config.StringKeyConstructor;
|
||||
import net.momirealms.craftengine.core.plugin.locale.MessageConstants;
|
||||
import net.momirealms.craftengine.core.util.FileUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.incendo.cloud.Command;
|
||||
import org.incendo.cloud.CommandManager;
|
||||
import org.incendo.cloud.parser.standard.StringParser;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CreateResourceCommand extends BukkitCommandFeature<CommandSender> {
|
||||
|
||||
@@ -31,51 +38,49 @@ public class CreateResourceCommand extends BukkitCommandFeature<CommandSender> {
|
||||
.required("pack", StringParser.stringComponent(StringParser.StringMode.SINGLE))
|
||||
.optional("namespace", StringParser.stringComponent(StringParser.StringMode.SINGLE))
|
||||
.optional("author", StringParser.stringComponent(StringParser.StringMode.SINGLE))
|
||||
.optional("description", StringParser.stringComponent(StringParser.StringMode.QUOTED))
|
||||
.optional("description", StringParser.stringComponent(StringParser.StringMode.GREEDY_FLAG_YIELDING))
|
||||
.handler(context -> {
|
||||
String packFolder = context.get("pack");
|
||||
Path packPath = plugin().dataFolderPath().resolve("resources").resolve(packFolder);
|
||||
if (Files.exists(packPath)) {
|
||||
handleFeedback(context, MessageConstants.COMMAND_RESOURCE_CREATE_FAILURE_EXISTS, Component.text(packFolder));
|
||||
return;
|
||||
}
|
||||
Path configurationPath = packPath.resolve("configuration");
|
||||
Path resourcepackPath = packPath.resolve("resourcepack");
|
||||
Path packMetaPath = packPath.resolve("pack.yml");
|
||||
if (Files.exists(packPath)) {
|
||||
handleFeedback(context, MessageConstants.COMMAND_RESOURCE_CREATE_EXISTS, Component.text(packFolder));
|
||||
return;
|
||||
}
|
||||
String namespace = context.getOrDefault("namespace", packFolder);
|
||||
if (!ResourceLocation.isValidNamespace(namespace)) {
|
||||
handleFeedback(context, MessageConstants.COMMAND_RESOURCE_CREATE_FAILURE_INVALID_NAMESPACE, Component.text(packFolder), Component.text(namespace));
|
||||
return;
|
||||
}
|
||||
String author = context.getOrDefault("author", "CraftEngine");
|
||||
String description = context.getOrDefault("description", "Auto-created by CraftEngine");
|
||||
String description = context.getOrDefault("description", "Auto-generated by CraftEngine");
|
||||
try {
|
||||
FileUtils.createDirectoriesSafe(packPath);
|
||||
FileUtils.createDirectoriesSafe(configurationPath);
|
||||
Path namespacePath = resourcepackPath.resolve("assets").resolve(namespace);
|
||||
FileUtils.createDirectoriesSafe(namespacePath);
|
||||
if (context.flags().hasFlag("full")) {
|
||||
Path modelsPath = namespacePath.resolve("models");
|
||||
FileUtils.createDirectoriesSafe(modelsPath.resolve("block"));
|
||||
FileUtils.createDirectoriesSafe(modelsPath.resolve("item"));
|
||||
Path texturesPath = namespacePath.resolve("textures");
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("block"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("entity"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("font"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("gui").resolve("sprites").resolve("tooltip"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("item"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("trims"));
|
||||
FileUtils.createDirectoriesSafe(namespacePath.resolve("sounds"));
|
||||
}
|
||||
YamlDocument document = plugin().config().loadYamlData(packMetaPath);
|
||||
document.set("author", author);
|
||||
document.set("version", "0.0.1");
|
||||
document.set("description", description);
|
||||
document.set("namespace", namespace);
|
||||
document.set("enable", false);
|
||||
document.save(packMetaPath.toFile());
|
||||
Path modelsPath = namespacePath.resolve("models");
|
||||
FileUtils.createDirectoriesSafe(modelsPath.resolve("block"));
|
||||
FileUtils.createDirectoriesSafe(modelsPath.resolve("item"));
|
||||
Path texturesPath = namespacePath.resolve("textures");
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("block"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("font"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("gui").resolve("sprites").resolve("tooltip"));
|
||||
FileUtils.createDirectoriesSafe(texturesPath.resolve("item"));
|
||||
FileUtils.createDirectoriesSafe(namespacePath.resolve("sounds"));
|
||||
Yaml yaml = new Yaml(new StringKeyConstructor(packMetaPath, new LoaderOptions()));
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("author", author);
|
||||
data.put("description", description);
|
||||
data.put("version", "1.0");
|
||||
data.put("namespace", namespace);
|
||||
data.put("enable", true);
|
||||
String dump = yaml.dump(data);
|
||||
Files.writeString(packMetaPath, dump);
|
||||
} catch (IOException e) {
|
||||
handleFeedback(context, MessageConstants.COMMAND_RESOURCE_CREATE_FAILURE, Component.text(packFolder), Component.text(e.getMessage()));
|
||||
handleFeedback(context, MessageConstants.COMMAND_RESOURCE_CREATE_FAILURE_UNKNOWN, Component.text(packFolder), Component.text(e.getMessage()));
|
||||
return;
|
||||
}
|
||||
handleFeedback(context, MessageConstants.COMMAND_RESOURCE_CREATE_SUCCESS, Component.text(packFolder));
|
||||
|
||||
@@ -86,9 +86,9 @@ command.resource.disable.success: "<white>Disabled resource <arg:0>. Run <click:
|
||||
command.resource.disable.failure.unknown: "<red>Unknown resource <arg:0></red>"
|
||||
command.resource.list: "<white>Enabled resources(<arg:0>): <green><arg:1></green><newline>Disabled resources(<arg:2>): <red><arg:3></red></white>"
|
||||
command.resource.create.success: "<white>Created resource <arg:0>.</white>"
|
||||
command.resource.create.failure: "<red>Failed to create resource <arg:0>, Error: <arg:1></red>"
|
||||
command.resource.create.failure.unknown: "<red>Failed to create resource <arg:0>, Error: <arg:1></red>"
|
||||
command.resource.create.failure.invalid_namespace: "<red>Failed to create resource <arg:0>, The namespace <arg:1> is invalid.</red>"
|
||||
command.resource.create.exists: "<red>Resource <arg:0> already exists.</red>"
|
||||
command.resource.create.failure.exists: "<red>Resource <arg:0> already exists.</red>"
|
||||
command.upload.failure.not_supported: "<red>Current hosting method '<arg:0>' doesn't support uploading resource packs.</red>"
|
||||
command.upload.on_progress: "<white>Started uploading progress. Check the console for more information.</white>"
|
||||
command.send_resource_pack.success.single: "<white>Sent resource pack to <arg:0>.</white>"
|
||||
|
||||
@@ -86,9 +86,9 @@ command.resource.disable.success: "<white>已禁用 <arg:0>. 执行 <click:run_c
|
||||
command.resource.disable.failure.unknown: "<red>未知资源 <arg:0></red>"
|
||||
command.resource.list: "<white>启用的资源(<arg:0>): <green><arg:1></green><newline>禁用的资源(<arg:2>): <red><arg:3></red></white>"
|
||||
command.resource.create.success: "<white>已创建资源 <arg:0></white>"
|
||||
command.resource.create.failure: "<red>创建资源 <arg:0> 失败, 错误信息: <arg:1></red>"
|
||||
command.resource.create.failure.unknown: "<red>创建资源 <arg:0> 失败, 错误信息: <arg:1></red>"
|
||||
command.resource.create.failure.invalid_namespace: "<red>创建资源 <arg:0> 失败, 命名空间 <arg:1> 不合法</red>"
|
||||
command.resource.create.exists: "<red>资源 <arg:0> 已存在</red>"
|
||||
command.resource.create.failure.exists: "<red>资源 <arg:0> 已存在</red>"
|
||||
command.upload.failure.not_supported: "<red>当前托管模式 '<arg:0>' 不支持上传资源包</red>"
|
||||
command.upload.on_progress: "<white>已开始上传进程. 检查控制台以获取详细信息</white>"
|
||||
command.send_resource_pack.success.single: "<white>发送资源包给 <arg:0></white>"
|
||||
|
||||
@@ -27,9 +27,9 @@ public interface MessageConstants {
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_DISABLE_FAILURE = Component.translatable().key("command.resource.disable.failure.unknown");
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_LIST = Component.translatable().key("command.resource.list");
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_CREATE_SUCCESS = Component.translatable().key("command.resource.create.success");
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_CREATE_FAILURE = Component.translatable().key("command.resource.create.failure");
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_CREATE_FAILURE_UNKNOWN = Component.translatable().key("command.resource.create.failure.unknown");
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_CREATE_FAILURE_INVALID_NAMESPACE = Component.translatable().key("command.resource.create.failure.invalid_namespace");
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_CREATE_EXISTS = Component.translatable().key("command.resource.create.exists");
|
||||
TranslatableComponent.Builder COMMAND_RESOURCE_CREATE_FAILURE_EXISTS = Component.translatable().key("command.resource.create.failure.exists");
|
||||
TranslatableComponent.Builder COMMAND_UPLOAD_FAILURE_NOT_SUPPORTED = Component.translatable().key("command.upload.failure.not_supported");
|
||||
TranslatableComponent.Builder COMMAND_UPLOAD_ON_PROGRESS = Component.translatable().key("command.upload.on_progress");
|
||||
TranslatableComponent.Builder COMMAND_SEND_RESOURCE_PACK_SUCCESS_SINGLE = Component.translatable().key("command.send_resource_pack.success.single");
|
||||
|
||||
Reference in New Issue
Block a user