did something again i dont remember

This commit is contained in:
5vl 2022-06-27 14:03:10 +02:00
parent 527f88befd
commit 4b8be7489e
No known key found for this signature in database
GPG Key ID: DA8938F22548E4D5
9 changed files with 131 additions and 54 deletions

View File

@ -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<Player, Long>()
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
}
}

View File

@ -8,19 +8,21 @@ import org.bukkit.event.block.Action
import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.InventoryCloseEvent import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.event.player.PlayerInteractEvent
import kotlin.math.roundToInt
class InteractListener : Listener { class InteractListener : Listener {
@EventHandler @EventHandler
fun onInteract(e: PlayerInteractEvent) { fun onInteract(e: PlayerInteractEvent) {
if (e.item == null) return if (e.item == null) return
for (check in Items.items.values) { for (check in Items.items.values) {
if (check.finalItem == e.item) { if (check.finalItem.isSimilar(e.item)) {
for (ability in check.abilities.keys) { 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)) { 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))) {
ability.invoke(e) val cooldownSeconds = ability.run(e)
} if (cooldownSeconds != null) {
if (check.abilities[ability]!! == AbilityType.LEFT_CLICK && (e.action == Action.LEFT_CLICK_AIR || e.action == Action.LEFT_CLICK_BLOCK)) { // This counts up i cba to change rn
ability.invoke(e) e.player.sendMessage(Utils.color("<red>This ability is on cooldown for ${(cooldownSeconds * 10.0).roundToInt() / 10.0} seconds."))
}
} }
} }
} }
@ -35,7 +37,6 @@ class InteractListener : Listener {
e.isCancelled = true e.isCancelled = true
if (e.currentItem == null) return if (e.currentItem == null) return
e.whoClicked.inventory.addItem(e.currentItem!!) e.whoClicked.inventory.addItem(e.currentItem!!)
e.whoClicked.sendMessage(Utils.color("<green>Added <blue>${e.currentItem!!.itemMeta.displayName} <green>to your inventory"))
} }
@EventHandler @EventHandler

View File

@ -3,16 +3,14 @@ package me.fivevl.itemadder
import me.fivevl.itemadder.types.AbilityType import me.fivevl.itemadder.types.AbilityType
import me.fivevl.itemadder.types.ItemType import me.fivevl.itemadder.types.ItemType
import me.fivevl.itemadder.types.Rarity import me.fivevl.itemadder.types.Rarity
import net.kyori.adventure.text.Component
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import java.util.* import java.util.*
import kotlin.collections.HashMap 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 finalItem: ItemStack
val abilities = HashMap<(PlayerInteractEvent) -> Unit, AbilityType>() val abilities = HashMap<Ability, AbilityType>()
init { init {
val item = ItemStack(material) val item = ItemStack(material)
@ -24,18 +22,20 @@ class Item(uniqueName: String, name: String, private val rarity: Rarity, materia
Items.items[uniqueName] = this Items.items[uniqueName] = this
} }
fun addAbility(abilityType: AbilityType, name: String, addLore: String, runnable: (PlayerInteractEvent) -> Unit) { fun addAbility(abilityType: AbilityType, addLore: String, ability: Ability) {
abilities[runnable] = abilityType abilities[ability] = abilityType
val clickType = if (abilityType == AbilityType.LEFT_CLICK) "Left click" else if (abilityType == AbilityType.RIGHT_CLICK) "Right click" else "Other type" 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 meta = finalItem.itemMeta
val newLore = meta.lore()!!
val lastLine = newLore.last()!!
newLore.remove(lastLine)
val sj = StringJoiner("\n") val sj = StringJoiner("\n")
for (line in newLore) { sj.add(lore)
sj.add(line.toString()) sj.add(" ")
} sj.add("<yellow><bold>$clickType: <gold>${ability.name}")
meta.lore(Utils.loreBuilder(sj.toString(), "<yellow><bold>$clickType: <gold>$name", addLore, " ", rarity.formatted + " " + type.type)) 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 finalItem.itemMeta = meta
} }
} }

View File

