9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-04 15:41:38 +00:00

refactor(core): 修改资源包的一些东西

This commit is contained in:
jhqwqmc
2025-05-06 15:37:22 +08:00
parent 3c9651a1b3
commit afa3d033dc
11 changed files with 76 additions and 18 deletions

View File

@@ -78,6 +78,8 @@ resource-pack:
ip: "localhost" ip: "localhost"
port: 8163 port: 8163
protocol: "http" protocol: "http"
# The optional URL must be complete and include a trailing slash / at the end.
#url: "http://localhost:8163/"
deny-non-minecraft-request: true deny-non-minecraft-request: true
one-time-token: true one-time-token: true
rate-limit: rate-limit:

View File

@@ -272,6 +272,7 @@ warning.config.host.s3.missing_secret: "<yellow>Issue found in config.yml at 're
warning.config.host.s3.missing_upload_path: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'upload-path' argument for s3 host.</yellow>" warning.config.host.s3.missing_upload_path: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'upload-path' argument for s3 host.</yellow>"
warning.config.host.self.missing_ip: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'ip' argument for self host.</yellow>" warning.config.host.self.missing_ip: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'ip' argument for self host.</yellow>"
warning.config.host.self.invalid_port: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Invalid port '<arg:0>' for self host.</yellow>" warning.config.host.self.invalid_port: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Invalid port '<arg:0>' for self host.</yellow>"
warning.config.host.self.invalid_url: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Invalid url '<arg:0>' for self host.</yellow>"
warning.config.host.gitlab.missing_url: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'gitlab-url' argument for gitlab host.</yellow>" warning.config.host.gitlab.missing_url: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'gitlab-url' argument for gitlab host.</yellow>"
warning.config.host.gitlab.missing_token: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'access-token' argument for gitlab host.</yellow>" warning.config.host.gitlab.missing_token: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'access-token' argument for gitlab host.</yellow>"
warning.config.host.gitlab.missing_project: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'project-id' argument for gitlab host.</yellow>" warning.config.host.gitlab.missing_project: "<yellow>Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'project-id' argument for gitlab host.</yellow>"
@@ -292,4 +293,5 @@ warning.config.conflict_matcher.all_of.missing_terms: "<yellow>Issue found in co
warning.config.conflict_matcher.any_of.missing_terms: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'terms' argument for 'any_of' matcher.</yellow>" warning.config.conflict_matcher.any_of.missing_terms: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'terms' argument for 'any_of' matcher.</yellow>"
warning.config.conflict_resolution.missing_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'type' argument for one of the resolutions.</yellow>" warning.config.conflict_resolution.missing_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'type' argument for one of the resolutions.</yellow>"
warning.config.conflict_resolution.invalid_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - One of the resolutions is using the invalid type '<arg:0>'.</yellow>" warning.config.conflict_resolution.invalid_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - One of the resolutions is using the invalid type '<arg:0>'.</yellow>"
warning.config.function.command.missing_command: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'command' argument for 'command' function.</yellow>" warning.config.function.command.missing_command: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'command' argument for 'command' function.</yellow>"
warning.resource_pack.generation_in_progress: "<yellow>Resource pack generation is already in progress. Please wait until it finishes.</yellow>"

View File

