mirror of
https://github.com/5vl/ItemAdder.git
synced 2025-05-23 22:06:59 +00:00
did something again i dont remember
This commit is contained in:
parent
527f88befd
commit
4b8be7489e
20
src/main/java/me/fivevl/itemadder/Ability.kt
Normal file
20
src/main/java/me/fivevl/itemadder/Ability.kt
Normal 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
|
||||
}
|
||||
}
|
@ -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("<red>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("<green>Added <blue>${e.currentItem!!.itemMeta.displayName} <green>to your inventory"))
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
@ -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<Ability, AbilityType>()
|
||||
|
||||
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(), "<yellow><bold>$clickType: <gold>$name", addLore, " ", rarity.formatted + " " + type.type))
|
||||
sj.add(lore)
|
||||
sj.add(" ")
|
||||
sj.add("<yellow><bold>$clickType: <gold>${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
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
@ -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, "<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 ->
|
||||
e.player.sendMessage(Utils.color("<red>You right clicked the TEST_ITEM!"))
|
||||
val testAbility = Ability("Send Message", 3.0) {
|
||||
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)
|
||||
}
|
||||
}
|
@ -12,13 +12,9 @@ object Utils {
|
||||
fun loreBuilder(vararg arr: String): ArrayList<Component> {
|
||||
val lore = ArrayList<Component>()
|
||||
for (s in arr) {
|
||||
if (s.contains("\n")) {
|
||||
for (ss in s.split("\n")) {
|
||||
lore.add(color("<!i>$ss"))
|
||||
}
|
||||
} else {
|
||||
lore.add(color("<!i>$s"))
|
||||
}
|
||||
if (s.contains("\n"))
|
||||
for (ss in s.split("\n")) lore.add(color("<!i>$ss"))
|
||||
else lore.add(color("<!i>$s"))
|
||||
}
|
||||
return lore
|
||||
}
|
||||
|
48
src/main/java/me/fivevl/itemadder/commands/GetCommand.kt
Normal file
48
src/main/java/me/fivevl/itemadder/commands/GetCommand.kt
Normal 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
|
||||
}
|
||||
}
|
30
src/main/java/me/fivevl/itemadder/commands/ItemsCommand.kt
Normal file
30
src/main/java/me/fivevl/itemadder/commands/ItemsCommand.kt
Normal 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
|
||||
}
|
||||
}
|
@ -6,4 +6,6 @@ authors: [ 5vl ]
|
||||
description: Create custom items with custom abilities!
|
||||
commands:
|
||||
items:
|
||||
description: Main item GUI.
|
||||
description: Main item GUI.
|
||||
get:
|
||||
description: Get item without GUI.
|
Loading…
x
Reference in New Issue
Block a user