Skip to content

Commit

Permalink
Finalise resource pack management
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Aug 11, 2024
1 parent 0a1d4bf commit 057d944
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
21 changes: 17 additions & 4 deletions src/main/java/net/slqmy/template_paper_plugin/file/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getResourceFileFolderResourceFilePaths(String resourceFileFolderPath) throws IOException {
ClassLoader classLoader = FileUtil.class.getClassLoader();

Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,43 @@
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 {

private final TemplatePaperPlugin plugin;

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;
}
Expand All @@ -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);
Expand Down

This file was deleted.

0 comments on commit 057d944

Please sign in to comment.