From 2cc021e62ba834d88e9c9eed1963a4c4c576f322 Mon Sep 17 00:00:00 2001 From: arutaka1220 <97819325+arutaka1220@users.noreply.github.com> Date: Tue, 28 May 2024 20:06:37 +0900 Subject: [PATCH] first commit --- pom.xml | 93 ++++++++++++++ .../io/github/suikamcbe/MapImagePlugin.java | 62 ++++++++++ .../github/suikamcbe/command/MapCommand.java | 114 ++++++++++++++++++ src/main/resources/plugin.yml | 6 + 4 files changed, 275 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/io/github/suikamcbe/MapImagePlugin.java create mode 100644 src/main/java/io/github/suikamcbe/command/MapCommand.java create mode 100644 src/main/resources/plugin.yml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b0c5274 --- /dev/null +++ b/pom.xml @@ -0,0 +1,93 @@ + + + 4.0.0 + + io.github.suikamcbe + MapImage + ${local.version} + + + 1.0.0 + 21 + 21 + UTF-8 + UTF-8 + + + + + central + https://repo.maven.apache.org/maven2/ + + + jitpack.io + https://www.jitpack.io + + + opencollab-repository-maven-releases + Opencollab Repository releases + https://repo.opencollab.dev/maven-releases + + + opencollab-repository-maven-snapshots + Opencollab Repository snapshots + https://repo.opencollab.dev/maven-snapshots + + + + + + com.github.PowerNukkitX + PowerNukkitX + nightly-build + provided + + + org.projectlombok + lombok + 1.18.30 + provided + + + + + clean package + + + + . + true + ${basedir}/src/main/resources + + + + + + maven-assembly-plugin + 3.7.1 + + + make-assembly + package + + single + + + + + MapImage-${local.version} + false + + jar-with-dependencies + + + io.github.suikamcbe.MapImagePlugin + + + + + + + \ No newline at end of file diff --git a/src/main/java/io/github/suikamcbe/MapImagePlugin.java b/src/main/java/io/github/suikamcbe/MapImagePlugin.java new file mode 100644 index 0000000..0b4a57d --- /dev/null +++ b/src/main/java/io/github/suikamcbe/MapImagePlugin.java @@ -0,0 +1,62 @@ +package io.github.suikamcbe; + +import cn.nukkit.Nukkit; +import cn.nukkit.Player; +import cn.nukkit.Server; +import cn.nukkit.item.ItemFilledMap; +import cn.nukkit.plugin.PluginBase; +import io.github.suikamcbe.command.MapCommand; +import lombok.Getter; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.net.URI; +import java.net.URL; + +public class MapImagePlugin extends PluginBase { + @Getter + private static MapImagePlugin instance; + + @Override + public void onEnable() { + instance = this; + + this.getDataFolder().mkdirs(); + + File imagesFolder = new File(this.getDataFolder() + "/images"); + + imagesFolder.mkdirs(); + + this.getServer().getCommandMap() + .register("MapImage", new MapCommand("mapimage")); + } + + public ItemFilledMap setMapImage(ItemFilledMap item, String urlOrFileName) throws Exception { + if(urlOrFileName.startsWith("http")) { + BufferedImage image = getBufferedImageFromURL(urlOrFileName); + + item.setImage(image); + + for(Player p : Server.getInstance().getOnlinePlayers().values()) { + item.sendImage(p); + } + + return item; + } else { + item.setImage(new File(this.getDataFolder() + "/images/" + urlOrFileName)); + + for(Player p : Server.getInstance().getOnlinePlayers().values()) { + item.sendImage(p); + } + + return item; + } + } + + public BufferedImage getBufferedImageFromURL(String link) throws Exception { + URL url = URI.create(link).toURL(); + + return ImageIO.read(url); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/suikamcbe/command/MapCommand.java b/src/main/java/io/github/suikamcbe/command/MapCommand.java new file mode 100644 index 0000000..72aebc1 --- /dev/null +++ b/src/main/java/io/github/suikamcbe/command/MapCommand.java @@ -0,0 +1,114 @@ +package io.github.suikamcbe.command; + +import cn.nukkit.Player; +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; +import cn.nukkit.command.data.CommandParamType; +import cn.nukkit.command.data.CommandParameter; +import cn.nukkit.command.tree.ParamList; +import cn.nukkit.command.utils.CommandLogger; +import cn.nukkit.item.Item; +import cn.nukkit.item.ItemFilledMap; +import cn.nukkit.permission.Permission; +import cn.nukkit.utils.TextFormat; +import io.github.suikamcbe.MapImagePlugin; + +import java.io.File; +import java.util.Map; +import java.util.Objects; + +public class MapCommand extends Command { + public MapCommand(String name) { + super(name); + + this.setPermission(Permission.DEFAULT_OP); + + this.commandParameters.clear(); + this.commandParameters.put("default", new CommandParameter[]{ + CommandParameter.newEnum("type", new String[]{"url", "file"}), + CommandParameter.newType("url_or_filename", CommandParamType.TEXT) + }); + + this.enableParamTree(); + } + + @Override + public int execute( + CommandSender sender, + String commandLabel, + Map.Entry result, + CommandLogger log + ) { + if(!sender.isPlayer()) { + log.addError("このコマンドはプレイヤーのみ実行できます。\n - This command can only be executed by players."); + log.output(); + + return 0; + } + + Player player = sender.asPlayer(); + ParamList list = result.getValue(); + + String type = list.getResult(0); + String urlOrFileName = list.getResult(1); + + if(Objects.equals(type, "url") && !urlOrFileName.startsWith("http")) { + log.addError("無効なURLです。URLはhttpまたはhttpsで始まる必要があります。\n - Invalid URL. URL must start with http or https."); + log.output(); + + return 0; + } + + if(Objects.equals(type, "url") && !urlOrFileName.endsWith(".png")) { + log.addError("無効なURLです。画像は.png形式である必要があります。\n - Invalid URL. Image must be in .png format."); + log.output(); + + return 0; + } + + if(Objects.equals(type, "file") && !urlOrFileName.endsWith(".png")) { + log.addError("無効なファイル名です。画像は.png形式である必要があります。\n - Invalid file name. Image must be in .png format."); + log.output(); + + return 0; + } + + if(Objects.equals(type, "file")) { + File file = new File(MapImagePlugin.getInstance().getDataFolder().toString(), "images/" + urlOrFileName); + + if(!file.exists()) { + log.addError(TextFormat.GRAY + urlOrFileName + TextFormat.RED + "は存在しないファイルです。画像を正しくimagesフォルダ内に配置できていますか?"); + log.output(); + + return 0; + } + } + + Item item = player.getInventory().getItemInHand(); + + if(item instanceof ItemFilledMap map) { + try { + ItemFilledMap filledItem = MapImagePlugin.getInstance().setMapImage(map, urlOrFileName); + + player.getInventory().setItemInHand(filledItem); + + log.addSuccess("マップの画像を設定しました。\n - Set the image of the map."); + log.output(); + } catch(Exception e) { + log.addError("マップの画像を設定できませんでした。\n - Failed to set the image of the map."); + log.output(); + + MapImagePlugin.getInstance().getLogger().error("Failed to set the image of the map.", e); + + return 0; + } + } else { + log.addError("手に持っているアイテムがマップではありません。\n - The item you are holding is not a map."); + log.output(); + + return 0; + } + + return 1; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..0e85e00 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ +name: MapImage +description: A PNX plugin that allows you to display images on the map. +version: "${pom.version}" +api: ["2.0.0"] +author: SuikaMCBE +main: io.github.suikamcbe.MapImagePlugin \ No newline at end of file