diff --git a/build.gradle b/build.gradle index 2059d33..9a47136 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,26 @@ spigot { main "ru.themixray.puton.Main" apiVersion "1.17" named "PutOn" + + command { + named "helmet" + description "Puts on item from your hand as helmet" + } + + command { + named "chestplate" + description "Puts on item from your hand as chestplate" + } + + command { + named "leggings" + description "Puts on item from your hand as leggings" + } + + command { + named "boots" + description "Puts on item from your hand as boots" + } } } diff --git a/src/main/java/ru/themixray/puton/Main.java b/src/main/java/ru/themixray/puton/Main.java index f3f2e22..07c8656 100644 --- a/src/main/java/ru/themixray/puton/Main.java +++ b/src/main/java/ru/themixray/puton/Main.java @@ -1,50 +1,47 @@ package ru.themixray.puton; import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; +import org.bukkit.command.*; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryDragEvent; -import org.bukkit.event.inventory.InventoryType; -import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; +import ru.themixray.puton.command.BootsCommand; +import ru.themixray.puton.command.ChestplateCommand; +import ru.themixray.puton.command.HelmetCommand; +import ru.themixray.puton.command.LeggingsCommand; +import ru.themixray.puton.listener.SwapItemListener; import ru.themixray.puton.util.UnrealConfig; -import java.util.Objects; +import java.lang.reflect.Field; +import java.util.List; public class Main extends JavaPlugin implements Listener { public void onEnable() { Config.loadConfig(new UnrealConfig(this, getDataFolder(), "config.yml")); - Bukkit.getPluginManager().registerEvents(this, this); + Bukkit.getPluginManager().registerEvents(new SwapItemListener(), this); + registerCommand(getCommand("helmet"), Config.HELMET_COMMANDS, new HelmetCommand()); + registerCommand(getCommand("chestplate"), Config.CHESTPLATE_COMMANDS, new ChestplateCommand()); + registerCommand(getCommand("leggings"), Config.LEGGINGS_COMMANDS, new LeggingsCommand()); + registerCommand(getCommand("boots"), Config.BOOTS_COMMANDS, new BootsCommand()); } - public static final int HELMET_SLOT = 39; - public static final int CHESTPLATE_SLOT = 40; - public static final int LEGGINGS_SLOT = 41; - public static final int BOOTS_SLOT = 42; - - @EventHandler - public void onInventoryClick(InventoryClickEvent e) { - ItemStack item = e.getCursor(); - - if (e.getSlotType() == InventoryType.SlotType.ARMOR) { - if (e.getSlot() == HELMET_SLOT && Config.ALLOW_HELMET) { - e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); - e.getWhoClicked().getInventory().setHelmet(item); - e.setCancelled(true); - } else if (e.getSlot() == CHESTPLATE_SLOT && Config.ALLOW_CHESTPLATE) { - e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); - e.getWhoClicked().getInventory().setHelmet(item); - e.setCancelled(true); - } else if (e.getSlot() == LEGGINGS_SLOT && Config.ALLOW_LEGGINGS) { - e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); - e.getWhoClicked().getInventory().setHelmet(item); - e.setCancelled(true); - } else if (e.getSlot() == BOOTS_SLOT && Config.ALLOW_BOOTS) { - e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); - e.getWhoClicked().getInventory().setHelmet(item); - e.setCancelled(true); - } + public CommandMap getCommandMap() { + try { + Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); + bukkitCommandMap.setAccessible(true); + return (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); + } catch (Exception e) { + throw new RuntimeException(e); } } + + public void registerCommand(PluginCommand command, List names, CommandExecutor executor) { + if (names.isEmpty()) { + command.unregister(getCommandMap()); + return; + } + command.setExecutor(executor); + command.setName(names.get(0)); + if (names.size() > 1) command.setAliases(names.subList(1, names.size())); + command.setTabCompleter((a, b, c, d) -> List.of()); + } } \ No newline at end of file diff --git a/src/main/java/ru/themixray/puton/commands/BootsCommand.java b/src/main/java/ru/themixray/puton/command/BootsCommand.java similarity index 94% rename from src/main/java/ru/themixray/puton/commands/BootsCommand.java rename to src/main/java/ru/themixray/puton/command/BootsCommand.java index 352e890..0371a56 100644 --- a/src/main/java/ru/themixray/puton/commands/BootsCommand.java +++ b/src/main/java/ru/themixray/puton/command/BootsCommand.java @@ -1,4 +1,4 @@ -package ru.themixray.puton.commands; +package ru.themixray.puton.command; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; diff --git a/src/main/java/ru/themixray/puton/commands/ChestplateCommand.java b/src/main/java/ru/themixray/puton/command/ChestplateCommand.java similarity index 94% rename from src/main/java/ru/themixray/puton/commands/ChestplateCommand.java rename to src/main/java/ru/themixray/puton/command/ChestplateCommand.java index c1d4d5b..13ed5b7 100644 --- a/src/main/java/ru/themixray/puton/commands/ChestplateCommand.java +++ b/src/main/java/ru/themixray/puton/command/ChestplateCommand.java @@ -1,4 +1,4 @@ -package ru.themixray.puton.commands; +package ru.themixray.puton.command; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; diff --git a/src/main/java/ru/themixray/puton/commands/HelmetCommand.java b/src/main/java/ru/themixray/puton/command/HelmetCommand.java similarity index 94% rename from src/main/java/ru/themixray/puton/commands/HelmetCommand.java rename to src/main/java/ru/themixray/puton/command/HelmetCommand.java index 6655a94..e87e113 100644 --- a/src/main/java/ru/themixray/puton/commands/HelmetCommand.java +++ b/src/main/java/ru/themixray/puton/command/HelmetCommand.java @@ -1,4 +1,4 @@ -package ru.themixray.puton.commands; +package ru.themixray.puton.command; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; diff --git a/src/main/java/ru/themixray/puton/commands/LeggingsCommand.java b/src/main/java/ru/themixray/puton/command/LeggingsCommand.java similarity index 94% rename from src/main/java/ru/themixray/puton/commands/LeggingsCommand.java rename to src/main/java/ru/themixray/puton/command/LeggingsCommand.java index 2f51a58..91d8992 100644 --- a/src/main/java/ru/themixray/puton/commands/LeggingsCommand.java +++ b/src/main/java/ru/themixray/puton/command/LeggingsCommand.java @@ -1,4 +1,4 @@ -package ru.themixray.puton.commands; +package ru.themixray.puton.command; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; diff --git a/src/main/java/ru/themixray/puton/listener/SwapItemListener.java b/src/main/java/ru/themixray/puton/listener/SwapItemListener.java new file mode 100644 index 0000000..3ccc17e --- /dev/null +++ b/src/main/java/ru/themixray/puton/listener/SwapItemListener.java @@ -0,0 +1,40 @@ +package ru.themixray.puton.listener; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.ItemStack; +import ru.themixray.puton.Config; + +public class SwapItemListener implements Listener { + public static final int HELMET_SLOT = 39; + public static final int CHESTPLATE_SLOT = 40; + public static final int LEGGINGS_SLOT = 41; + public static final int BOOTS_SLOT = 42; + + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + ItemStack item = e.getCursor(); + + if (e.getSlotType() == InventoryType.SlotType.ARMOR) { + if (e.getSlot() == HELMET_SLOT && Config.ALLOW_HELMET) { + e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); + e.getWhoClicked().getInventory().setHelmet(item); + e.setCancelled(true); + } else if (e.getSlot() == CHESTPLATE_SLOT && Config.ALLOW_CHESTPLATE) { + e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); + e.getWhoClicked().getInventory().setHelmet(item); + e.setCancelled(true); + } else if (e.getSlot() == LEGGINGS_SLOT && Config.ALLOW_LEGGINGS) { + e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); + e.getWhoClicked().getInventory().setHelmet(item); + e.setCancelled(true); + } else if (e.getSlot() == BOOTS_SLOT && Config.ALLOW_BOOTS) { + e.getWhoClicked().setItemOnCursor(e.getCurrentItem()); + e.getWhoClicked().getInventory().setHelmet(item); + e.setCancelled(true); + } + } + } +}