@@ -274,6 +274,7 @@ warning.config.host.s3.missing_secret: "<yellow>在 config.yml 的 'resource-pac
warning.config.host.s3.missing_upload_path: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - S3 托管缺少必需的 'upload-path' 参数</yellow>" warning.config.host.s3.missing_upload_path: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - S3 托管缺少必需的 'upload-path' 参数</yellow>"
warning.config.host.self.missing_ip: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - 自托管托管缺少必需的 'ip' 参数</yellow>" warning.config.host.self.missing_ip: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - 自托管托管缺少必需的 'ip' 参数</yellow>"
warning.config.host.self.invalid_port: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - 自托管托管的端口 '<arg:0>' 无效</yellow>" warning.config.host.self.invalid_port: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - 自托管托管的端口 '<arg:0>' 无效</yellow>"
warning.config.host.self.invalid_url: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - 自托管托管的 URL '<arg:0>' 无效</yellow>"
warning.config.host.gitlab.missing_url: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - GitLab 托管缺少必需的 'gitlab-url' 参数</yellow>" warning.config.host.gitlab.missing_url: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - GitLab 托管缺少必需的 'gitlab-url' 参数</yellow>"
warning.config.host.gitlab.missing_token: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - GitLab 托管缺少必需的 'access-token' 参数</yellow>" warning.config.host.gitlab.missing_token: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - GitLab 托管缺少必需的 'access-token' 参数</yellow>"
warning.config.host.gitlab.missing_project: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - GitLab 托管缺少必需的 'project-id' 参数</yellow>" warning.config.host.gitlab.missing_project: "<yellow>在 config.yml 的 'resource-pack.delivery.hosting' 处发现问题 - GitLab 托管缺少必需的 'project-id' 参数</yellow>"
@@ -293,4 +294,5 @@ warning.config.conflict_matcher.inverted.missing_term: "<yellow>在 config.yml
warning.config.conflict_matcher.all_of.missing_terms: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 全匹配器缺少必需的 'terms' 参数</yellow>" warning.config.conflict_matcher.all_of.missing_terms: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 全匹配器缺少必需的 'terms' 参数</yellow>"
warning.config.conflict_matcher.any_of.missing_terms: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 任一匹配器缺少必需的 'terms' 参数</yellow>" warning.config.conflict_matcher.any_of.missing_terms: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 任一匹配器缺少必需的 'terms' 参数</yellow>"
warning.config.conflict_resolution.missing_type: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 文件冲突处理器的某个解决方案缺少必需的 'type' 参数</yellow>" warning.config.conflict_resolution.missing_type: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 文件冲突处理器的某个解决方案缺少必需的 'type' 参数</yellow>"
warning.config.conflict_resolution.invalid_type: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 文件冲突处理器的某个解决方案使用了无效类型 '<arg:0>'</yellow>" warning.config.conflict_resolution.invalid_type: "<yellow>在 config.yml 的 'resource-pack.duplicated-files-handler' 处发现问题 - 文件冲突处理器的某个解决方案使用了无效类型 '<arg:0>'</yellow>"
warning.resource_pack.generation_in_progress: "<yellow>资源包正在生成中, 请等待完成再试</yellow>"

View File

@@ -4,6 +4,7 @@ import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature; import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager; import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.plugin.locale.MessageConstants; import net.momirealms.craftengine.core.plugin.locale.MessageConstants;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.incendo.cloud.Command; import org.incendo.cloud.Command;
@@ -68,7 +69,9 @@ public class ReloadCommand extends BukkitCommandFeature<CommandSender> {
long time2 = System.currentTimeMillis(); long time2 = System.currentTimeMillis();
long packTime = time2 - time1; long packTime = time2 - time1;
handleFeedback(context, MessageConstants.COMMAND_RELOAD_PACK_SUCCESS, Component.text(packTime)); handleFeedback(context, MessageConstants.COMMAND_RELOAD_PACK_SUCCESS, Component.text(packTime));
} catch (LocalizedException e) {
handleFeedback(context, MessageConstants.COMMAND_RELOAD_PACK_FAILURE);
plugin().logger().warn(e.getMessage());
} catch (Exception e) { } catch (Exception e) {
handleFeedback(context, MessageConstants.COMMAND_RELOAD_PACK_FAILURE); handleFeedback(context, MessageConstants.COMMAND_RELOAD_PACK_FAILURE);
plugin().logger().warn("Failed to generate resource pack", e); plugin().logger().warn("Failed to generate resource pack", e);
@@ -89,6 +92,9 @@ public class ReloadCommand extends BukkitCommandFeature<CommandSender> {
Component.text(reloadResult.syncTime()), Component.text(reloadResult.syncTime()),
Component.text(packTime) Component.text(packTime)
); );
} catch (LocalizedException e) {
handleFeedback(context, MessageConstants.COMMAND_RELOAD_PACK_FAILURE);
plugin().logger().warn(e.getMessage());
} finally { } finally {
RELOAD_PACK_FLAG = false; RELOAD_PACK_FLAG = false;
} }

View File

