Skip to content

Commit eb10abc

Browse files
author
Francisco Solis
committed
Fixes, Features and Improvements
* Fixed some issues when you opened the UIs * Added extension values to the ItemStack class * Removed Test Data * Added taskmodule depend * Added new onBuild function * Fixes to the UI Click system
1 parent 5bf1b77 commit eb10abc

File tree

5 files changed

+73
-87
lines changed

5 files changed

+73
-87
lines changed
Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,5 @@
11
package xyz.theprogramsrc.uismodule
22

3-
import com.cryptomorin.xseries.XMaterial
4-
import org.bukkit.Bukkit
5-
import org.bukkit.event.EventHandler
6-
import org.bukkit.event.Listener
7-
import org.bukkit.event.player.AsyncPlayerChatEvent
83
import xyz.theprogramsrc.simplecoreapi.global.module.Module
9-
import xyz.theprogramsrc.simplecoreapi.spigot.SpigotLoader
10-
import xyz.theprogramsrc.tasksmodule.spigot.SpigotTasks
11-
import xyz.theprogramsrc.uismodule.objects.*
12-
import xyz.theprogramsrc.uismodule.simple.SimpleUi
13-
import xyz.theprogramsrc.uismodule.ui.Ui
144

15-
class Main: Module(), Listener {
16-
17-
// TODO: Test this :p
18-
19-
override fun onEnable() {
20-
Bukkit.getPluginManager().registerEvents(this, SpigotLoader.instance)
21-
}
22-
23-
private val item = XMaterial.DIAMOND_SWORD.itemStack()
24-
.setName("&bDiamond Sword")
25-
.lore(
26-
"&7",
27-
"&7&l&m-------------------",
28-
"&7&l&m&oDescription",
29-
"&7&l&m-------------------",
30-
"&7&l&m&oThis is a test",
31-
"&7&l&m&oof the Ui",
32-
"&7"
33-
)
34-
.setGlowing()
35-
36-
@EventHandler fun onCmd(event: AsyncPlayerChatEvent){
37-
SpigotTasks.instance.run {
38-
if(event.message.lowercase().contains("simpleui")){
39-
SimpleUi.Builder()
40-
.title("&a&lWelcome to the server! (MC)")
41-
.slots(9)
42-
.entries(mapOf(
43-
4 to UiEntry(item = item){
44-
it.player.inventory.addItem(item)
45-
it.closeUi()
46-
}
47-
))
48-
.closeAfterClick(false)
49-
.build(event.player)
50-
} else if (event.message.lowercase().contains("ui")){
51-
Ui("&a&lWelcome to the server! (MC)", 9, event.player).apply {
52-
onBuild = {
53-
set(4, UiEntry(item = item){
54-
it.player.inventory.addItem(item)
55-
it.closeUi()
56-
})
57-
}
58-
}
59-
} else {
60-
61-
}
62-
}
63-
}
64-
}
5+
class Main: Module()

src/main/kotlin/xyz/theprogramsrc/uismodule/objects/SimpleItem.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ fun String.bukkitColor(vararg args: Any): String = ChatColor.translateAlternateC
2929
*/
3030
fun String.bukkitStripColor(): String = ChatColor.stripColor(this) ?: this
3131

