Skip to content

Commit f79bd3e

Browse files
committed
Fix recolor command
1 parent 942e770 commit f79bd3e

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

src/main/kotlin/dev/lumas/jobsaddons/commands/ReColorCommand.kt

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,30 @@ import org.bukkit.entity.Player
2222
class ReColorCommand : AbstractCommand() {
2323

2424
companion object {
25-
private val COLORS = listOf("WHITE", "LIGHT_GRAY", "GRAY", "BLACK", "BROWN", "RED", "ORANGE", "YELLOW", "LIME", "GREEN", "CYAN", "LIGHT_BLUE", "BLUE", "PURPLE", "MAGENTA", "PINK")
26-
private val DYEABLE_PATTERN = Regex("^(?<base>.+)_((WOOL)|(CARPET)|(CONCRETE)|(CONCRETE_POWDER)|(TERRACOTTA)|(GLAZED_TERRACOTTA)|(STAINED_GLASS)|(STAINED_GLASS_PANE)|(BED)|(SHULKER_BOX)|(CANDLE)|(BANNER))$")
25+
private val COLORS = listOf(
26+
"WHITE", "LIGHT_GRAY", "GRAY", "BLACK", "BROWN", "RED", "ORANGE", "YELLOW",
27+
"LIME", "GREEN", "CYAN", "LIGHT_BLUE", "BLUE", "PURPLE", "MAGENTA", "PINK"
28+
)
29+
private val COLOR_PREFIX = Regex(
30+
"^(WHITE|LIGHT_GRAY|GRAY|BLACK|BROWN|RED|ORANGE|YELLOW|LIME|GREEN|CYAN|LIGHT_BLUE|BLUE|PURPLE|MAGENTA|PINK)_(.+)$"
31+
)
32+
private val VALID_SUFFIXES = setOf(
33+
"WOOL",
34+
"CARPET",
35+
"CONCRETE",
36+
"CONCRETE_POWDER",
37+
"TERRACOTTA",
38+
"GLAZED_TERRACOTTA",
39+
"STAINED_GLASS",
40+
"STAINED_GLASS_PANE",
41+
"SHULKER_BOX",
42+
"BED",
43+
"CANDLE",
44+
"BANNER",
45+
//"BUNDLE",
46+
"HARNESS",
47+
"DYE"
48+
)
2749
}
2850

2951
override fun handle(sender: CommandSender, label: String, args: Array<String>): Boolean {
@@ -39,27 +61,27 @@ class ReColorCommand : AbstractCommand() {
3961
return true
4062
}
4163

42-
val items = player.inventory.contents
43-
.filterNotNull()
44-
.filter { itemStack ->
45-
!itemStack.hasItemMeta() && DYEABLE_PATTERN.matches(itemStack.type.name)
46-
}
64+
var changed = 0
65+
66+
for (item in player.inventory.contents.filterNotNull()) {
67+
val match = COLOR_PREFIX.matchEntire(item.type.name) ?: continue
68+
val suffix = match.groupValues[2]
69+
if (suffix !in VALID_SUFFIXES) continue
4770

48-
for (item in items) {
49-
val matchResult = DYEABLE_PATTERN.find(item.type.name) ?: continue
50-
val base = matchResult.groups["base"]?.value ?: continue
51-
val newMaterialName = "${color}_${base.substringAfterLast("_")}"
52-
val newMaterial = ClassUtil.enumValueOfOrNull(Material::class.java, newMaterialName)
53-
newMaterial?.let {
71+
val newMaterial = ClassUtil.enumValueOfOrNull(Material::class.java, "${color}_$suffix") ?: continue
72+
if (item.type != newMaterial) {
5473
@Suppress("DEPRECATION")
55-
item.type = it
74+
item.type = newMaterial
75+
changed += item.amount
5676
}
5777
}
58-
Text.msg(player, "Recolored ${items.size} items to $name.")
78+
79+
Text.msg(player, "Recolored $changed item(s) to ${color.lowercase()}.")
5980
return true
6081
}
6182

62-
override fun handleTabComplete(sender: CommandSender, label: String, args: Array<String>): List<String>? {
63-
return COLORS.map { it.lowercase() }
83+
override fun handleTabComplete(sender: CommandSender, label: String, args: Array<String>): List<String> {
84+
val prefix = args.getOrNull(0)?.lowercase() ?: ""
85+
return COLORS.map { it.lowercase() }.filter { it.startsWith(prefix) }
6486
}
6587
}

0 commit comments

Comments
 (0)