diff --git a/src/main/java/me/fivevl/itemadder/Ability.kt b/src/main/java/me/fivevl/itemadder/Ability.kt new file mode 100644 index 0000000..fe5a192 --- /dev/null +++ b/src/main/java/me/fivevl/itemadder/Ability.kt @@ -0,0 +1,20 @@ +package me.fivevl.itemadder + +import org.bukkit.entity.Player +import org.bukkit.event.player.PlayerInteractEvent + +class Ability(val name: String, private val cooldown: Double?, private val runnable: (PlayerInteractEvent) -> Unit) { + private val cooldowns = HashMap() + + fun run(e: PlayerInteractEvent): Double? { + if (cooldown != null) { + val player = e.player + val now = System.currentTimeMillis() + val last = cooldowns[player] ?: 0L + if (now - last < cooldown * 1000) return ((now - last) / 1000).toDouble() + cooldowns[player] = now + } + runnable.invoke(e) + return null + } +} \ 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 index f1a6184..a6f96d4 100644 --- a/src/main/java/me/fivevl/itemadder/InteractListener.kt +++ b/src/main/java/me/fivevl/itemadder/InteractListener.kt @@ -8,19 +8,21 @@ import org.bukkit.event.block.Action import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.inventory.InventoryCloseEvent import org.bukkit.event.player.PlayerInteractEvent +import kotlin.math.roundToInt class InteractListener : Listener { @EventHandler fun onInteract(e: PlayerInteractEvent) { if (e.item == null) return for (check in Items.items.values) { - if (check.finalItem == e.item) { + if (check.finalItem.isSimilar(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.invoke(e) - } - if (check.abilities[ability]!! == AbilityType.LEFT_CLICK && (e.action == Action.LEFT_CLICK_AIR || e.action == Action.LEFT_CLICK_BLOCK)) { - ability.invoke(e) + if ((check.abilities[ability]!! == AbilityType.RIGHT_CLICK && (e.action == Action.RIGHT_CLICK_AIR || e.action == Action.RIGHT_CLICK_BLOCK)) || (check.abilities[ability]!! == AbilityType.LEFT_CLICK && (e.action == Action.LEFT_CLICK_AIR || e.action == Action.LEFT_CLICK_BLOCK))) { + val cooldownSeconds = ability.run(e) + if (cooldownSeconds != null) { + // This counts up i cba to change rn + e.player.sendMessage(Utils.color("This ability is on cooldown for ${(cooldownSeconds * 10.0).roundToInt() / 10.0} seconds.")) + } } } } @@ -35,7 +37,6 @@ class InteractListener : Listener { 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 diff --git a/src/main/java/me/fivevl/itemadder/Item.kt b/src/main/java/me/fivevl/itemadder/Item.kt index ce59d1c..d7559a3 100644 --- a/src/main/java/me/fivevl/itemadder/Item.kt +++ b/src/main/java/me/fivevl/itemadder/Item.kt @@ -3,16 +3,14 @@ package me.fivevl.itemadder import me.fivevl.itemadder.types.AbilityType import me.fivevl.itemadder.types.ItemType import me.fivevl.itemadder.types.Rarity -import net.kyori.adventure.text.Component import org.bukkit.Material -import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.inventory.ItemStack import java.util.* import kotlin.collections.HashMap -class Item(uniqueName: String, name: String, private val rarity: Rarity, material: Material, private val type: ItemType, private val lore: String) { +class Item(val uniqueName: String, name: String, private val rarity: Rarity, material: Material, private val type: ItemType, private var lore: String) { val finalItem: ItemStack - val abilities = HashMap<(PlayerInteractEvent) -> Unit, AbilityType>() + val abilities = HashMap() init { val item = ItemStack(material) @@ -24,18 +22,20 @@ class Item(uniqueName: String, name: String, private val rarity: Rarity, materia Items.items[uniqueName] = this } - fun addAbility(abilityType: AbilityType, name: String, addLore: String, runnable: (PlayerInteractEvent) -> Unit) { - abilities[runnable] = abilityType + fun addAbility(abilityType: AbilityType, addLore: String, ability: Ability) { + abilities[ability] = abilityType val clickType = if (abilityType == AbilityType.LEFT_CLICK) "Left click" else if (abilityType == AbilityType.RIGHT_CLICK) "Right click" else "Other type" val meta = finalItem.itemMeta - val newLore = meta.lore()!! - val lastLine = newLore.last()!! - newLore.remove(lastLine) val sj = StringJoiner("\n") - for (line in newLore) { - sj.add(line.toString()) - } - meta.lore(Utils.loreBuilder(sj.toString(), "$clickType: $name", addLore, " ", rarity.formatted + " " + type.type)) + sj.add(lore) + sj.add(" ") + sj.add("$clickType: ${ability.name}") + sj.add(addLore) + sj.add(" ") + sj.add(rarity.formatted + " " + type.type) + val newLore = Utils.loreBuilder(sj.toString()) + lore = sj.toString() + meta.lore(newLore) finalItem.itemMeta = meta } } \ 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 deleted file mode 100644 index 43d5c1e..0000000 --- a/src/main/java/me/fivevl/itemadder/ItemsCommand.kt +++ /dev/null @@ -1,23 +0,0 @@ -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()) - Utils.inItemsGui.add(p) - } 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/Main.kt b/src/main/java/me/fivevl/itemadder/Main.kt index bc9f079..99a6195 100644 --- a/src/main/java/me/fivevl/itemadder/Main.kt +++ b/src/main/java/me/fivevl/itemadder/Main.kt @@ -1,20 +1,23 @@ package me.fivevl.itemadder +import me.fivevl.itemadder.commands.GetCommand +import me.fivevl.itemadder.commands.ItemsCommand import me.fivevl.itemadder.types.AbilityType import me.fivevl.itemadder.types.ItemType import me.fivevl.itemadder.types.Rarity import org.bukkit.Bukkit import org.bukkit.Material -import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.plugin.java.JavaPlugin class Main : JavaPlugin() { override fun onEnable() { Bukkit.getPluginManager().registerEvents(InteractListener(), this) getCommand("items")?.setExecutor(ItemsCommand()) + getCommand("get")?.setExecutor(GetCommand()) Item("TEST_ITEM", "Test Item", Rarity.EPIC, Material.STONE_SWORD, ItemType.WEAPON, "The most basic testing\nsword, by 5vl") - Items.items["TEST_ITEM"]!!.addAbility(AbilityType.RIGHT_CLICK, "Send Message", "Sends you a nice message\nfor using this item!") { e: PlayerInteractEvent -> - e.player.sendMessage(Utils.color("You right clicked the TEST_ITEM!")) + val testAbility = Ability("Send Message", 3.0) { + it.player.sendMessage(Utils.color("You right clicked the TEST_ITEM!")) } + Items.items["TEST_ITEM"]!!.addAbility(AbilityType.RIGHT_CLICK, "Sends you a nice message\nfor using this item!", testAbility) } } \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/Utils.kt b/src/main/java/me/fivevl/itemadder/Utils.kt index 23f0ef3..7e19280 100644 --- a/src/main/java/me/fivevl/itemadder/Utils.kt +++ b/src/main/java/me/fivevl/itemadder/Utils.kt @@ -12,13 +12,9 @@ object Utils { fun loreBuilder(vararg arr: String): ArrayList { val lore = ArrayList() for (s in arr) { - if (s.contains("\n")) { - for (ss in s.split("\n")) { - lore.add(color("$ss")) - } - } else { - 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/java/me/fivevl/itemadder/commands/GetCommand.kt b/src/main/java/me/fivevl/itemadder/commands/GetCommand.kt new file mode 100644 index 0000000..3f09cf1 --- /dev/null +++ b/src/main/java/me/fivevl/itemadder/commands/GetCommand.kt @@ -0,0 +1,48 @@ +package me.fivevl.itemadder.commands + +import me.fivevl.itemadder.Items +import me.fivevl.itemadder.Utils +import org.bukkit.Bukkit +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player + +class GetCommand : CommandExecutor { + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (sender !is Player) { + sender.sendMessage(Utils.color("You must be a player to use this command!")) + return true + } + val p = sender.player!! + if (!p.hasPermission("itemadder.get")) { + p.sendMessage(Utils.color("You don't have permission to use this command!")) + return true + } + val item = Items.items[args[0]] + if (item == null) { + p.sendMessage(Utils.color("Item not found! Please use the unique name.")) + return true + } + when (args.size) { + 1 -> { + p.inventory.addItem(item.finalItem) + p.sendMessage(Utils.color("You have been given the item ${item.uniqueName}!")) + } + 2 -> { + val target = Bukkit.getPlayer(args[1]) + if (target == null) { + p.sendMessage(Utils.color("Player not found!")) + return true + } + target.inventory.addItem(item.finalItem) + p.sendMessage(Utils.color("You have given ${target.name} the item ${item.uniqueName}")) + target.sendMessage(Utils.color("You have been given the item ${item.uniqueName}")) + } + else -> { + p.sendMessage(Utils.color("Usage: /get [player]")) + } + } + return true + } +} \ No newline at end of file diff --git a/src/main/java/me/fivevl/itemadder/commands/ItemsCommand.kt b/src/main/java/me/fivevl/itemadder/commands/ItemsCommand.kt new file mode 100644 index 0000000..07a694c --- /dev/null +++ b/src/main/java/me/fivevl/itemadder/commands/ItemsCommand.kt @@ -0,0 +1,30 @@ +package me.fivevl.itemadder.commands + +import me.fivevl.itemadder.Items +import me.fivevl.itemadder.ItemsGui +import me.fivevl.itemadder.Utils +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("You must be a player to use this command!")) + return true + } + val p = sender.player!! + if (!p.hasPermission("itemadder.items")) { + p.sendMessage(Utils.color("You don't have permission to use this command!")) + return true + } + if (Items.items.isEmpty()) { + p.sendMessage(Utils.color("There are no items!")) + return true + } + p.openInventory(ItemsGui.getGui()) + Utils.inItemsGui.add(p) + return true + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 228abf9..a173bda 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,4 +6,6 @@ authors: [ 5vl ] description: Create custom items with custom abilities! commands: items: - description: Main item GUI. \ No newline at end of file + description: Main item GUI. + get: + description: Get item without GUI. \ No newline at end of file