Skip to content

Commit 1e12456

Browse files
committed
# v1.0.9.7
Update SMenu.kt
1 parent aa1b7c8 commit 1e12456

File tree

9 files changed

+208
-42
lines changed

9 files changed

+208
-42
lines changed

src/main/kotlin/io/github/sunshinewzy/sunstcore/SunSTCore.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.github.sunshinewzy.sunstcore.modules.machine.*
99
import io.github.sunshinewzy.sunstcore.modules.machine.custom.SMachineRecipe
1010
import io.github.sunshinewzy.sunstcore.modules.machine.custom.SMachineRecipes
1111
import io.github.sunshinewzy.sunstcore.modules.task.TaskProgress
12+
import io.github.sunshinewzy.sunstcore.objects.SBlock
1213
import io.github.sunshinewzy.sunstcore.objects.SItem
1314
import io.github.sunshinewzy.sunstcore.objects.item.SunSTItem
1415
import io.github.sunshinewzy.sunstcore.objects.item.constructionstick.LineStick
@@ -86,6 +87,8 @@ object SunSTCore : Plugin() {
8687
}
8788

8889
private fun registerSerialization() {
90+
ConfigurationSerialization.registerClass(SBlock::class.java)
91+
8992
ConfigurationSerialization.registerClass(TaskProgress::class.java)
9093

9194
ConfigurationSerialization.registerClass(LineStick::class.java)
@@ -97,7 +100,6 @@ object SunSTCore : Plugin() {
97100

98101
ConfigurationSerialization.registerClass(SMachineRecipe::class.java)
99102
ConfigurationSerialization.registerClass(SMachineRecipes::class.java)
100-
101103
}
102104

103105

src/main/kotlin/io/github/sunshinewzy/sunstcore/modules/data/sunst/SMachineData.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.github.sunshinewzy.sunstcore.modules.data.SAutoCoverSaveData
44
import io.github.sunshinewzy.sunstcore.modules.machine.SMachine
55
import io.github.sunshinewzy.sunstcore.modules.machine.SMachineInformation
66
import io.github.sunshinewzy.sunstcore.objects.SLocation
7+
import io.github.sunshinewzy.sunstcore.utils.castMap
78
import org.bukkit.configuration.file.YamlConfiguration
89

910
class SMachineData(val sMachine: SMachine) : SAutoCoverSaveData(sMachine.wrench.plugin, sMachine.id, "SMachine") {
@@ -12,14 +13,20 @@ class SMachineData(val sMachine: SMachine) : SAutoCoverSaveData(sMachine.wrench.
1213
sMachine.sMachines.forEach { (sLoc, information) ->
1314
set(sLoc.toString(), information)
1415
}
16+
17+
set("Config.Recipes", sMachine.recipes)
1518
}
1619

1720
override fun YamlConfiguration.loadConfig() {
1821
val roots = getKeys(false)
1922
roots.forEach { sLoc ->
23+
if(sLoc == "Config") return@forEach
24+
2025
val information = get(sLoc) as? SMachineInformation ?: SMachineInformation()
2126
sMachine.addMachine(SLocation(sLoc), information)
2227
}
28+
29+
get("Config.Recipes")?.castMap(sMachine.recipes)
2330
}
2431

2532
}

src/main/kotlin/io/github/sunshinewzy/sunstcore/modules/machine/SMachine.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import io.github.sunshinewzy.sunstcore.events.smachine.SMachineRemoveEvent
66
import io.github.sunshinewzy.sunstcore.events.smachine.SMachineUpgradeEvent
77
import io.github.sunshinewzy.sunstcore.interfaces.Initable
88
import io.github.sunshinewzy.sunstcore.modules.data.sunst.SMachineData
9+
import io.github.sunshinewzy.sunstcore.modules.machine.custom.SMachineRecipes
10+
import io.github.sunshinewzy.sunstcore.objects.SItem
911
import io.github.sunshinewzy.sunstcore.objects.SItem.Companion.setNameAndLore
1012
import io.github.sunshinewzy.sunstcore.objects.SLocation
1113
import io.github.sunshinewzy.sunstcore.objects.SLocation.Companion.toSLocation
14+
import io.github.sunshinewzy.sunstcore.objects.SMenu
1215
import io.github.sunshinewzy.sunstcore.utils.SunSTTestApi
1316
import io.github.sunshinewzy.sunstcore.utils.getSMetadata
1417
import io.github.sunshinewzy.sunstcore.utils.sendMsg
1518
import io.github.sunshinewzy.sunstcore.utils.subtractClone
1619
import org.bukkit.Location
20+
import org.bukkit.Material
1721
import org.bukkit.Sound
1822
import org.bukkit.block.Block
1923
import org.bukkit.entity.Player
@@ -31,14 +35,33 @@ abstract class SMachine(
3135
val structure: SMachineStructure
3236
) : Initable {
3337
val sMachines = HashMap<SLocation, SMachineInformation>()
38+
val recipes = HashMap<String, SMachineRecipes>()
3439
val displayItem = structure.centerBlock.toItem().setNameAndLore("§e$name", "§7----------", "§fID: §a$id", "§7----------")
3540
var isCancelInteract = true
3641

42+
private val editMenu = SMenu("SMachine Edit - $id", "§r[$name§r] 机器编辑", 5)
43+
val editRecipeMenu = SMenu("SMachine Edit Recipe - $id", "§r[$name§r] 机器配方编辑", 6)
44+
3745

3846
init {
3947
wrench.addMachine(this)
4048

4149
SMachineData(this)
50+
51+
editMenu.apply {
52+
createEdge(SItem(Material.WHITE_STAINED_GLASS_PANE))
53+
setItem(3, 3, displayItem)
54+
}
55+
56+
editRecipeMenu.apply {
57+
createEdge(SItem(Material.WHITE_STAINED_GLASS_PANE))
58+
setDefaultTurnPageButton()
59+
60+
setItem(1, 2, SItem(Material.BROWN_STAINED_GLASS_PANE, "§a配方ID: "))
61+
setItem(1, 3, SItem(Material.RED_STAINED_GLASS_PANE, "§a输入: "))
62+
setItem(1, 4, SItem(Material.GREEN_STAINED_GLASS_PANE, "§a输出: "))
63+
setItem(1, 5, SItem(Material.YELLOW_STAINED_GLASS_PANE, "§a概率: "))
64+
}
4265
}
4366

4467

@@ -53,6 +76,17 @@ abstract class SMachine(
5376
*/
5477
abstract fun runMachine(event: SMachineRunEvent)
5578

79+
/**
80+
* 机器编辑界面
81+
*/
82+
fun edit(player: Player) {
83+
editMenu.setButton(5, 3, SItem(Material.CRAFTING_TABLE, "§a修改机器配方"), "EDIT_RECIPE") {
84+
editRecipe(player)
85+
}
86+
87+
editMenu.openInventory(player)
88+
}
89+
5690
/**
5791
* 当编辑机器配方时触发
5892
*/

src/main/kotlin/io/github/sunshinewzy/sunstcore/modules/machine/SMachineInformation.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package io.github.sunshinewzy.sunstcore.modules.machine
22

3-
import io.github.sunshinewzy.sunstcore.modules.machine.custom.SMachineRecipes
43
import io.github.sunshinewzy.sunstcore.utils.castMap
54
import org.bukkit.configuration.serialization.ConfigurationSerializable
65

76
data class SMachineInformation(
87
var owner: String = "",
98
var level: Short = 0,
10-
var recipes: HashMap<String, SMachineRecipes> = hashMapOf(),
119
val data: HashMap<String, Any> = HashMap()
1210
) : ConfigurationSerializable {
1311

1412
override fun serialize(): HashMap<String, Any> {
1513
val map = HashMap<String, Any>()
1614
map["owner"] = owner
1715
map["level"] = level
18-
map["recipes"] = recipes
1916
map["data"] = data
2017
return map
2118
}
@@ -36,8 +33,6 @@ data class SMachineInformation(
3633
information.level = it
3734
}
3835

39-
map["recipes"]?.castMap(information.recipes)
40-
4136
map["data"]?.castMap(information.data)
4237

4338
return information

src/main/kotlin/io/github/sunshinewzy/sunstcore/modules/machine/SMachineWrench.kt

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import io.github.sunshinewzy.sunstcore.modules.machine.SMachine.Companion.judgeS
1010
import io.github.sunshinewzy.sunstcore.objects.*
1111
import io.github.sunshinewzy.sunstcore.objects.inventoryholder.SPartProtectInventoryHolder
1212
import io.github.sunshinewzy.sunstcore.objects.item.TaskGuideItem
13-
import io.github.sunshinewzy.sunstcore.utils.actionList
1413
import io.github.sunshinewzy.sunstcore.utils.getPlayer
1514
import io.github.sunshinewzy.sunstcore.utils.sendMsg
1615
import io.github.sunshinewzy.sunstcore.utils.subscribeEvent
@@ -111,22 +110,18 @@ class SMachineWrench(
111110
}
112111
}
113112

114-
var page = 0
115-
var itemList = ArrayList<SMachine>()
116-
do {
117-
page++
118-
menu.setPageAction(page) {
119-
itemList = actionList(2, 2, 2, 5, 7, sMachines) { order ->
120-
menu.setPageButton(page, order, displayItem, id) {
121-
editRecipe(getPlayer())
122-
}
113+
114+
menu.setMultiPageAction(1, 2, 2, 2, 5, 7, sMachines) { page, order ->
115+
menu.setPageButton(page, order, displayItem, id) {
116+
val player = getPlayer()
117+
if(player.isOp) {
118+
edit(player)
123119
}
124120
}
125-
} while(itemList.isNotEmpty())
126-
menu.maxPage = page
121+
}
127122

128-
menu.setAllPageButton(9, 6, STurnPageType.NEXT_PAGE, TaskGuideItem.PAGE_NEXT.item)
129-
menu.setAllPageButton(1, 6, STurnPageType.PRE_PAGE, TaskGuideItem.PAGE_PRE.item)
123+
menu.setAllTurnPageButton(9, 6, STurnPageType.NEXT_PAGE, TaskGuideItem.PAGE_NEXT.item)
124+
menu.setAllTurnPageButton(1, 6, STurnPageType.PRE_PAGE, TaskGuideItem.PAGE_PRE.item)
130125
}
131126

132127
fun addMachine(machine: SMachine) {

src/main/kotlin/io/github/sunshinewzy/sunstcore/modules/machine/custom/SMachineRecipe.kt

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ sealed class SMachineRecipe(val coord: SCoordinate) : ConfigurationSerializable
2424
fun consume(loc: Location) {
2525
execute(loc.addClone(coord))
2626
}
27+
28+
fun consume(loc: Location, player: Player) {
29+
playerExecute(loc, player)
30+
}
2731

2832
override fun serialize(): MutableMap<String, Any> {
2933
val map = HashMap<String, Any>()
@@ -38,60 +42,101 @@ sealed class SMachineRecipe(val coord: SCoordinate) : ConfigurationSerializable
3842

3943
}
4044

45+
protected open fun playerExecute(loc: Location, player: Player) {
46+
47+
}
48+
4149

4250
class BlockPlace(coord: SCoordinate, val sBlock: SBlock) : SMachineRecipe(coord) {
51+
52+
constructor(map: Map<String, Any>) : this(map["coord"] as? SCoordinate ?: SCoordinate(0, 0, 0), map["sBlock"] as? SBlock ?: SBlock(Material.AIR))
53+
4354
override fun execute(loc: Location) {
4455
sBlock.setLocation(loc)
4556
}
4657

4758
override fun serialize(): MutableMap<String, Any> {
48-
val map = super.serialize()
49-
50-
return map
59+
return super.serialize().also {
60+
it["sBlock"] = sBlock
61+
}
5162
}
5263
}
5364

5465
class BlockBreak(coord: SCoordinate, val sBlock: SBlock) : SMachineRecipe(coord) {
66+
67+
constructor(map: Map<String, Any>) : this(map["coord"] as? SCoordinate ?: SCoordinate(0, 0, 0), map["sBlock"] as? SBlock ?: SBlock(Material.AIR))
68+
5569
override fun execute(loc: Location) {
5670
val block = loc.block
5771
if(sBlock.isSimilar(block)) {
5872
block.type = Material.AIR
5973
}
6074
}
75+
76+
override fun serialize(): MutableMap<String, Any> {
77+
return super.serialize().also {
78+
it["sBlock"] = sBlock
79+
}
80+
}
6181
}
6282

63-
class ItemAddPlayer(coord: SCoordinate, val player: Player, val items: List<ItemStack>) : SMachineRecipe(coord) {
64-
65-
constructor(coord: SCoordinate, player: Player, vararg item: ItemStack) : this(coord, player, item.toList())
83+
class ItemAddPlayer(coord: SCoordinate, val items: List<ItemStack>) : SMachineRecipe(coord) {
6684

67-
override fun execute(loc: Location) {
85+
constructor(coord: SCoordinate, vararg item: ItemStack) : this(coord, item.toList())
86+
87+
constructor(map: Map<String, Any>) : this(
88+
map["coord"] as? SCoordinate ?: SCoordinate(0, 0, 0),
89+
map["items"]?.castList<ItemStack>() ?: emptyList()
90+
)
91+
92+
override fun playerExecute(loc: Location, player: Player) {
6893
player.giveItem(items)
6994
}
95+
96+
override fun serialize(): MutableMap<String, Any> {
97+
return super.serialize().also {
98+
it["items"] = items
99+
}
100+
}
70101
}
71102

72-
class ItemRemovePlayer(coord: SCoordinate, val player: Player, val type: Type, val items: List<ItemStack>) : SMachineRecipe(coord) {
103+
class ItemRemovePlayer(coord: SCoordinate, val type: Type, val items: List<ItemStack>) : SMachineRecipe(coord) {
73104

74-
constructor(coord: SCoordinate, player: Player, type: Type, vararg item: ItemStack) : this(coord, player, type, item.toList())
105+
constructor(coord: SCoordinate, type: Type, vararg item: ItemStack) : this(coord, type, item.toList())
75106

76-
override fun execute(loc: Location) {
107+
constructor(map: Map<String, Any>) : this(
108+
map["coord"] as? SCoordinate ?: SCoordinate(0, 0, 0),
109+
(map["type"] as? String)?.let { Type.valueOf(it) } ?: Type.HAND,
110+
map["items"]?.castList<ItemStack>() ?: emptyList()
111+
)
112+
113+
114+
override fun playerExecute(loc: Location, player: Player) {
77115
val inv = player.inventory
78116
when(type) {
79117
Type.HAND -> {
80-
items.forEach {
118+
items.forEach {
81119
inv.removeHandItem(it)
82120
}
83121
}
84-
122+
85123
Type.OFF_HAND -> {
86-
items.forEach {
124+
items.forEach {
87125
inv.removeOffHandItem(it)
88126
}
89127
}
90-
128+
91129
Type.INVENTORY -> inv.removeSItem(items)
92130
}
93131
}
94132

133+
override fun serialize(): MutableMap<String, Any> {
134+
return super.serialize().also {
135+
it["type"] = type.name
136+
it["items"] = items
137+
}
138+
}
139+
95140
enum class Type {
96141
HAND,
97142
OFF_HAND,
@@ -102,6 +147,11 @@ sealed class SMachineRecipe(val coord: SCoordinate) : ConfigurationSerializable
102147
class ItemAddGround(coord: SCoordinate, val items: List<ItemStack>) : SMachineRecipe(coord) {
103148

104149
constructor(coord: SCoordinate, vararg item: ItemStack) : this(coord, item.toList())
150+
151+
constructor(map: Map<String, Any>) : this(
152+
map["coord"] as? SCoordinate ?: SCoordinate(0, 0, 0),
153+
map["items"]?.castList<ItemStack>() ?: emptyList()
154+
)
105155

106156
override fun execute(loc: Location) {
107157
loc.world?.let { world ->
@@ -110,6 +160,12 @@ sealed class SMachineRecipe(val coord: SCoordinate) : ConfigurationSerializable
110160
}
111161
}
112162
}
163+
164+
override fun serialize(): MutableMap<String, Any> {
165+
return super.serialize().also {
166+
it["items"] = items
167+
}
168+
}
113169
}
114170

115171
class ItemRemoveGround(coord: SCoordinate, val x: Double, val y: Double , val z: Double, val items: List<ItemStack>) : SMachineRecipe(coord) {

src/main/kotlin/io/github/sunshinewzy/sunstcore/objects/SBlock.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.sunshinewzy.sunstcore.objects
22

33
import io.github.sunshinewzy.sunstcore.interfaces.Materialsable
44
import io.github.sunshinewzy.sunstcore.objects.SLocation.Companion.toSLocation
5+
import io.github.sunshinewzy.sunstcore.utils.castList
56
import org.bukkit.Location
67
import org.bukkit.Material
78
import org.bukkit.block.Block
@@ -16,7 +17,16 @@ class SBlock(val type: Material, val damage: Short = -1, var name: String = "")
1617
private var hasTypes: Boolean = false
1718

1819

19-
constructor(map: Map<String, Any>) : this(map["type"] as? Material ?: Material.AIR, map["damage"] as? Short ?: -1, map["name"] as? String ?: "")
20+
constructor(map: Map<String, Any>) : this(map["type"] as? Material ?: Material.AIR, map["damage"] as? Short ?: -1, map["name"] as? String ?: "") {
21+
map["item"]?.let {
22+
if(it is ItemStack)
23+
item = it
24+
}
25+
26+
map["types"]?.castList<Material>()?.let {
27+
types += it
28+
}
29+
}
2030

2131
constructor(type: Material, name: String) : this(type) {
2232
this.name = name

0 commit comments

Comments
 (0)