put stuff in lambda function thing

This commit is contained in:
5vl 2022-06-21 16:09:14 +02:00
parent fb38607d8a
commit 59d7f1822e
No known key found for this signature in database
GPG Key ID: DA8938F22548E4D5
11 changed files with 34 additions and 25 deletions

View File

@ -1,8 +0,0 @@
package me.fivevl.itemadder
import org.bukkit.event.player.PlayerInteractEvent
enum class AbilityType(val event: PlayerInteractEvent) {
LEFT_CLICK(Utils.interactEvent),
RIGHT_CLICK(Utils.interactEvent)
}

View File

@ -1,5 +1,6 @@
package me.fivevl.itemadder package me.fivevl.itemadder
import me.fivevl.itemadder.types.AbilityType
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.event.EventHandler import org.bukkit.event.EventHandler
import org.bukkit.event.Listener import org.bukkit.event.Listener
@ -12,14 +13,14 @@ 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 Utils.items.values) { for (check in Items.items.values) {
if (check.finalItem == e.item) { if (check.finalItem == 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)) {
ability.run() ability.invoke(e)
} }
if (check.abilities[ability]!! == AbilityType.LEFT_CLICK && (e.action == Action.LEFT_CLICK_AIR || e.action == Action.LEFT_CLICK_BLOCK)) { if (check.abilities[ability]!! == AbilityType.LEFT_CLICK && (e.action == Action.LEFT_CLICK_AIR || e.action == Action.LEFT_CLICK_BLOCK)) {
ability.run() ability.invoke(e)
} }
} }
} }

View File

@ -1,12 +1,16 @@
package me.fivevl.itemadder 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 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
class Item(uniqueName: String, name: Component, rarity: Rarity, material: Material, type: Type, lore: String) { class Item(uniqueName: String, name: Component, rarity: Rarity, material: Material, type: ItemType, lore: String) {
val finalItem: ItemStack val finalItem: ItemStack
val abilities = HashMap<Runnable, AbilityType>() val abilities = HashMap<(PlayerInteractEvent) -> Unit, AbilityType>()
init { init {
val item = ItemStack(material) val item = ItemStack(material)
@ -15,10 +19,10 @@ class Item(uniqueName: String, name: Component, rarity: Rarity, material: Materi
meta.lore(Utils.loreBuilder(lore, "", rarity.formatted + " " + type.type)) meta.lore(Utils.loreBuilder(lore, "", rarity.formatted + " " + type.type))
item.itemMeta = meta item.itemMeta = meta
finalItem = item finalItem = item
Utils.items[uniqueName] = this Items.items[uniqueName] = this
} }
fun addAbility(type: AbilityType, runnable: Runnable) { fun addAbility(type: AbilityType, runnable: (PlayerInteractEvent) -> Unit) {
abilities[runnable] = type abilities[runnable] = type
} }
} }

View File

@ -0,0 +1,5 @@
package me.fivevl.itemadder
object Items {
val items = HashMap<String, Item>()
}

View File

@ -14,6 +14,7 @@ class ItemsCommand : CommandExecutor {
val p = sender.player!! val p = sender.player!!
if (p.hasPermission("itemadder.items")) { if (p.hasPermission("itemadder.items")) {
p.openInventory(ItemsGui.getGui()) p.openInventory(ItemsGui.getGui())
Utils.inItemsGui.add(p)
} else { } else {
p.sendMessage(Utils.color("<red>You don't have permission to use this command")) p.sendMessage(Utils.color("<red>You don't have permission to use this command"))
} }

View File

@ -6,7 +6,7 @@ import org.bukkit.inventory.Inventory
object ItemsGui { object ItemsGui {
fun getGui(): Inventory { fun getGui(): Inventory {
val gui = Bukkit.createInventory(null, 54, Utils.color("Items")) val gui = Bukkit.createInventory(null, 54, Utils.color("Items"))
for (item in Utils.items.values) { for (item in Items.items.values) {
gui.addItem(item.finalItem) gui.addItem(item.finalItem)
} }
return gui return gui

View File

@ -1,16 +1,19 @@
package me.fivevl.itemadder package me.fivevl.itemadder
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.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())
Item("TEST_ITEM", Utils.color("<blue>Test Item"), Rarity.EPIC, Material.STONE_SWORD, Type.WEAPON, "<red>The most basic testing</red>\n<blue>sword, by 5vl</blue>") Item("TEST_ITEM", Utils.color("<blue>Test Item"), Rarity.EPIC, Material.STONE_SWORD, ItemType.WEAPON, "<red>The most basic testing</red>\n<blue>sword, by 5vl</blue>")
Utils.items["TEST_ITEM"]!!.addAbility(AbilityType.RIGHT_CLICK) { Items.items["TEST_ITEM"]!!.addAbility(AbilityType.RIGHT_CLICK) { e: PlayerInteractEvent ->
val e = AbilityType.RIGHT_CLICK.event
e.player.sendMessage(Utils.color("<red>You right clicked the TEST_ITEM!")) e.player.sendMessage(Utils.color("<red>You right clicked the TEST_ITEM!"))
} }
} }

View File

@ -3,11 +3,8 @@ package me.fivevl.itemadder
import net.kyori.adventure.text.Component import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerInteractEvent
object Utils { object Utils {
lateinit var interactEvent: PlayerInteractEvent
val items = HashMap<String, Item>()
val inItemsGui = ArrayList<Player>() val inItemsGui = ArrayList<Player>()
fun color(s: String): Component { fun color(s: String): Component {
return MiniMessage.miniMessage().deserialize(s) return MiniMessage.miniMessage().deserialize(s)

View File

@ -0,0 +1,6 @@
package me.fivevl.itemadder.types
enum class AbilityType {
LEFT_CLICK,
RIGHT_CLICK
}

View File

@ -1,6 +1,6 @@
package me.fivevl.itemadder package me.fivevl.itemadder.types
enum class Type(val type: String) { enum class ItemType(val type: String) {
WEAPON("WEAPON"), WEAPON("WEAPON"),
PICKAXE("PICKAXE"), PICKAXE("PICKAXE"),
ITEM("ITEM"), ITEM("ITEM"),

View File

@ -1,4 +1,4 @@
package me.fivevl.itemadder package me.fivevl.itemadder.types
enum class Rarity(val formatted: String) { enum class Rarity(val formatted: String) {
COMMON("<white><bold>COMMON"), COMMON("<white><bold>COMMON"),