Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arutaka1220 authored May 28, 2024
1 parent fbd8197 commit 2cc021e
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 0 deletions.
93 changes: 93 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.suikamcbe</groupId>
<artifactId>MapImage</artifactId>
<version>${local.version}</version>

<properties>
<local.version>1.0.0</local.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://www.jitpack.io</url>
</repository>
<repository>
<id>opencollab-repository-maven-releases</id>
<name>Opencollab Repository releases</name>
<url>https://repo.opencollab.dev/maven-releases</url>
</repository>
<repository>
<id>opencollab-repository-maven-snapshots</id>
<name>Opencollab Repository snapshots</name>
<url>https://repo.opencollab.dev/maven-snapshots</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.PowerNukkitX</groupId>
<artifactId>PowerNukkitX</artifactId>
<version>nightly-build</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<defaultGoal>clean package</defaultGoal>

<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>

<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>MapImage-${local.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest><mainClass>io.github.suikamcbe.MapImagePlugin</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
62 changes: 62 additions & 0 deletions src/main/java/io/github/suikamcbe/MapImagePlugin.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
114 changes: 114 additions & 0 deletions src/main/java/io/github/suikamcbe/command/MapCommand.java
Original file line number Diff line number Diff line change
@@ -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<String, ParamList> 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;
}
}
6 changes: 6 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2cc021e

Please sign in to comment.