@@ -34,6 +34,7 @@ import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.network.ConnectionState; import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.NetWorkUser; import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
import net.momirealms.craftengine.core.plugin.network.NetworkManager; import net.momirealms.craftengine.core.plugin.network.NetworkManager;
import net.momirealms.craftengine.core.plugin.network.ProtocolVersion;
import net.momirealms.craftengine.core.util.*; import net.momirealms.craftengine.core.util.*;
import net.momirealms.craftengine.core.world.BlockHitResult; import net.momirealms.craftengine.core.world.BlockHitResult;
import net.momirealms.craftengine.core.world.BlockPos; import net.momirealms.craftengine.core.world.BlockPos;
@@ -1348,7 +1349,7 @@ public class PacketConsumers {
// When the hotbar is full, the latest creative mode inventory can only be accessed when the player opens the inventory screen. Currently, it is not worth further handling this issue. // When the hotbar is full, the latest creative mode inventory can only be accessed when the player opens the inventory screen. Currently, it is not worth further handling this issue.
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> SET_CREATIVE_SLOT = (user, event, packet) -> { public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> SET_CREATIVE_SLOT = (user, event, packet) -> {
try { try {
if (VersionHelper.isOrAbove1_21_4()) return; if (user.protocolVersion().isVersionNewerThan(ProtocolVersion.V1_21_4)) return;
if (!user.isOnline()) return; if (!user.isOnline()) return;
BukkitServerPlayer player = (BukkitServerPlayer) user; BukkitServerPlayer player = (BukkitServerPlayer) user;
if (VersionHelper.isFolia()) { if (VersionHelper.isFolia()) {

View File

@@ -44,6 +44,7 @@ import java.util.*;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream;
import static net.momirealms.craftengine.core.util.MiscUtils.castToMap; import static net.momirealms.craftengine.core.util.MiscUtils.castToMap;
@@ -73,6 +74,7 @@ public abstract class AbstractPackManager implements PackManager {
private final TreeMap<ConfigSectionParser, List<CachedConfig>> cachedConfigs = new TreeMap<>(); private final TreeMap<ConfigSectionParser, List<CachedConfig>> cachedConfigs = new TreeMap<>();
protected BiConsumer<Path, Path> zipGenerator; protected BiConsumer<Path, Path> zipGenerator;
protected ResourcePackHost resourcePackHost; protected ResourcePackHost resourcePackHost;
private boolean generateResourcePack = false;
public AbstractPackManager(CraftEngine plugin, BiConsumer<Path, Path> eventDispatcher) { public AbstractPackManager(CraftEngine plugin, BiConsumer<Path, Path> eventDispatcher) {
this.plugin = plugin; this.plugin = plugin;
@@ -141,6 +143,7 @@ public abstract class AbstractPackManager implements PackManager {
@Override @Override
public void load() { public void load() {
initFileSystemProvider();
List<Map<?, ?>> list = Config.instance().settings().getMapList("resource-pack.delivery.hosting"); List<Map<?, ?>> list = Config.instance().settings().getMapList("resource-pack.delivery.hosting");
if (list == null || list.isEmpty()) { if (list == null || list.isEmpty()) {
this.resourcePackHost = NoneHost.INSTANCE; this.resourcePackHost = NoneHost.INSTANCE;
@@ -484,8 +487,42 @@ public abstract class AbstractPackManager implements PackManager {
} }
} }
private static void initFileSystemProvider() {
String osName = System.getProperty("os.name").toLowerCase();
String providerClass = null;
if (osName.contains("win")) {
providerClass = "sun.nio.fs.WindowsFileSystemProvider";
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
providerClass = "sun.nio.fs.LinuxFileSystemProvider";
} else if (osName.contains("mac")) {
providerClass = "sun.nio.fs.MacOSXFileSystemProvider";
}
if (providerClass != null) {
try {
System.setProperty("java.nio.file.spi.DefaultFileSystemProvider", providerClass);
} catch (Exception ignored) {}
}
}
private static void deleteDirectory(Path folder) throws IOException {
if (!Files.exists(folder)) return;
try (Stream<Path> walk = Files.walk(folder)) {
walk.sorted(Comparator.reverseOrder())
.parallel()
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException ignored) {}
});
}
}
@Override @Override
public void generateResourcePack() { public void generateResourcePack() {
if (this.generateResourcePack) {
throw new LocalizedException("warning.resource_pack.generation_in_progress");
}
this.generateResourcePack = true;
this.plugin.logger().info("Generating resource pack..."); this.plugin.logger().info("Generating resource pack...");
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
// get the target location // get the target location
@@ -494,7 +531,7 @@ public abstract class AbstractPackManager implements PackManager {
.resolve("resource_pack"); .resolve("resource_pack");
try { try {
org.apache.commons.io.FileUtils.deleteDirectory(generatedPackPath.toFile()); deleteDirectory(generatedPackPath);
} catch (IOException e) { } catch (IOException e) {
this.plugin.logger().severe("Error deleting previous resource pack", e); this.plugin.logger().severe("Error deleting previous resource pack", e);
} }
@@ -547,6 +584,7 @@ public abstract class AbstractPackManager implements PackManager {
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
this.plugin.logger().info("Finished generating resource pack in " + (end - start) + "ms"); this.plugin.logger().info("Finished generating resource pack in " + (end - start) + "ms");
this.eventDispatcher.accept(generatedPackPath, zipFile); this.eventDispatcher.accept(generatedPackPath, zipFile);
this.generateResourcePack = false;
} }
private void generateParticle(Path generatedPackPath) { private void generateParticle(Path generatedPackPath) {

View File

@@ -66,6 +66,13 @@ public class SelfHost implements ResourcePackHost {
if (port <= 0 || port > 65535) { if (port <= 0 || port > 65535) {
throw new LocalizedException("warning.config.host.self.invalid_port", String.valueOf(port)); throw new LocalizedException("warning.config.host.self.invalid_port", String.valueOf(port));
} }
String url = arguments.getOrDefault("url", "").toString();
if (!url.isEmpty()) {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
throw new LocalizedException("warning.config.host.self.invalid_url", url);
}
if (!url.endsWith("/")) url += "/";
}
boolean oneTimeToken = (boolean) arguments.getOrDefault("one-time-token", true); boolean oneTimeToken = (boolean) arguments.getOrDefault("one-time-token", true);
String protocol = arguments.getOrDefault("protocol", "http").toString(); String protocol = arguments.getOrDefault("protocol", "http").toString();
boolean denyNonMinecraftRequest = (boolean) arguments.getOrDefault("deny-non-minecraft-request", true); boolean denyNonMinecraftRequest = (boolean) arguments.getOrDefault("deny-non-minecraft-request", true);
@@ -76,7 +83,7 @@ public class SelfHost implements ResourcePackHost {
maxRequests = ResourceConfigUtils.getAsInt(rateMap.getOrDefault("max-requests", 5), "max-requests"); maxRequests = ResourceConfigUtils.getAsInt(rateMap.getOrDefault("max-requests", 5), "max-requests");
resetInterval = ResourceConfigUtils.getAsInt(rateMap.getOrDefault("reset-interval", 20), "reset-interval") * 1000; resetInterval = ResourceConfigUtils.getAsInt(rateMap.getOrDefault("reset-interval", 20), "reset-interval") * 1000;
} }
selfHostHttpServer.updateProperties(ip, port, denyNonMinecraftRequest, protocol, maxRequests, resetInterval, oneTimeToken); selfHostHttpServer.updateProperties(ip, port, url, denyNonMinecraftRequest, protocol, maxRequests, resetInterval, oneTimeToken);
return INSTANCE; return INSTANCE;
} }
} }

View File

@@ -48,6 +48,7 @@ public class SelfHostHttpServer {
private String ip = "localhost"; private String ip = "localhost";
private int port = -1; private int port = -1;
private String protocol = "http"; private String protocol = "http";
private String url;
private boolean denyNonMinecraft = true; private boolean denyNonMinecraft = true;
private boolean useToken; private boolean useToken;
@@ -57,12 +58,14 @@ public class SelfHostHttpServer {
public void updateProperties(String ip, public void updateProperties(String ip,
int port, int port,
String url,
boolean denyNonMinecraft, boolean denyNonMinecraft,
String protocol, String protocol,
int maxRequests, int maxRequests,
int resetInternal, int resetInternal,
boolean token) { boolean token) {
this.ip = ip; this.ip = ip;
this.url = url;
this.denyNonMinecraft = denyNonMinecraft; this.denyNonMinecraft = denyNonMinecraft;
this.protocol = protocol; this.protocol = protocol;
this.rateLimit = maxRequests; this.rateLimit = maxRequests;
@@ -112,6 +115,9 @@ public class SelfHostHttpServer {
} }
public String url() { public String url() {
if (this.url != null && !this.url.isEmpty()) {
return this.url;
}
return this.protocol + "://" + this.ip + ":" + this.port + "/"; return this.protocol + "://" + this.ip + ":" + this.port + "/";
} }

View File

@@ -32,6 +32,10 @@ public enum ProtocolVersion {
return name; return name;
} }
public boolean isVersionNewerThan(ProtocolVersion targetVersion) {
return this.getId() >= targetVersion.getId();
}
public static ProtocolVersion getByName(String name) { public static ProtocolVersion getByName(String name) {
for (ProtocolVersion version : values()) { for (ProtocolVersion version : values()) {
if (version.getName().equals(name)) { if (version.getName().equals(name)) {

View File

@@ -1,10 +0,0 @@
package net.momirealms.craftengine.core.util;
import net.momirealms.craftengine.core.plugin.network.ProtocolVersion;
public class ProtocolVersionUtils {
public static boolean isVersionNewerThan(ProtocolVersion version, ProtocolVersion targetVersion) {
return version.getId() >= targetVersion.getId();
}
}

View File

@@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G
# Rule: [major update].[feature update].[bug fix] # Rule: [major update].[feature update].[bug fix]
project_version=0.0.53-beta.3 project_version=0.0.53-beta.3
config_version=31 config_version=31
lang_version=11 lang_version=12
project_group=net.momirealms project_group=net.momirealms
latest_supported_version=1.21.5 latest_supported_version=1.21.5
@@ -40,7 +40,7 @@ geantyref_version=1.3.16
zstd_version=1.5.7-2 zstd_version=1.5.7-2
commons_io_version=2.18.0 commons_io_version=2.18.0
sparrow_nbt_version=0.7.3 sparrow_nbt_version=0.7.3
sparrow_util_version=0.39 sparrow_util_version=0.40
fastutil_version=8.5.15 fastutil_version=8.5.15
netty_version=4.1.119.Final netty_version=4.1.119.Final
joml_version=1.10.8 joml_version=1.10.8