diff --git a/src/main/java/net/slqmy/template_paper_plugin/TemplatePaperPlugin.java b/src/main/java/net/slqmy/template_paper_plugin/TemplatePaperPlugin.java index 46bb010e..b120afe3 100644 --- a/src/main/java/net/slqmy/template_paper_plugin/TemplatePaperPlugin.java +++ b/src/main/java/net/slqmy/template_paper_plugin/TemplatePaperPlugin.java @@ -24,8 +24,8 @@ public final class TemplatePaperPlugin extends JavaPlugin { private FileManager fileManager; private PlayerDataManager playerDataManager; private LanguageManager languageManager; - private HttpServerManager httpServerManager; private ResourcePackManager resourcePackManager; + private HttpServerManager httpServerManager; private CustomItemManager customItemManager; private CustomEntityManager customEntityManager; @@ -44,14 +44,14 @@ public LanguageManager getLanguageManager() { return languageManager; } - public HttpServerManager getHttpServerManager() { - return httpServerManager; - } - public ResourcePackManager getResourcePackManager() { return resourcePackManager; } + public HttpServerManager getHttpServerManager() { + return httpServerManager; + } + public CustomItemManager getCustomItemManager() { return customItemManager; } @@ -81,8 +81,8 @@ public void onEnable() { fileManager = new FileManager(this); playerDataManager = new PlayerDataManager(this); languageManager = new LanguageManager(this); - httpServerManager = new HttpServerManager(this); resourcePackManager = new ResourcePackManager(this); + httpServerManager = new HttpServerManager(this); customItemManager = new CustomItemManager(this); customEntityManager = new CustomEntityManager(this); diff --git a/src/main/java/net/slqmy/template_paper_plugin/file/FileUtil.java b/src/main/java/net/slqmy/template_paper_plugin/file/FileUtil.java index 34038589..03c028a9 100644 --- a/src/main/java/net/slqmy/template_paper_plugin/file/FileUtil.java +++ b/src/main/java/net/slqmy/template_paper_plugin/file/FileUtil.java @@ -22,6 +22,13 @@ import java.security.NoSuchAlgorithmException; public class FileUtil { + + private static final String fileExtensionSeparator = "."; + + public static String getFileExtensionSeparator() { + return fileExtensionSeparator; + } + public static List getResourceFileFolderResourceFilePaths(String resourceFileFolderPath) throws IOException { ClassLoader classLoader = FileUtil.class.getClassLoader(); @@ -73,13 +80,16 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) thro zipOutputStream.close(); } - public static String createSha1Hex(File file) { + public static String getSha1HexString(File file) { + String algorithm = "SHA-1"; + MessageDigest digest; try (InputStream fileInputStream = new FileInputStream(file)) { - digest = MessageDigest.getInstance("SHA-1"); + digest = MessageDigest.getInstance(algorithm); int n = 0; byte[] buffer = new byte[8192]; + while (n != -1) { n = fileInputStream.read(buffer); if (n > 0) { @@ -93,13 +103,16 @@ public static String createSha1Hex(File file) { byte[] hashBytes = digest.digest(); StringBuilder hexString = new StringBuilder(2 * hashBytes.length); - for (byte b : hashBytes) { - String hex = Integer.toHexString(0xff & b); + for (byte hashByte : hashBytes) { + String hex = Integer.toHexString(0xff & hashByte); + if (hex.length() == 1) { hexString.append('0'); } + hexString.append(hex); } + return hexString.toString(); } } diff --git a/src/main/java/net/slqmy/template_paper_plugin/http_server/HttpServerManager.java b/src/main/java/net/slqmy/template_paper_plugin/http_server/HttpServerManager.java index 713fa5df..d910b54e 100644 --- a/src/main/java/net/slqmy/template_paper_plugin/http_server/HttpServerManager.java +++ b/src/main/java/net/slqmy/template_paper_plugin/http_server/HttpServerManager.java @@ -10,63 +10,81 @@ import java.io.OutputStream; import java.net.InetSocketAddress; +import org.bukkit.Bukkit; + import net.slqmy.template_paper_plugin.TemplatePaperPlugin; +import net.slqmy.template_paper_plugin.file.FileUtil; +import net.slqmy.template_paper_plugin.http_server.event.listeners.PlayerJoinListener; +import net.slqmy.template_paper_plugin.resource_pack.ResourcePackManager; public class HttpServerManager { private final TemplatePaperPlugin plugin; + private final String hostName = "localhost"; + private final int port = 8000; + + private final int successResponseCode = 200; + private final int notFoundResponseCode = 404; + + private HttpServer server; + + public int getPort() { + return server.getAddress().getPort(); + } + + public String getHostName() { + return server.getAddress().getHostName(); + } + + public String getSocketAddress() { + return getHostName() + ":" + getPort(); + } + public HttpServerManager(TemplatePaperPlugin plugin) { this.plugin = plugin; - HttpServer server; try { - server = HttpServer.create(new InetSocketAddress(8000), 0); + server = HttpServer.create(new InetSocketAddress(hostName, port), 0); } catch (IOException exception) { exception.printStackTrace(); return; } - // Create a context for a specific path and set the handler - server.createContext("/", new MyHandler()); + server.createContext("/", new ResourcePackDownloadHandler()); - // Start the server - server.setExecutor(null); // Use the default executor + server.setExecutor(null); server.start(); - plugin.getLogger().info("Server is running on port 8000"); + Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(plugin, this), plugin); } - class MyHandler implements HttpHandler { + class ResourcePackDownloadHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { - // Set the path to the zip file - String filePath = "plugins/template-paper-plugin/Template Paper Plugin Resource Pack.zip"; - File file = new File(filePath); + ResourcePackManager resourcePackManager = plugin.getResourcePackManager(); + + File file = new File(resourcePackManager.getResourceZipFilePath()); if (file.exists()) { - // Set the response headers - exchange.getResponseHeaders().set("Content-Type", "application/zip"); - exchange.getResponseHeaders().set("Content-Disposition", "attachment; filename=\"Template Paper Plugin Resource Pack.zip\""); + exchange.getResponseHeaders().set("Content-Type", resourcePackManager.getResourcePackFileMimeType()); + exchange.getResponseHeaders().set("Content-Disposition", "attachment; filename=\"" + resourcePackManager.getResourcePackResourceFolderName() + FileUtil.getFileExtensionSeparator() + resourcePackManager.getResourcePackFileExtension() + "\""); - // Send the response headers with the file length - exchange.sendResponseHeaders(200, file.length()); + exchange.sendResponseHeaders(successResponseCode, file.length()); - // Write the file to the response body - try (FileInputStream fis = new FileInputStream(file); OutputStream os = exchange.getResponseBody()) { + try (FileInputStream fileInputStream = new FileInputStream(file); OutputStream outputStream = exchange.getResponseBody()) { byte[] buffer = new byte[1024]; int count; - while ((count = fis.read(buffer)) != -1) { - os.write(buffer, 0, count); + while ((count = fileInputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, count); } } } else { - // If the file does not exist, return a 404 error String response = "404 (Not Found)\n"; - exchange.sendResponseHeaders(404, response.length()); - OutputStream os = exchange.getResponseBody(); - os.write(response.getBytes()); - os.close(); + exchange.sendResponseHeaders(notFoundResponseCode, response.length()); + OutputStream outputStream = exchange.getResponseBody(); + outputStream.write(response.getBytes()); + outputStream.close(); } } } diff --git a/src/main/java/net/slqmy/template_paper_plugin/http_server/event/listeners/PlayerJoinListener.java b/src/main/java/net/slqmy/template_paper_plugin/http_server/event/listeners/PlayerJoinListener.java new file mode 100644 index 00000000..d935d844 --- /dev/null +++ b/src/main/java/net/slqmy/template_paper_plugin/http_server/event/listeners/PlayerJoinListener.java @@ -0,0 +1,32 @@ +package net.slqmy.template_paper_plugin.http_server.event.listeners; + +import java.net.URI; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +import net.kyori.adventure.resource.ResourcePackInfo; +import net.kyori.adventure.resource.ResourcePackRequest; +import net.slqmy.template_paper_plugin.TemplatePaperPlugin; +import net.slqmy.template_paper_plugin.file.FileUtil; +import net.slqmy.template_paper_plugin.http_server.HttpServerManager; + +public class PlayerJoinListener implements Listener { + + private ResourcePackInfo resourcePackInfo; + + public PlayerJoinListener(TemplatePaperPlugin plugin, HttpServerManager httpServerManager) { + plugin.getLogger().info(httpServerManager.getSocketAddress()); + + resourcePackInfo = ResourcePackInfo.resourcePackInfo().hash(FileUtil.getSha1HexString(plugin.getResourcePackManager().getResourcePackZipFile())) + .uri(URI.create("http://" + httpServerManager.getSocketAddress() + "/")).build(); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); + player.sendResourcePacks(ResourcePackRequest.resourcePackRequest().packs(resourcePackInfo).required(true).build()); + } +} diff --git a/src/main/java/net/slqmy/template_paper_plugin/resource_pack/ResourcePackManager.java b/src/main/java/net/slqmy/template_paper_plugin/resource_pack/ResourcePackManager.java index fdb42338..fee4376e 100644 --- a/src/main/java/net/slqmy/template_paper_plugin/resource_pack/ResourcePackManager.java +++ b/src/main/java/net/slqmy/template_paper_plugin/resource_pack/ResourcePackManager.java @@ -4,11 +4,9 @@ import java.nio.file.Path; import org.apache.commons.io.FileUtils; -import org.bukkit.Bukkit; import net.slqmy.template_paper_plugin.TemplatePaperPlugin; import net.slqmy.template_paper_plugin.file.FileUtil; -import net.slqmy.template_paper_plugin.resource_pack.event.listeners.PlayerJoinListener; public class ResourcePackManager { @@ -16,8 +14,33 @@ public class ResourcePackManager { private final String resourcePackResourceFolderName = String.join(" ", TemplatePaperPlugin.class.getSimpleName().split("(?=[A-Z])")) + " Resource Pack"; + private final String resourcePackFileType = "application"; + private final String resourcePackFileExtension = "zip"; + private final String resourcePackFileMimeType = resourcePackFileType + "/" + resourcePackFileExtension; + + private String resourcePackZipFilePath; private File resourcePackZipFile; + public String getResourcePackFileType() { + return resourcePackFileType; + } + + public String getResourcePackFileExtension() { + return resourcePackFileExtension; + } + + public String getResourcePackFileMimeType() { + return resourcePackFileMimeType; + } + + public String getResourcePackResourceFolderName() { + return resourcePackResourceFolderName; + } + + public String getResourceZipFilePath() { + return resourcePackZipFilePath; + } + public File getResourcePackZipFile() { return resourcePackZipFile; } @@ -26,16 +49,16 @@ public ResourcePackManager(TemplatePaperPlugin plugin) { this.plugin = plugin; saveResourcepackZipFile(); - - Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(plugin, this), plugin); } private void saveResourcepackZipFile() { plugin.getFileManager().saveResourceFileFolder(resourcePackResourceFolderName); + resourcePackZipFilePath = plugin.getDataPath() + File.separator + resourcePackResourceFolderName + FileUtil.getFileExtensionSeparator() + resourcePackFileExtension; + try { - FileUtil.zipFolder(Path.of(plugin.getDataPath() + File.separator + resourcePackResourceFolderName), Path.of(plugin.getDataPath() + File.separator + resourcePackResourceFolderName + ".zip")); - resourcePackZipFile = new File(plugin.getDataPath() + File.separator + resourcePackResourceFolderName + ".zip"); + FileUtil.zipFolder(Path.of(plugin.getDataPath() + File.separator + resourcePackResourceFolderName), Path.of(resourcePackZipFilePath)); + resourcePackZipFile = new File(resourcePackZipFilePath); File resourcePackFolder = new File(plugin.getDataPath() + File.separator + resourcePackResourceFolderName); FileUtils.deleteDirectory(resourcePackFolder); diff --git a/src/main/java/net/slqmy/template_paper_plugin/resource_pack/event/listeners/PlayerJoinListener.java b/src/main/java/net/slqmy/template_paper_plugin/resource_pack/event/listeners/PlayerJoinListener.java deleted file mode 100644 index 8505bab3..00000000 --- a/src/main/java/net/slqmy/template_paper_plugin/resource_pack/event/listeners/PlayerJoinListener.java +++ /dev/null @@ -1,37 +0,0 @@ -package net.slqmy.template_paper_plugin.resource_pack.event.listeners; - -import java.net.URI; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; - -import net.kyori.adventure.resource.ResourcePackCallback; -import net.kyori.adventure.resource.ResourcePackInfo; -import net.kyori.adventure.resource.ResourcePackRequest; -import net.kyori.adventure.text.Component; -import net.slqmy.template_paper_plugin.TemplatePaperPlugin; -import net.slqmy.template_paper_plugin.file.FileUtil; -import net.slqmy.template_paper_plugin.resource_pack.ResourcePackManager; - -public class PlayerJoinListener implements Listener { - - private ResourcePackInfo resourcePackInfo; - - public PlayerJoinListener(TemplatePaperPlugin plugin, ResourcePackManager resourcePackManager) { - resourcePackInfo = ResourcePackInfo.resourcePackInfo().hash(FileUtil.createSha1Hex(resourcePackManager.getResourcePackZipFile())).uri(URI.create("http://localhost:8000/")).build(); - } - - @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) { - Player player = event.getPlayer(); - player.sendResourcePacks( - ResourcePackRequest.resourcePackRequest().packs(resourcePackInfo).prompt(Component.text("Please download the resource pack!")).callback(ResourcePackCallback.onTerminal((uuid, audience) -> { - Bukkit.getLogger().info("SUCCESS"); - }, (uuid, audience) -> { - Bukkit.getLogger().info("FAILURE"); - })).required(true).build()); - } -}