diff --git a/src/main/java/com/leohabrom/velocity/customServerIcon/CustomServerIcon.java b/src/main/java/com/leohabrom/velocity/customServerIcon/CustomServerIcon.java index b493d43..f3ca828 100644 --- a/src/main/java/com/leohabrom/velocity/customServerIcon/CustomServerIcon.java +++ b/src/main/java/com/leohabrom/velocity/customServerIcon/CustomServerIcon.java @@ -1,6 +1,9 @@ package com.leohabrom.velocity.customServerIcon; import com.google.inject.Inject; +import com.velocitypowered.api.command.CommandManager; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.RawCommand; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.plugin.Plugin; @@ -70,6 +73,12 @@ public class CustomServerIcon { return; } + CommandManager commandManager = server.getCommandManager(); + CommandMeta commandMeta = commandManager.metaBuilder("reload-icons") + .aliases("iconrel","relicons") + .plugin(this).build(); + RawCommand command = new ReloadCommand(customIcons,dataDirectory,logger); + commandManager.register(commandMeta,command); server.getEventManager().register(this, new PluginListener(logger,customIcons)); logger.info("Registered Plugin"); diff --git a/src/main/java/com/leohabrom/velocity/customServerIcon/ReloadCommand.java b/src/main/java/com/leohabrom/velocity/customServerIcon/ReloadCommand.java new file mode 100644 index 0000000..d94c3a2 --- /dev/null +++ b/src/main/java/com/leohabrom/velocity/customServerIcon/ReloadCommand.java @@ -0,0 +1,69 @@ +package com.leohabrom.velocity.customServerIcon; + +import com.velocitypowered.api.command.RawCommand; +import net.kyori.adventure.text.Component; +import org.slf4j.Logger; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.concurrent.ConcurrentHashMap; + +public class ReloadCommand implements RawCommand { + private final ConcurrentHashMap customIcons; + private final Path dataDirectory; + private final Logger logger; + + public ReloadCommand(ConcurrentHashMap customIcons, Path dataDirectory, Logger logger) { + this.dataDirectory = dataDirectory; + this.customIcons = customIcons; + this.logger = logger; + } + + @Override + public void execute(final Invocation invocation) { + invocation.source().sendMessage(Component.text("reloading icons")); + try { + File dataDir = dataDirectory.toFile(); + if ((!dataDir.exists() && !dataDir.mkdirs()) || !dataDir.isDirectory()) { + logger.warn("data dir doesn't exist!"); + return; + } + File[] files = dataDir.listFiles((dir, name) -> name.endsWith(".png")); + if (files == null) { + logger.warn("couldn't read files in the data directory!"); + invocation.source().sendMessage(Component.text("couldn't read files in the data directory!")); + return; + } + logger.info("Found {} images in the data directory.",files.length); + for (File file : files) { + String hostname = file.getName().substring(0,file.getName().length()-4); + try { + BufferedImage image = ImageIO.read(file); + if (image != null) { + if (image.getWidth() != 64 || image.getHeight() != 64) { + logger.warn("image {} is not 64x64 pixels, skipping",file.getName()); + invocation.source().sendMessage(Component.text("image "+file.getName()+" is not 64x64 pixels, skipping")); + continue; + } + customIcons.put(hostname,image); + invocation.source().sendMessage(Component.text("added custom icon for " + hostname)); + logger.info("added custom icon for {}",hostname); + } + } catch (IOException e) { + logger.warn("couldn't read image {}", file.getName()); + } + } + + } catch (SecurityException securityException) { + logger.warn("no read access in the data directory!"); + } + } + + @Override + public boolean hasPermission(final Invocation invocation) { + return invocation.source().hasPermission("command.reload-icons"); + } +}