32+
/**
33+
* Gets the name of this [ItemStack]
34+
* @return the name of the item
35+
*/
36+
val ItemStack.name: String
37+
get() = this.itemMeta?.displayName ?: this.type.name
38+
3239
/**
3340
* Sets the name of this [ItemStack]
3441
* @param name the name of the item
@@ -40,6 +47,13 @@ fun ItemStack.setName(name: String): ItemStack = this.apply {
4047
}
4148
}
4249

50+
/**
51+
* Gets the lore of this [ItemStack]
52+
* @return the lore of the item
53+
*/
54+
val ItemStack.lore: List<String>
55+
get() = this.itemMeta?.lore ?: emptyList()
56+
4357
/**
4458
* Sets the lore of this [ItemStack]
4559
* @param lore the lore of the item
@@ -93,6 +107,13 @@ fun ItemStack.amount(amount: Int): ItemStack = this.apply {
93107
this.amount = amount
94108
}
95109

110+
/**
111+
* Gets the flags of this [ItemStack]
112+
* @return the flags of the item
113+
*/
114+
val ItemStack.flags: List<ItemFlag>
115+
get() = this.itemMeta?.itemFlags?.toList() ?: emptyList()
116+
96117
/**
97118
* Toggles the given flags on this [ItemStack]. If the flag is already set, it will be removed.
98119
* @param flags the flags to toggle
@@ -132,6 +153,13 @@ fun ItemStack.removeFlags(vararg flags: ItemFlag): ItemStack = this.apply {
132153
}
133154
}
134155

156+
/**
157+
* Gets the enchantments of this [ItemStack]
158+
* @return the enchantments of the item
159+
*/
160+
val ItemStack.enchantments: Map<Enchantment, Int>
161+
get() = this.itemMeta?.enchants ?: mapOf()
162+
135163
/**
136164
* Toggles the given enchantments on this [ItemStack]. If the enchantment is already set, it will be removed.
137165
* @param enchantments the enchantments to toggle

src/main/kotlin/xyz/theprogramsrc/uismodule/simple/SimpleUi.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.bukkit.inventory.Inventory
1515
import org.bukkit.inventory.ItemStack
1616
import org.bukkit.inventory.PlayerInventory
1717
import xyz.theprogramsrc.simplecoreapi.spigot.SpigotLoader
18+
import xyz.theprogramsrc.tasksmodule.spigot.SpigotTasks
1819
import xyz.theprogramsrc.uismodule.objects.ClickType
1920
import xyz.theprogramsrc.uismodule.objects.UiAction
2021
import xyz.theprogramsrc.uismodule.objects.UiEntry
@@ -42,24 +43,23 @@ open class SimpleUi(private val title: String, private val slots: Int, private v
4243
*/
4344
var closeAfterClick = true
4445

45-
private val actions = mutableMapOf<Int, (UiAction) -> Unit>()
46-
4746
/**
4847
* Opens the UI for the player
4948
* @return This [SimpleUi]
5049
*/
5150
open fun open(): SimpleUi = this.apply {
52-
if(player.openInventory == this.inventory) return@apply
5351
if(inventory == null){
5452
HandlerList.unregisterAll(this)
5553
Bukkit.getPluginManager().registerEvents(this, SpigotLoader.instance)
5654
this.inventory = Bukkit.createInventory(null, slots, title.bukkitColor())
5755
entries.forEach {
58-
inventory?.setItem(it.key, it.value.item)
56+
this.inventory?.setItem(it.key, it.value.item)
5957
}
6058
}
6159

62-
player.openInventory(inventory ?: throw IllegalStateException("An unknown error occurred while creating the inventory"))
60+
SpigotTasks.instance.runTask {
61+
player.openInventory(this.inventory ?: throw IllegalStateException("An unknown error occurred while creating the inventory"))
62+
}
6363
}
6464

6565
/**
@@ -68,16 +68,16 @@ open class SimpleUi(private val title: String, private val slots: Int, private v
6868
*/
6969
open fun close(): SimpleUi = this.apply {
7070
HandlerList.unregisterAll(this)
71-
this.player.closeInventory()
72-
inventory = null
71+
SpigotTasks.instance.runTask(this.player::closeInventory)
72+
this.inventory = null
7373
}
7474

