From 17b39c158e9d5c5faab322cd930997f8adacc478 Mon Sep 17 00:00:00 2001 From: heyxmirko Date: Mon, 29 Apr 2024 17:14:16 +0200 Subject: [PATCH] feat: Add `/imagefireworkspro` command --- pom.xml | 6 +- .../imagefireworkspro/ImageFireworksPro.java | 7 ++ .../commands/CommandHandler.java | 89 +++++++++++++++++++ .../imagefireworkspro/core/ImageFirework.java | 9 ++ .../imagefireworkspro/util/ConfigManager.java | 28 ++++++ src/main/resources/plugin.yml | 6 ++ 6 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 src/main/java/me/lukyn76/imagefireworkspro/commands/CommandHandler.java diff --git a/pom.xml b/pom.xml index 2545cec..5f078d5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.lukyn76 ImageFireworksPro - 1.0.1 + 1.1.0 jar ImageFireworksPro @@ -23,8 +23,8 @@ maven-compiler-plugin 3.8.1 - ${java.version} - ${java.version} + 16 + 16 diff --git a/src/main/java/me/lukyn76/imagefireworkspro/ImageFireworksPro.java b/src/main/java/me/lukyn76/imagefireworkspro/ImageFireworksPro.java index 7a81c36..6bd3b75 100644 --- a/src/main/java/me/lukyn76/imagefireworkspro/ImageFireworksPro.java +++ b/src/main/java/me/lukyn76/imagefireworkspro/ImageFireworksPro.java @@ -1,5 +1,6 @@ package me.lukyn76.imagefireworkspro; +import me.lukyn76.imagefireworkspro.commands.CommandHandler; import me.lukyn76.imagefireworkspro.listeners.FireworkExplodeListener; import org.bukkit.plugin.java.JavaPlugin; @@ -15,6 +16,7 @@ public void onEnable() { saveDefaultConfig(); createFoldersAndFiles(); registerListeners(); + registerCommands(); getLogger().info("Plugin has been loaded!"); } @@ -41,6 +43,11 @@ private void registerListeners() { getLogger().info("Listeners registered!"); } + private void registerCommands() { + this.getCommand("imagefireworkspro").setExecutor(new CommandHandler()); + this.getCommand("imagefireworkspro").setTabCompleter(new CommandHandler()); + } + public static ImageFireworksPro getInstance() { return instance; } diff --git a/src/main/java/me/lukyn76/imagefireworkspro/commands/CommandHandler.java b/src/main/java/me/lukyn76/imagefireworkspro/commands/CommandHandler.java new file mode 100644 index 0000000..459913b --- /dev/null +++ b/src/main/java/me/lukyn76/imagefireworkspro/commands/CommandHandler.java @@ -0,0 +1,89 @@ +package me.lukyn76.imagefireworkspro.commands; + +import me.lukyn76.imagefireworkspro.ImageFireworksPro; +import me.lukyn76.imagefireworkspro.core.ImageFirework; +import me.lukyn76.imagefireworkspro.util.ConfigManager; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class CommandHandler implements CommandExecutor, TabCompleter { + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + + + switch (args[0].toLowerCase()) { + case "reload": + ConfigManager.reloadConfig(); + sender.sendMessage("§aConfig reloaded!"); + break; + case "give": + if (args.length < 2) { + sender.sendMessage("§cUsage: /imagefireworkspro give "); + break; + } + + Player player = ImageFireworksPro.getInstance().getServer().getPlayer(args[1]); + if (player == null) { + sender.sendMessage("§cPlayer not found!"); + break; + } + + String fireworkName = args[2]; + ImageFirework imageFirework = ConfigManager.getImageFirework(fireworkName); + if (imageFirework != null) { + + + // Give firework + int amount = 1; + if (args.length == 4) { + amount = Integer.parseInt(args[3]); + } + ItemStack firework = new ItemStack(Material.FIREWORK_ROCKET, amount); + ItemMeta fireworkMeta = firework.getItemMeta(); + + fireworkMeta.setDisplayName(imageFirework.getName()); + fireworkMeta.setCustomModelData(imageFirework.getCustomModelData()); + firework.setItemMeta(fireworkMeta); + + player.getInventory().addItem(firework); + sender.sendMessage("§aFirework given!"); + + } else { + sender.sendMessage("§cFirework not found!"); + } + break; + + default: + sender.sendMessage("§cUsage: /imagefireworkspro "); + break; + } + + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String s, String[] args) { + if (args.length == 1) { + return List.of("reload", "give"); + } else if (args.length == 2) { + return new ArrayList<>(Bukkit.getOnlinePlayers().stream().map(Player::getName).toList()); + + } else if (args.length == 3) { + return new ArrayList<>(Objects.requireNonNull(ConfigManager.getAvailableImageFireworks())); + }else if (args.length == 4) { + return List.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); + } + return null; + } +} diff --git a/src/main/java/me/lukyn76/imagefireworkspro/core/ImageFirework.java b/src/main/java/me/lukyn76/imagefireworkspro/core/ImageFirework.java index 4c67103..8dec988 100644 --- a/src/main/java/me/lukyn76/imagefireworkspro/core/ImageFirework.java +++ b/src/main/java/me/lukyn76/imagefireworkspro/core/ImageFirework.java @@ -17,6 +17,7 @@ public class ImageFirework { private String name; private String imageName; + private int customModelData; public String getName() { @@ -35,6 +36,14 @@ public void setImageName(String imageName) { this.imageName = imageName; } + public int getCustomModelData() { + return customModelData; + } + + public void setCustomModelData(int customModelData) { + this.customModelData = customModelData; + } + public void explode(Location location, double yawRotation) throws IOException { displayImage(location, yawRotation); } diff --git a/src/main/java/me/lukyn76/imagefireworkspro/util/ConfigManager.java b/src/main/java/me/lukyn76/imagefireworkspro/util/ConfigManager.java index 3d78937..42e9c08 100644 --- a/src/main/java/me/lukyn76/imagefireworkspro/util/ConfigManager.java +++ b/src/main/java/me/lukyn76/imagefireworkspro/util/ConfigManager.java @@ -4,6 +4,9 @@ import me.lukyn76.imagefireworkspro.core.ImageFirework; import org.bukkit.configuration.ConfigurationSection; +import java.util.List; +import java.util.Set; + public class ConfigManager { static ImageFireworksPro plugin = ImageFireworksPro.getInstance(); @@ -20,10 +23,35 @@ public static ImageFirework getImageFirework(int customModelData) { ImageFirework firework = new ImageFirework(); firework.setName(fireworkSection.getString("name")); firework.setImageName(fireworkSection.getString("imageName")); + firework.setCustomModelData(fireworkSection.getInt("customModelData")); return firework; } } return null; } + + public static ImageFirework getImageFirework(String id) { + ConfigurationSection fireworksSection = plugin.getConfig().getConfigurationSection("Fireworks"); + if (fireworksSection == null) return null; // No fireworks configuration found + ConfigurationSection fireworkSection = fireworksSection.getConfigurationSection(id); + if (fireworkSection == null) return null; + + ImageFirework firework = new ImageFirework(); + firework.setName(fireworkSection.getString("name")); + firework.setImageName(fireworkSection.getString("imageName")); + firework.setCustomModelData(fireworkSection.getInt("customModelData")); + + return firework; + } + + public static Set getAvailableImageFireworks() { + ConfigurationSection fireworksSection = plugin.getConfig().getConfigurationSection("Fireworks"); + if (fireworksSection == null) return null; // No fireworks configuration found + return fireworksSection.getKeys(false); + } + + public static void reloadConfig() { + plugin.reloadConfig(); + } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 73d436f..821fec6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,3 +2,9 @@ name: ImageFireworksPro version: '${project.version}' main: me.lukyn76.imagefireworkspro.ImageFireworksPro api-version: '1.20' +commands: + imagefireworkspro: + description: Give custom image fireworks to players. + permission: imagefireworkspro.command + aliases: [ifp] + default: op