@ -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<out String>?): Boolean {
if (sender !is Player) {
sender.sendMessage(Utils.color("<red>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("<red>You don't have permission to use this command"))
}
return true
}
}

View File

@ -1,20 +1,23 @@
package me.fivevl.itemadder 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.AbilityType
import me.fivevl.itemadder.types.ItemType import me.fivevl.itemadder.types.ItemType
import me.fivevl.itemadder.types.Rarity import me.fivevl.itemadder.types.Rarity
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.plugin.java.JavaPlugin import org.bukkit.plugin.java.JavaPlugin
class Main : JavaPlugin() { class Main : JavaPlugin() {
override fun onEnable() { override fun onEnable() {
Bukkit.getPluginManager().registerEvents(InteractListener(), this) Bukkit.getPluginManager().registerEvents(InteractListener(), this)
getCommand("items")?.setExecutor(ItemsCommand()) getCommand("items")?.setExecutor(ItemsCommand())
getCommand("get")?.setExecutor(GetCommand())
Item("TEST_ITEM", "Test Item", Rarity.EPIC, Material.STONE_SWORD, ItemType.WEAPON, "<red>The most basic testing</red>\n<blue>sword, by 5vl</blue>") Item("TEST_ITEM", "Test Item", Rarity.EPIC, Material.STONE_SWORD, ItemType.WEAPON, "<red>The most basic testing</red>\n<blue>sword, by 5vl</blue>")
Items.items["TEST_ITEM"]!!.addAbility(AbilityType.RIGHT_CLICK, "Send Message", "<light_purple>Sends you a nice message\n<light_purple>for using this item!") { e: PlayerInteractEvent -> val testAbility = Ability("Send Message", 3.0) {
e.player.sendMessage(Utils.color("<red>You right clicked the TEST_ITEM!")) it.player.sendMessage(Utils.color("<red>You right clicked the TEST_ITEM!"))
} }
Items.items["TEST_ITEM"]!!.addAbility(AbilityType.RIGHT_CLICK, "<light_purple>Sends you a nice message\n<light_purple>for using this item!", testAbility)
} }
} }

View File

@ -12,13 +12,9 @@ object Utils {
fun loreBuilder(vararg arr: String): ArrayList<Component> { fun loreBuilder(vararg arr: String): ArrayList<Component> {
val lore = ArrayList<Component>() val lore = ArrayList<Component>()
for (s in arr) { for (s in arr) {
if (s.contains("\n")) { if (s.contains("\n"))
for (ss in s.split("\n")) { for (ss in s.split("\n")) lore.add(color("<!i>$ss"))
lore.add(color("<!i>$ss")) else lore.add(color("<!i>$s"))
}
} else {
lore.add(color("<!i>$s"))
}
} }
return lore return lore
} }

View File

@ -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<out String>): Boolean {
if (sender !is Player) {
sender.sendMessage(Utils.color("<red>You must be a player to use this command!"))
return true
}
val p = sender.player!!
if (!p.hasPermission("itemadder.get")) {
p.sendMessage(Utils.color("<red>You don't have permission to use this command!"))
return true
}
val item = Items.items[args[0]]
if (item == null) {
p.sendMessage(Utils.color("<red>Item not found! Please use the unique name."))
return true
}
when (args.size) {
1 -> {
p.inventory.addItem(item.finalItem)
p.sendMessage(Utils.color("<green>You have been given the item ${item.uniqueName}!"))
}
2 -> {
val target = Bukkit.getPlayer(args[1])
if (target == null) {
p.sendMessage(Utils.color("<red>Player not found!"))
return true
}
target.inventory.addItem(item.finalItem)
p.sendMessage(Utils.color("<green>You have given ${target.name} the item ${item.uniqueName}"))
target.sendMessage(Utils.color("<green>You have been given the item ${item.uniqueName}"))
}
else -> {
p.sendMessage(Utils.color("<red>Usage: /get <item> [player]"))
}
}
return true
}
}

View File

@ -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<out String>): Boolean {
if (sender !is Player) {
sender.sendMessage(Utils.color("<red>You must be a player to use this command!"))
return true
}
val p = sender.player!!
if (!p.hasPermission("itemadder.items")) {
p.sendMessage(Utils.color("<red>You don't have permission to use this command!"))
return true
}
if (Items.items.isEmpty()) {
p.sendMessage(Utils.color("<red>There are no items!"))
return true
}
p.openInventory(ItemsGui.getGui())
Utils.inItemsGui.add(p)
return true
}
}

View File

@ -7,3 +7,5 @@ description: Create custom items with custom abilities!
commands: commands:
items: items:
description: Main item GUI. description: Main item GUI.
get:
description: Get item without GUI.