7575
@EventHandler
7676
fun onClick(event: InventoryClickEvent){
7777
if(this.inventory == null || event.whoClicked != this.player) return
7878
event.isCancelled = true
7979
if(event.slotType == InventoryType.SlotType.OUTSIDE) return
80-
if(event.clickedInventory !is PlayerInventory && this.actions.containsKey(event.slot)){
80+
if(event.clickedInventory !is PlayerInventory && this.entries.containsKey(event.slot)){
8181
val action = object:UiAction(this.player, ClickType.fromEvent(event), event) {
8282

8383
override fun openUi() {
@@ -88,7 +88,9 @@ open class SimpleUi(private val title: String, private val slots: Int, private v
8888
this@SimpleUi.close()
8989
}
9090
}
91-
this.actions[event.slot]?.invoke(action)
91+
SpigotTasks.instance.runTask {
92+
this.entries[event.slot]?.action?.invoke(action)
93+
}
9294
if(this.closeAfterClick) this.close()
9395
}
9496
}

src/main/kotlin/xyz/theprogramsrc/uismodule/ui/Ui.kt

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ open class Ui(private val title: String, private val slots: Int, private val pla
6161

6262
init {
6363
task = SpigotTasks.instance.runTaskTimer(0L, 1L){
64-
if(!(this.inventory == null || this.player.openInventory.title.bukkitStripColor() != this.title.bukkitStripColor() || this.inventory?.size != this.slots)) {
64+
if(!(this.inventory == null || this.player.openInventory.title.bukkitColor().bukkitStripColor() != this.title.bukkitColor().bukkitStripColor() || this.inventory?.size != this.slots)) {
6565
if(preventOpening) {
6666
task.stop()
6767
HandlerList.unregisterAll(this)
@@ -74,14 +74,15 @@ open class Ui(private val title: String, private val slots: Int, private val pla
7474
}
7575
this.onBuild(model)
7676
val entries = model.items
77-
IntRange(0, this.slots).forEach { slot ->
77+
IntRange(0, this.slots.dec()).forEach { slot ->
7878
val entry = entries[slot]
79-
if(entry == null) {
80-
this.inventory?.setItem(slot, null)
81-
this.entries.remove(slot)
82-
} else {
83-
this.inventory?.setItem(slot, entry.item)
84-
this.entries[slot] = entry
79+
this.inventory?.apply {
80+
setItem(slot, entry?.item)
81+
if(entry == null){
82+
this@Ui.entries.remove(slot)
83+
} else {
84+
this@Ui.entries[slot] = entry
85+
}
8586
}
8687
}
8788
this.player.updateInventory()
@@ -98,15 +99,15 @@ open class Ui(private val title: String, private val slots: Int, private val pla
9899
* @return This [Ui]
99100
*/
100101
open fun open(): Ui = this.apply {
101-
if(player.openInventory == this.inventory) return@apply
102102
this.preventOpening = false
103+
this.task.stop()
103104
if(inventory == null){
104105
HandlerList.unregisterAll(this)
105106
Bukkit.getPluginManager().registerEvents(this, SpigotLoader.instance)
106-
this.task.start()
107107
}
108108

109109
this.inventory = Bukkit.createInventory(null, slots, title.bukkitColor())
110+
this.task.start()
110111
SpigotTasks.instance.runTask {
111112
Bukkit.getPluginManager().callEvent(UiOpenEvent(this, this.player))
112113
player.openInventory(inventory ?: throw IllegalStateException("An unknown error occurred while creating the inventory"))
@@ -125,21 +126,34 @@ open class Ui(private val title: String, private val slots: Int, private val pla
125126
}
126127
}
127128

129+
/**
130+
* Sets the onBuild variable
131+
* @param onBuild executed to build the model
132+
* @return This [Ui]
133+
*/
134+
fun onBuild(onBuild: UiModel.() -> Unit): Ui {
135+
this.onBuild = onBuild
136+
return this
137+
}
138+
128139
@EventHandler
129140
fun onClick(event: InventoryClickEvent){
130141
if(this.inventory == null || event.whoClicked != this.player) return
131142
event.isCancelled = true
132143
if(event.slotType == InventoryType.SlotType.OUTSIDE || event.clickedInventory is PlayerInventory) return
144+
println("${event.slot}: ${this.entries[event.slot]?.item?.name}")
133145
val entry = this.entries[event.slot] ?: return
134-
entry.action.invoke(object:UiAction(this.player, ClickType.fromEvent(event), event) {
135-
override fun openUi() {
136-
this@Ui.open()
137-
}
146+
SpigotTasks.instance.runTask {
147+
entry.action.invoke(object:UiAction(this.player, ClickType.fromEvent(event), event) {
148+
override fun openUi() {
149+
this@Ui.open()
150+
}
138151

139-
override fun closeUi() {
140-
this@Ui.close()
141-
}
142-
})
152+
override fun closeUi() {
153+
this@Ui.close()
154+
}
155+
})
156+
}
143157
if(this.closeAfterClick) this.close()
144158
Bukkit.getPluginManager().callEvent(UiClickEvent(this, this.player, entry))
145159
}

src/main/resources/module.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ description=@description@
44
version=@version@
55
author=TheProgramSrc
66
repository-id=uismodule
7+
dependencies=tasksmodule

0 commit comments

Comments
 (0)