From fb38607d8a2030f149b97bfc1a0e58dedea7507c Mon Sep 17 00:00:00 2001 From: 5vl Date: Tue, 21 Jun 2022 00:18:25 +0200 Subject: [PATCH] added a fuck ton of shit, idk if it works but im not gonna test rn --- .../java/me/fivevl/itemadder/AbilityType.kt | 8 ++++ .../me/fivevl/itemadder/InteractListener.kt | 43 +++++++++++++++++++ src/main/java/me/fivevl/itemadder/Item.kt | 19 ++++---- .../java/me/fivevl/itemadder/ItemsCommand.kt | 22 ++++++++++ src/main/java/me/fivevl/itemadder/ItemsGui.kt | 14 ++++++ src/main/java/me/fivevl/itemadder/Main.kt | 7 +++ src/main/java/me/fivevl/itemadder/Type.kt | 2 +- src/main/java/me/fivevl/itemadder/Utils.kt | 13 +++++- src/main/resources/plugin.yml | 3 ++ 9 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 src/main/java/me/fivevl/itemadder/AbilityType.kt create mode 100644 src/main/java/me/fivevl/itemadder/InteractListener.kt create mode 100644 src/main/java/me/fivevl/itemadder/ItemsCommand.kt create mode 100644 src/main/java/me/fivevl/itemadder/ItemsGui.kt diff --git a/src/main/java/me/fivevl/itemadder/AbilityType.kt b/src/main/java/me/fivevl/itemadder/AbilityType.kt new file mode 100644 index 0000000..563b939 --- /dev/null +++ b/src/main/java/me/fivevl/itemadder/AbilityType.kt @@ -0,0 +1,8 @@ +package me.fivevl.itemadder + +import org.bukkit.event.player.PlayerInteractEvent + +enum class AbilityType(val event: PlayerInteractEvent) { + LEFT_CLICK(Utils.interactEvent), + RIGHT_CLICK(Utils.interactEvent) +} \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/InteractListener.kt b/src/main/java/me/fivevl/itemadder/InteractListener.kt new file mode 100644 index 0000000..862a83a --- /dev/null +++ b/src/main/java/me/fivevl/itemadder/InteractListener.kt @@ -0,0 +1,43 @@ +package me.fivevl.itemadder + +import org.bukkit.entity.Player +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.block.Action +import org.bukkit.event.inventory.InventoryClickEvent +import org.bukkit.event.inventory.InventoryCloseEvent +import org.bukkit.event.player.PlayerInteractEvent + +class InteractListener : Listener { + @EventHandler + fun onInteract(e: PlayerInteractEvent) { + if (e.item == null) return + for (check in Utils.items.values) { + if (check.finalItem == e.item) { + for (ability in check.abilities.keys) { + if (check.abilities[ability]!! == AbilityType.RIGHT_CLICK && (e.action == Action.RIGHT_CLICK_AIR || e.action == Action.RIGHT_CLICK_BLOCK)) { + ability.run() + } + if (check.abilities[ability]!! == AbilityType.LEFT_CLICK && (e.action == Action.LEFT_CLICK_AIR || e.action == Action.LEFT_CLICK_BLOCK)) { + ability.run() + } + } + } + } + } + + @EventHandler + fun onInvClick(e: InventoryClickEvent) { + if (e.whoClicked !is Player) return + if (!Utils.inItemsGui.contains(e.whoClicked as Player)) return + e.isCancelled = true + if (e.currentItem == null) return + e.whoClicked.inventory.addItem(e.currentItem!!) + e.whoClicked.sendMessage(Utils.color("Added ${e.currentItem!!.itemMeta.displayName()} to your inventory")) + } + + @EventHandler + fun onInvClose(e: InventoryCloseEvent) { + Utils.inItemsGui.remove(e.player) + } +} \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/Item.kt b/src/main/java/me/fivevl/itemadder/Item.kt index 51822bb..80fb297 100644 --- a/src/main/java/me/fivevl/itemadder/Item.kt +++ b/src/main/java/me/fivevl/itemadder/Item.kt @@ -4,18 +4,21 @@ import net.kyori.adventure.text.Component import org.bukkit.Material import org.bukkit.inventory.ItemStack -class Item(uniqueName: String, name: Component, rarity: Rarity, material: Material, type: Type, vararg lore: String) { - object Items { - val items = HashMap() - } - - lateinit var finalItem: ItemStack +class Item(uniqueName: String, name: Component, rarity: Rarity, material: Material, type: Type, lore: String) { + val finalItem: ItemStack + val abilities = HashMap() init { val item = ItemStack(material) val meta = item.itemMeta meta.displayName(name) - meta.lore(Utils.loreBuilder()) - Items.items[uniqueName] = this + meta.lore(Utils.loreBuilder(lore, "", rarity.formatted + " " + type.type)) + item.itemMeta = meta + finalItem = item + Utils.items[uniqueName] = this + } + + fun addAbility(type: AbilityType, runnable: Runnable) { + abilities[runnable] = type } } \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/ItemsCommand.kt b/src/main/java/me/fivevl/itemadder/ItemsCommand.kt new file mode 100644 index 0000000..8dcb8d3 --- /dev/null +++ b/src/main/java/me/fivevl/itemadder/ItemsCommand.kt @@ -0,0 +1,22 @@ +package me.fivevl.itemadder + +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player + +class ItemsCommand : CommandExecutor { + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array?): Boolean { + if (sender !is Player) { + sender.sendMessage(Utils.color("Only players can use this command")) + return true + } + val p = sender.player!! + if (p.hasPermission("itemadder.items")) { + p.openInventory(ItemsGui.getGui()) + } else { + p.sendMessage(Utils.color("You don't have permission to use this command")) + } + return true + } +} \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/ItemsGui.kt b/src/main/java/me/fivevl/itemadder/ItemsGui.kt new file mode 100644 index 0000000..a0a3e20 --- /dev/null +++ b/src/main/java/me/fivevl/itemadder/ItemsGui.kt @@ -0,0 +1,14 @@ +package me.fivevl.itemadder + +import org.bukkit.Bukkit +import org.bukkit.inventory.Inventory + +object ItemsGui { + fun getGui(): Inventory { + val gui = Bukkit.createInventory(null, 54, Utils.color("Items")) + for (item in Utils.items.values) { + gui.addItem(item.finalItem) + } + return gui + } +} \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/Main.kt b/src/main/java/me/fivevl/itemadder/Main.kt index c7c01f6..a0b6f69 100644 --- a/src/main/java/me/fivevl/itemadder/Main.kt +++ b/src/main/java/me/fivevl/itemadder/Main.kt @@ -1,10 +1,17 @@ package me.fivevl.itemadder +import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.plugin.java.JavaPlugin class Main : JavaPlugin() { override fun onEnable() { + Bukkit.getPluginManager().registerEvents(InteractListener(), this) + getCommand("items")?.setExecutor(ItemsCommand()) Item("TEST_ITEM", Utils.color("Test Item"), Rarity.EPIC, Material.STONE_SWORD, Type.WEAPON, "The most basic testing\nsword, by 5vl") + Utils.items["TEST_ITEM"]!!.addAbility(AbilityType.RIGHT_CLICK) { + val e = AbilityType.RIGHT_CLICK.event + e.player.sendMessage(Utils.color("You right clicked the TEST_ITEM!")) + } } } \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/Type.kt b/src/main/java/me/fivevl/itemadder/Type.kt index 11ac07d..6f4546d 100644 --- a/src/main/java/me/fivevl/itemadder/Type.kt +++ b/src/main/java/me/fivevl/itemadder/Type.kt @@ -1,6 +1,6 @@ package me.fivevl.itemadder -enum class Type(type: String) { +enum class Type(val type: String) { WEAPON("WEAPON"), PICKAXE("PICKAXE"), ITEM("ITEM"), diff --git a/src/main/java/me/fivevl/itemadder/Utils.kt b/src/main/java/me/fivevl/itemadder/Utils.kt index d9dd59a..73bfb30 100644 --- a/src/main/java/me/fivevl/itemadder/Utils.kt +++ b/src/main/java/me/fivevl/itemadder/Utils.kt @@ -2,15 +2,26 @@ package me.fivevl.itemadder import net.kyori.adventure.text.Component import net.kyori.adventure.text.minimessage.MiniMessage +import org.bukkit.entity.Player +import org.bukkit.event.player.PlayerInteractEvent object Utils { + lateinit var interactEvent: PlayerInteractEvent + val items = HashMap() + val inItemsGui = ArrayList() fun color(s: String): Component { return MiniMessage.miniMessage().deserialize(s) } fun loreBuilder(vararg arr: String): ArrayList { val lore = ArrayList() for (s in arr) { - lore.add(color(s)) + if (s.contains("\n")) { + for (ss in s.split("\n")) { + lore.add(color(ss)) + } + } else { + lore.add(color(s)) + } } return lore } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 25b73db..228abf9 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,3 +4,6 @@ main: me.fivevl.itemadder.Main api-version: 1.18 authors: [ 5vl ] description: Create custom items with custom abilities! +commands: + items: + description: Main item GUI. \ No newline at end of file