diff --git a/.gitignore b/.gitignore index e506ab9..8875983 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ -/.idea -/*.iml .gradle/ build/ +bin/ .project .classpath .settings/ diff --git a/build.gradle.kts b/build.gradle.kts index caa763e..f0250be 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -104,7 +104,7 @@ tasks.shadowJar { exclude("META-INF/**") exclude("LICENSE") archiveClassifier.set("") // Use empty string instead of null. - archiveFileName.set("Yamipa-${project.version}-.jar") + archiveFileName.set("Yamipa-${project.version}.jar") } tasks.jar { diff --git a/src/main/java/io/josemmo/bukkit/plugin/YamipaPlugin.java b/src/main/java/io/josemmo/bukkit/plugin/YamipaPlugin.java index 30c887a..bff046b 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/YamipaPlugin.java +++ b/src/main/java/io/josemmo/bukkit/plugin/YamipaPlugin.java @@ -1,234 +1,249 @@ package io.josemmo.bukkit.plugin; -import io.josemmo.bukkit.plugin.commands.ImageCommandBridge; -import io.josemmo.bukkit.plugin.renderer.*; -import io.josemmo.bukkit.plugin.storage.ImageStorage; -import org.bukkit.Bukkit; -import org.bukkit.event.HandlerList; -import org.bukkit.plugin.java.JavaPlugin; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.awt.Color; import java.nio.file.Path; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import io.josemmo.bukkit.plugin.commands.ImageCommandBridge; +import io.josemmo.bukkit.plugin.renderer.FakeEntity; +import io.josemmo.bukkit.plugin.renderer.FakeImage; +import io.josemmo.bukkit.plugin.renderer.FakeMap; +import io.josemmo.bukkit.plugin.renderer.ImageRenderer; +import io.josemmo.bukkit.plugin.renderer.ItemService; +import io.josemmo.bukkit.plugin.storage.ImageStorage; +import net.trueog.utilitiesog.UtilitiesOG; + public class YamipaPlugin extends JavaPlugin { - public static final int BSTATS_PLUGIN_ID = 10243; - private static YamipaPlugin instance; - private boolean verbose; - private ImageStorage storage; - private ImageRenderer renderer; - private ItemService itemService; - private ScheduledExecutorService scheduler; + private static YamipaPlugin instance; + private boolean verbose; + private ImageStorage storage; + private ImageRenderer renderer; + private ItemService itemService; + private ScheduledExecutorService scheduler; - /** - * Get plugin instance - * - * @return Plugin instance - */ - public static @NotNull YamipaPlugin getInstance() { + /** + * Get plugin instance + * + * @return Plugin instance + */ + public static @NotNull YamipaPlugin getInstance() { - return instance; + return instance; - } + } - /** - * Get image storage instance - * - * @return Image storage instance - */ - public @NotNull ImageStorage getStorage() { + public static String getPrefix() { - return storage; + return "&7[&eYamipa&f-&4OG&7] "; - } + } - /** - * Get image renderer instance - * - * @return Image renderer instance - */ - public @NotNull ImageRenderer getRenderer() { + /** + * Get image storage instance + * + * @return Image storage instance + */ + public @NotNull ImageStorage getStorage() { - return renderer; + return storage; - } + } - /** - * Get internal tasks scheduler - * - * @return Tasks scheduler - */ - public @NotNull ScheduledExecutorService getScheduler() { + /** + * Get image renderer instance + * + * @return Image renderer instance + */ + public @NotNull ImageRenderer getRenderer() { - return scheduler; + return renderer; - } + } - @Override - public void onLoad() { + /** + * Get internal tasks scheduler + * + * @return Tasks scheduler + */ + public @NotNull ScheduledExecutorService getScheduler() { - instance = this; + return scheduler; - } + } - @Override - public void onEnable() { + @Override + public void onLoad() { - // Initialize logger - verbose = getConfig().getBoolean("verbose", false); - if (verbose) { + instance = this; - info("Running on VERBOSE mode"); + } - } + @Override + public void onEnable() { - // Register plugin commands - ImageCommandBridge.register(this); + // Initialize logger + verbose = getConfig().getBoolean("verbose", false); + if (verbose) { - // Read plugin configuration paths - Path basePath = getDataFolder().toPath(); - String imagesPath = getConfig().getString("images-path", "images"); - String cachePath = getConfig().getString("cache-path", "cache"); - String dataPath = getConfig().getString("data-path", "images.dat"); + info("Running on VERBOSE mode"); - // Create image storage - storage = new ImageStorage(basePath.resolve(imagesPath).toString(), basePath.resolve(cachePath).toString()); - try { + } - storage.start(); + // Register plugin commands + ImageCommandBridge.register(this); - } catch (Exception e) { + // Read plugin configuration paths + final Path basePath = getDataFolder().toPath(); + final String imagesPath = getConfig().getString("images-path", "images"); + final String cachePath = getConfig().getString("cache-path", "cache"); + final String dataPath = getConfig().getString("data-path", "images.dat"); - log(Level.SEVERE, "Failed to initialize image storage", e); + // Create image storage + storage = new ImageStorage(basePath.resolve(imagesPath).toString(), basePath.resolve(cachePath).toString()); + try { - } + storage.start(); - // Create image renderer - boolean animateImages = getConfig().getBoolean("animate-images", true); - FakeImage.configure(animateImages); - info(animateImages ? "Enabled image animation support" : "Image animation support is disabled"); - renderer = new ImageRenderer(basePath.resolve(dataPath).toString()); - renderer.start(); + } catch (Exception e) { - // Create image item service - itemService = new ItemService(); - itemService.start(); + log(Level.SEVERE, "Failed to initialize image storage", e); - // Create thread pool - scheduler = Executors.newScheduledThreadPool(6); + } - // Warm-up plugin dependencies - fine("Waiting for ProtocolLib to be ready..."); - scheduler.execute(() -> { + // Create image renderer + final boolean animateImages = getConfig().getBoolean("animate-images", true); + FakeImage.configure(animateImages); + info(animateImages ? "Enabled image animation support" : "Image animation support is disabled"); + renderer = new ImageRenderer(basePath.resolve(dataPath).toString()); + renderer.start(); - FakeEntity.waitForProtocolLib(); - fine("ProtocolLib is now ready"); + // Create image item service + itemService = new ItemService(); + itemService.start(); - }); - fine("Triggered map color cache warm-up"); - FakeMap.pixelToIndex(Color.RED.getRGB()); // Ask for a color index to force cache generation + // Create thread pool + scheduler = Executors.newScheduledThreadPool(6); - } + // Warm-up plugin dependencies + fine("Waiting for ProtocolLib to be ready..."); + scheduler.execute(() -> { - @Override - public void onDisable() { + FakeEntity.waitForProtocolLib(); + fine("ProtocolLib is now ready"); - // Stop plugin components - storage.stop(); - renderer.stop(); - itemService.stop(); - storage = null; - renderer = null; - itemService = null; + }); + fine("Triggered map color cache warm-up"); + FakeMap.pixelToIndex(Color.RED.getRGB()); // Ask for a color index to force cache generation - // Stop internal scheduler - scheduler.shutdownNow(); - scheduler = null; + } - // Remove Bukkit listeners and tasks - HandlerList.unregisterAll(this); - Bukkit.getScheduler().cancelTasks(this); + @Override + public void onDisable() { - } + // Stop plugin components + storage.stop(); + renderer.stop(); + itemService.stop(); + storage = null; + renderer = null; + itemService = null; - /** - * Log message - * - * @param level Record level - * @param message Message - * @param e Throwable instance, NULL to ignore - */ - public void log(@NotNull Level level, @NotNull String message, @Nullable Throwable e) { + // Stop internal scheduler + scheduler.shutdownNow(); + scheduler = null; - // Fix log level - if (level.intValue() < Level.INFO.intValue()) { + // Remove Bukkit listeners and tasks + HandlerList.unregisterAll(this); + Bukkit.getScheduler().cancelTasks(this); - if (!verbose) - return; - level = Level.INFO; + } - } + /** + * Log message + * + * @param level Record level + * @param message Message + * @param e Throwable instance, NULL to ignore + */ + public void log(@NotNull Level level, @NotNull String message, @Nullable Throwable e) { - // Proxy record to real logger - if (e == null) { + // Fix log level + if (level.intValue() < Level.INFO.intValue()) { - getLogger().log(level, message); + if (!verbose) { + return; + } + level = Level.INFO; - } else { + } - getLogger().log(level, message, e); + // Proxy record to real logger + if (e == null) { - } + UtilitiesOG.logToConsole(getPrefix(), message); - } + } else { - /** - * Log message - * - * @param level Record level - * @param message Message - */ - public void log(@NotNull Level level, @NotNull String message) { + UtilitiesOG.logToConsole(getPrefix(), message); - log(level, message, null); + e.printStackTrace(); - } + } - /** - * Log warning message - * - * @param message Message - */ - public void warning(@NotNull String message) { + } - log(Level.WARNING, message); + /** + * Log message + * + * @param level Record level + * @param message Message + */ + public void log(@NotNull Level level, @NotNull String message) { - } + log(level, message, null); - /** - * Log info message - * - * @param message Message - */ - public void info(@NotNull String message) { + } - log(Level.INFO, message); + /** + * Log warning message + * + * @param message Message + */ + public void warning(@NotNull String message) { - } + log(Level.WARNING, message); - /** - * Log fine message - * - * @param message Message - */ - public void fine(@NotNull String message) { + } - log(Level.FINE, message); + /** + * Log info message + * + * @param message Message + */ + public void info(@NotNull String message) { - } + log(Level.INFO, message); -} + } + + /** + * Log fine message + * + * @param message Message + */ + public void fine(@NotNull String message) { + + log(Level.FINE, message); + + } + +} \ No newline at end of file diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java b/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java index 694c769..5881e6b 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java @@ -1,233 +1,244 @@ package io.josemmo.bukkit.plugin.commands; -import com.mojang.brigadier.builder.ArgumentBuilder; -import com.mojang.brigadier.builder.LiteralArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import io.josemmo.bukkit.plugin.commands.arguments.Argument; -import io.josemmo.bukkit.plugin.utils.Internals; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Predicate; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; + +import io.josemmo.bukkit.plugin.YamipaPlugin; +import io.josemmo.bukkit.plugin.commands.arguments.Argument; +import io.josemmo.bukkit.plugin.utils.Internals; +import net.trueog.utilitiesog.UtilitiesOG; + public class Command { - private final String name; - private final List arguments = new ArrayList<>(); - private Predicate requirementHandler = __ -> true; - private BiConsumer executesHandler = null; - private BiConsumer executesPlayerHandler = null; - private final List subcommands = new ArrayList<>(); + private final String name; + private final List arguments = new ArrayList<>(); + private Predicate requirementHandler = __ -> true; + private BiConsumer executesHandler = null; + private BiConsumer executesPlayerHandler = null; + private final List subcommands = new ArrayList<>(); - /** - * Command constructor - * - * @param name Command literal name - */ - public Command(@NotNull String name) { + /** + * Command constructor + * + * @param name Command literal name + */ + public Command(@NotNull String name) { - this.name = name; + this.name = name; - } + } - /** - * Add argument to command - * - * @param argument Argument instance - * @return This instance - */ - public @NotNull Command withArgument(@NotNull Argument argument) { + /** + * Add argument to command + * + * @param argument Argument instance + * @return This instance + */ + public @NotNull Command withArgument(@NotNull Argument argument) { - arguments.add(argument); - return this; + arguments.add(argument); + return this; - } + } - /** - * Add requirement handler to this command - * - * @param handler Handler to determine command availability - * @return This instance - */ - public @NotNull Command withRequirement(@NotNull Predicate handler) { + /** + * Add requirement handler to this command + * + * @param handler Handler to determine command availability + * @return This instance + */ + public @NotNull Command withRequirement(@NotNull Predicate handler) { - requirementHandler = handler; - return this; + requirementHandler = handler; + return this; - } + } - /** - * Add permission requirement to this command - * - * @param permissions Permission names (match as least one) - * @return This instance - */ - public @NotNull Command withPermission(@NotNull String... permissions) { + /** + * Add permission requirement to this command + * + * @param permissions Permission names (match as least one) + * @return This instance + */ + public @NotNull Command withPermission(@NotNull String... permissions) { - return withRequirement(sender -> { + return withRequirement(sender -> { - for (String permission : permissions) { + for (String permission : permissions) { - if (sender.hasPermission(permission)) { + if (sender.hasPermission(permission)) { - return true; + return true; - } + } - } + } - return false; + return false; - }); + }); - } + } - /** - * Add handler that will be executed when the command gets called - * - * @param handler Command handler - * @return This instance - */ - public @NotNull Command executes(@NotNull BiConsumer handler) { + /** + * Add handler that will be executed when the command gets called + * + * @param handler Command handler + * @return This instance + */ + public @NotNull Command executes(@NotNull BiConsumer handler) { - executesHandler = handler; - return this; + executesHandler = handler; + return this; - } + } - /** - * Add handler that will be executed when the command gets called by a player - * - * @param handler Command handler - * @return This instance - */ - public @NotNull Command executesPlayer(@NotNull BiConsumer handler) { + /** + * Add handler that will be executed when the command gets called by a player + * + * @param handler Command handler + * @return This instance + */ + public @NotNull Command executesPlayer(@NotNull BiConsumer handler) { - executesPlayerHandler = handler; - return this; + executesPlayerHandler = handler; + return this; - } + } - /** - * Add subcommand to this command - * - * @param name Subcommand literal name - * @return The new subcommand - */ - public @NotNull Command addSubcommand(@NotNull String name) { + /** + * Add subcommand to this command + * + * @param name Subcommand literal name + * @return The new subcommand + */ + public @NotNull Command addSubcommand(@NotNull String name) { - Command subcommand = new Command(name); - subcommands.add(subcommand); - return subcommand; + final Command subcommand = new Command(name); + subcommands.add(subcommand); + return subcommand; - } + } - /** - * Build command and all of its children - * - * @return Literal argument builder instance - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public @NotNull LiteralArgumentBuilder build() { + /** + * Build command and all of its children + * + * @return Literal argument builder instance + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + public @NotNull LiteralArgumentBuilder build() { - LiteralArgumentBuilder root = LiteralArgumentBuilder.literal(name); + final LiteralArgumentBuilder root = LiteralArgumentBuilder.literal(name); - // Add arguments and execution handler - buildElement(root, 0); + // Add arguments and execution handler + buildElement(root, 0); - // Add subcommands - for (Command subcommand : subcommands) { + // Add subcommands + subcommands.forEach(subcommand -> root.then((ArgumentBuilder) subcommand.build())); - root.then((ArgumentBuilder) subcommand.build()); + return root; - } + } - return root; + @SuppressWarnings({ "rawtypes", "unchecked" }) + private @NotNull ArgumentBuilder buildElement(@NotNull ArgumentBuilder parent, int argIndex) { - } + // Attach requirement handler to each command element + parent.requires(source -> { - @SuppressWarnings({ "rawtypes", "unchecked" }) - private @NotNull ArgumentBuilder buildElement(@NotNull ArgumentBuilder parent, int argIndex) { + final CommandSender sender = Internals.getBukkitSender(source); + return requirementHandler.test(sender); - // Attach requirement handler to each command element - parent.requires(source -> { + }); - CommandSender sender = Internals.getBukkitSender(source); - return requirementHandler.test(sender); + // Chain command elements from the bottom-up + if (argIndex < arguments.size()) { - }); + parent.then(buildElement(arguments.get(argIndex).build(), argIndex + 1)).executes(ctx -> { - // Chain command elements from the bottom-up - if (argIndex < arguments.size()) { + final CommandSender sender = Internals.getBukkitSender(ctx.getSource()); + final String argsErrorMessage = "&cERROR: Missing required arguments!"; + if(sender instanceof Player) { - parent.then(buildElement(arguments.get(argIndex).build(), argIndex + 1)).executes(ctx -> { + UtilitiesOG.trueogMessage((Player) sender, argsErrorMessage); - CommandSender sender = Internals.getBukkitSender(ctx.getSource()); - sender.sendMessage(ChatColor.RED + "Missing required arguments"); - return 1; + } + else { - }); - return parent; + UtilitiesOG.logToConsole(YamipaPlugin.getPrefix(), argsErrorMessage); - } + } - // Attach execution handler to last command element - if (executesPlayerHandler != null) { + return 1; - parent.executes(ctx -> { + }); + return parent; - CommandSender sender = Internals.getBukkitSender(ctx.getSource()); - if (sender instanceof Player) { + } - executesPlayerHandler.accept((Player) sender, getArgumentValues(sender, ctx)); + // Attach execution handler to last command element + if (executesPlayerHandler != null) { - } else { + parent.executes(ctx -> { - sender.sendMessage(ChatColor.RED + "Only in-game players can execute this command!"); + final CommandSender sender = Internals.getBukkitSender(ctx.getSource()); + if (sender instanceof Player) { - } + executesPlayerHandler.accept((Player) sender, getArgumentValues(sender, ctx)); - return 0; + } else { - }); + UtilitiesOG.logToConsole(YamipaPlugin.getPrefix(), "Only in-game players can execute this command!"); - } else if (executesHandler != null) { + } - parent.executes(ctx -> { + return 0; - CommandSender sender = Internals.getBukkitSender(ctx.getSource()); - executesHandler.accept(sender, getArgumentValues(sender, ctx)); - return 0; + }); - }); + } else if (executesHandler != null) { - } + parent.executes(ctx -> { - return parent; + final CommandSender sender = Internals.getBukkitSender(ctx.getSource()); + executesHandler.accept(sender, getArgumentValues(sender, ctx)); + return 0; - } + }); - private @NotNull Object[] getArgumentValues(@NotNull CommandSender sender, @NotNull CommandContext ctx) - throws CommandSyntaxException - { + } - List argValues = new ArrayList<>(); - argValues.add(ctx.getNodes().get(0).getNode().getName()); - for (Argument argument : arguments) { + return parent; - Object value = ctx.getArgument(argument.getName(), Object.class); - value = argument.parse(sender, value); - argValues.add(value); + } - } + private @NotNull Object[] getArgumentValues(@NotNull CommandSender sender, @NotNull CommandContext ctx) + throws CommandSyntaxException + { - return argValues.toArray(new Object[0]); + final List argValues = new ArrayList<>(); + argValues.add(ctx.getNodes().get(0).getNode().getName()); + for (Argument argument : arguments) { - } + Object value = ctx.getArgument(argument.getName(), Object.class); + value = argument.parse(sender, value); + argValues.add(value); -} + } + + return argValues.toArray(new Object[0]); + + } + +} \ No newline at end of file