mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-06 15:52:03 +00:00
移除资源包发送旧有代码
This commit is contained in:
@@ -7,8 +7,6 @@ import net.momirealms.craftengine.core.font.BitmapImage;
|
||||
import net.momirealms.craftengine.core.font.Font;
|
||||
import net.momirealms.craftengine.core.item.EquipmentData;
|
||||
import net.momirealms.craftengine.core.pack.conflict.resolution.ConditionalResolution;
|
||||
import net.momirealms.craftengine.core.pack.host.HostMode;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
import net.momirealms.craftengine.core.pack.misc.EquipmentGeneration;
|
||||
import net.momirealms.craftengine.core.pack.model.ItemModel;
|
||||
import net.momirealms.craftengine.core.pack.model.generation.ModelGeneration;
|
||||
@@ -151,13 +149,6 @@ public abstract class AbstractPackManager implements PackManager {
|
||||
@Override
|
||||
public void load() {
|
||||
this.calculateHash();
|
||||
if (Config.hostMode() == HostMode.SELF_HOST) {
|
||||
Path path = Config.hostResourcePackPath().startsWith(".") ? plugin.dataFolderPath().resolve(Config.hostResourcePackPath()) : Path.of(Config.hostResourcePackPath());
|
||||
ResourcePackHost.instance().enable(Config.hostIP(), Config.hostPort(), path);
|
||||
ResourcePackHost.instance().setRateLimit(Config.requestRate(), Config.requestInterval(), TimeUnit.SECONDS);
|
||||
} else {
|
||||
ResourcePackHost.instance().disable();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package net.momirealms.craftengine.core.pack.host;
|
||||
|
||||
public enum HostMode {
|
||||
SELF_HOST,
|
||||
EXTERNAL_HOST,
|
||||
NONE
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
package net.momirealms.craftengine.core.pack.host;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ResourcePackHost {
|
||||
private static ResourcePackHost instance;
|
||||
private HttpServer server;
|
||||
private String ip;
|
||||
private int port;
|
||||
private Path resourcePackPath;
|
||||
private final ConcurrentHashMap<String, IpAccessRecord> ipAccessMap = new ConcurrentHashMap<>();
|
||||
private int rateLimit = 1;
|
||||
private long rateLimitInterval = 1000;
|
||||
|
||||
public String url() {
|
||||
return Config.hostProtocol() + "://" + ip + ":" + port + "/";
|
||||
}
|
||||
|
||||
public void enable(String ip, int port, Path resourcePackPath) {
|
||||
if (isAlive() && ip.equals(this.ip) && port == this.port && resourcePackPath.equals(this.resourcePackPath)) {
|
||||
return;
|
||||
}
|
||||
if (server != null) {
|
||||
disable();
|
||||
}
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
this.resourcePackPath = resourcePackPath;
|
||||
|
||||
try {
|
||||
server = HttpServer.create(new InetSocketAddress("::", port), 0);
|
||||
server.createContext("/", new ResourcePackHandler());
|
||||
server.setExecutor(Executors.newCachedThreadPool());
|
||||
server.start();
|
||||
CraftEngine.instance().logger().info("HTTP resource pack server running on " + ip + ":" + port);
|
||||
} catch (IOException e) {
|
||||
CraftEngine.instance().logger().warn("Failed to start HTTP server", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
if (server != null) {
|
||||
server.stop(0);
|
||||
server = null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return server != null;
|
||||
}
|
||||
|
||||
public static ResourcePackHost instance() {
|
||||
if (instance == null) {
|
||||
instance = new ResourcePackHost();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void setRateLimit(int rateLimit, long rateLimitInterval, TimeUnit timeUnit) {
|
||||
this.rateLimit = rateLimit;
|
||||
this.rateLimitInterval = timeUnit.toMillis(rateLimitInterval);
|
||||
}
|
||||
|
||||
private class ResourcePackHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
if (Config.denyNonMinecraftRequest()) {
|
||||
String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
|
||||
if (userAgent == null || !userAgent.startsWith("Minecraft Java/")) {
|
||||
CraftEngine.instance().debug(() -> "Blocked non-Minecraft Java client. User-Agent: " + userAgent);
|
||||
sendError(exchange, 403);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
String clientIp = exchange.getRemoteAddress().getAddress().getHostAddress();
|
||||
|
||||
IpAccessRecord record = ipAccessMap.compute(clientIp, (k, v) -> {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (v == null || currentTime - v.lastAccessTime > rateLimitInterval) {
|
||||
return new IpAccessRecord(currentTime, 1);
|
||||
} else {
|
||||
v.accessCount++;
|
||||
return v;
|
||||
}
|
||||
});
|
||||
|
||||
if (record.accessCount > rateLimit) {
|
||||
CraftEngine.instance().debug(() -> "Rate limit exceeded for IP: " + clientIp);
|
||||
sendError(exchange, 429);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Files.exists(resourcePackPath)) {
|
||||
CraftEngine.instance().logger().warn("ResourcePack not found: " + resourcePackPath);
|
||||
sendError(exchange, 404);
|
||||
return;
|
||||
}
|
||||
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/zip");
|
||||
exchange.getResponseHeaders().set("Content-Length", String.valueOf(Files.size(resourcePackPath)));
|
||||
exchange.sendResponseHeaders(200, Files.size(resourcePackPath));
|
||||
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
Files.copy(resourcePackPath, os);
|
||||
} catch (IOException e) {
|
||||
CraftEngine.instance().logger().warn("Failed to send pack", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendError(HttpExchange exchange, int code) throws IOException {
|
||||
exchange.sendResponseHeaders(code, 0);
|
||||
exchange.getResponseBody().close();
|
||||
}
|
||||
}
|
||||
|
||||
private static class IpAccessRecord {
|
||||
long lastAccessTime;
|
||||
int accessCount;
|
||||
|
||||
IpAccessRecord(long lastAccessTime, int accessCount) {
|
||||
this.lastAccessTime = lastAccessTime;
|
||||
this.accessCount = accessCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import net.momirealms.craftengine.core.item.ItemManager;
|
||||
import net.momirealms.craftengine.core.item.recipe.RecipeManager;
|
||||
import net.momirealms.craftengine.core.loot.VanillaLootManager;
|
||||
import net.momirealms.craftengine.core.pack.PackManager;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
import net.momirealms.craftengine.core.plugin.classpath.ClassPathAppender;
|
||||
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
|
||||
import net.momirealms.craftengine.core.plugin.command.sender.SenderFactory;
|
||||
@@ -243,7 +242,6 @@ public abstract class CraftEngine implements Plugin {
|
||||
if (this.commandManager != null) this.commandManager.unregisterFeatures();
|
||||
if (this.senderFactory != null) this.senderFactory.close();
|
||||
if (this.dependencyManager != null) this.dependencyManager.close();
|
||||
ResourcePackHost.instance().disable();
|
||||
}
|
||||
|
||||
protected void registerDefaultParsers() {
|
||||
|
||||
@@ -13,7 +13,6 @@ import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings;
|
||||
import dev.dejvokep.boostedyaml.utils.format.NodeRole;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.core.pack.conflict.resolution.ConditionalResolution;
|
||||
import net.momirealms.craftengine.core.pack.host.HostMode;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.PluginProperties;
|
||||
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
|
||||
@@ -79,7 +78,6 @@ public class Config {
|
||||
protected float resource_pack$supported_version$min;
|
||||
protected float resource_pack$supported_version$max;
|
||||
|
||||
protected HostMode resource_pack$send$mode;
|
||||
protected boolean resource_pack$send$kick_if_declined;
|
||||
protected boolean resource_pack$send$send_on_join;
|
||||
protected boolean resource_pack$send$send_on_reload;
|
||||
@@ -215,7 +213,7 @@ public class Config {
|
||||
resource_pack$supported_version$min = getVersion(config.get("resource-pack.supported-version.min", "1.20").toString());
|
||||
resource_pack$supported_version$max = getVersion(config.get("resource-pack.supported-version.max", "LATEST").toString());
|
||||
resource_pack$merge_external_folders = config.getStringList("resource-pack.merge-external-folders");
|
||||
resource_pack$send$mode = HostMode.valueOf(config.getString("resource-pack.send.mode", "self-host").replace("-", "_").toUpperCase(Locale.ENGLISH));
|
||||
//resource_pack$send$mode = HostMode.valueOf(config.getString("resource-pack.send.mode", "self-host").replace("-", "_").toUpperCase(Locale.ENGLISH));
|
||||
resource_pack$send$self_host$port = config.getInt("resource-pack.send.self-host.port", 8163);
|
||||
resource_pack$send$self_host$ip = config.getString("resource-pack.send.self-host.ip", "localhost");
|
||||
resource_pack$self_host$local_file_path = config.getString("resource-pack.send.self-host.local-file-path", "./generated/resource_pack.zip");
|
||||
@@ -459,10 +457,6 @@ public class Config {
|
||||
return instance.resource_pack$merge_external_folders;
|
||||
}
|
||||
|
||||
public static HostMode hostMode() {
|
||||
return instance.resource_pack$send$mode;
|
||||
}
|
||||
|
||||
public static String hostIP() {
|
||||
return instance.resource_pack$send$self_host$ip;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user