mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-08-28 00:15:56 +02:00
add /ca enchant
This commit is contained in:
parent
c062684a3c
commit
a202df9f0a
8 changed files with 136 additions and 17 deletions
|
|
@ -39,7 +39,8 @@ ca.bypass.level: Allow player to bypass every level limit (no custom limit)
|
||||||
|
|
||||||
# Command permissions
|
# Command permissions
|
||||||
ca.command.reload: Allow administrator to reload the plugin's configs
|
ca.command.reload: Allow administrator to reload the plugin's configs
|
||||||
ca.command.diagnostic: Allow adminastator to diagnistic some simple problem with the plugin
|
ca.command.diagnostic: Get debug information about the plugin and server
|
||||||
|
ca.command.enchantment: Allows to set enchantment to holden item
|
||||||
ca.config.edit: Allow administrator to edit the plugin's config in game
|
ca.config.edit: Allow administrator to edit the plugin's config in game
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,8 @@ open class CustomAnvil : JavaPlugin() {
|
||||||
// Permission string required to edit the plugin's config
|
// Permission string required to edit the plugin's config
|
||||||
const val editConfigPermission = "ca.config.edit"
|
const val editConfigPermission = "ca.config.edit"
|
||||||
|
|
||||||
|
// Permission string required to edit the plugin's config
|
||||||
|
const val giveEnchantmentPermission = "ca.command.enchantment"
|
||||||
|
|
||||||
// Command Name to reload the config
|
// Command Name to reload the config
|
||||||
const val commandReloadName = "anvilconfigreload"
|
const val commandReloadName = "anvilconfigreload"
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import org.bukkit.ChatColor
|
||||||
import org.bukkit.command.Command
|
import org.bukkit.command.Command
|
||||||
import org.bukkit.command.CommandExecutor
|
import org.bukkit.command.CommandExecutor
|
||||||
import org.bukkit.command.CommandSender
|
import org.bukkit.command.CommandSender
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||||
|
|
||||||
abstract class CASubCommand: CommandExecutor {
|
abstract class CASubCommand: CommandExecutor {
|
||||||
|
|
||||||
|
|
@ -43,4 +44,14 @@ abstract class CASubCommand: CommandExecutor {
|
||||||
return "no description"
|
return "no description"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected fun allEnchantmentsByName(): Collection<String> {
|
||||||
|
val names = mutableSetOf<String>()
|
||||||
|
for (enchantment in CAEnchantmentRegistry.getInstance().values()) {
|
||||||
|
names.add(enchantment.name)
|
||||||
|
names.add(enchantment.key.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -7,7 +7,6 @@ import org.bukkit.command.CommandExecutor
|
||||||
import org.bukkit.command.CommandSender
|
import org.bukkit.command.CommandSender
|
||||||
import org.bukkit.command.TabCompleter
|
import org.bukkit.command.TabCompleter
|
||||||
import xyz.alexcrea.cuanvil.util.MetricsUtil
|
import xyz.alexcrea.cuanvil.util.MetricsUtil
|
||||||
import java.util.ArrayList
|
|
||||||
|
|
||||||
class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter {
|
class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter {
|
||||||
|
|
||||||
|
|
@ -18,13 +17,14 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter {
|
||||||
|
|
||||||
private val editConfigCommand = EditConfigExecutor()
|
private val editConfigCommand = EditConfigExecutor()
|
||||||
private val helpCommand = HelpExecutor()
|
private val helpCommand = HelpExecutor()
|
||||||
private val commands = ImmutableMap.of(
|
private val commands: ImmutableMap<String, CASubCommand> = ImmutableMap.of(
|
||||||
"gui", editConfigCommand,
|
"gui", editConfigCommand,
|
||||||
"config", editConfigCommand,
|
"config", editConfigCommand,
|
||||||
"reload", ReloadExecutor(),
|
"reload", ReloadExecutor(),
|
||||||
"diagnostic", DiagnosticExecutor(),
|
"diagnostic", DiagnosticExecutor(),
|
||||||
"debug", DebugToggleExecutor(),
|
"debug", DebugToggleExecutor(),
|
||||||
"help", helpCommand,
|
"help", helpCommand,
|
||||||
|
"enchant", EnchantExecutor(),
|
||||||
)
|
)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|
@ -95,9 +95,11 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter {
|
||||||
}
|
}
|
||||||
|
|
||||||
//assumed all provided tab completed string are lowercase
|
//assumed all provided tab completed string are lowercase
|
||||||
|
val prefix = args[args.size - 1]
|
||||||
return result.stream()
|
return result.stream()
|
||||||
.filter { it.startsWith(args[args.size - 1]) }
|
.filter { it.startsWith(prefix) }
|
||||||
.sorted()
|
.sorted()
|
||||||
.toList()
|
.toList()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import org.bukkit.entity.HumanEntity
|
||||||
import xyz.alexcrea.cuanvil.api.EnchantmentApi
|
import xyz.alexcrea.cuanvil.api.EnchantmentApi
|
||||||
import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil
|
import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil
|
||||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
|
||||||
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui
|
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui
|
||||||
import xyz.alexcrea.cuanvil.gui.config.global.EnchantConfigGui
|
import xyz.alexcrea.cuanvil.gui.config.global.EnchantConfigGui
|
||||||
import xyz.alexcrea.cuanvil.gui.config.global.ItemConfigGui
|
import xyz.alexcrea.cuanvil.gui.config.global.ItemConfigGui
|
||||||
|
|
@ -103,16 +102,6 @@ class EditConfigExecutor : CASubCommand() {
|
||||||
// Tab completer
|
// Tab completer
|
||||||
// ---------------
|
// ---------------
|
||||||
|
|
||||||
private fun allEnchantmentsByName(): Collection<String> {
|
|
||||||
val names = mutableSetOf<String>()
|
|
||||||
for (enchantment in CAEnchantmentRegistry.getInstance().values()) {
|
|
||||||
names.add(enchantment.name)
|
|
||||||
names.add(enchantment.key.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
return names
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun tabCompleter(sender: CommandSender, args: Array<out String>, list: MutableList<String>) {
|
override fun tabCompleter(sender: CommandSender, args: Array<out String>, list: MutableList<String>) {
|
||||||
list.addAll(
|
list.addAll(
|
||||||
when (args.size) {
|
when (args.size) {
|
||||||
|
|
@ -127,7 +116,6 @@ class EditConfigExecutor : CASubCommand() {
|
||||||
else -> listOf()
|
else -> listOf()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
113
src/main/kotlin/xyz/alexcrea/cuanvil/command/EnchantExecutor.kt
Normal file
113
src/main/kotlin/xyz/alexcrea/cuanvil/command/EnchantExecutor.kt
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
package xyz.alexcrea.cuanvil.command
|
||||||
|
|
||||||
|
import io.delilaheve.CustomAnvil
|
||||||
|
import io.delilaheve.util.ConfigOptions
|
||||||
|
import org.bukkit.command.Command
|
||||||
|
import org.bukkit.command.CommandSender
|
||||||
|
import org.bukkit.entity.HumanEntity
|
||||||
|
import xyz.alexcrea.cuanvil.api.EnchantmentApi
|
||||||
|
import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir
|
||||||
|
|
||||||
|
class EnchantExecutor : CASubCommand() {
|
||||||
|
|
||||||
|
|
||||||
|
override fun allowed(sender: CommandSender): Boolean {
|
||||||
|
return sender.hasPermission(CustomAnvil.giveEnchantmentPermission)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun description(): String {
|
||||||
|
return "Allows to set enchantment to holden item"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun executeCommand(
|
||||||
|
sender: CommandSender,
|
||||||
|
cmd: Command,
|
||||||
|
cmdstr: String,
|
||||||
|
args: Array<out String>
|
||||||
|
): Boolean {
|
||||||
|
if (sender !is HumanEntity) return true
|
||||||
|
|
||||||
|
if (!allowed(sender)) {
|
||||||
|
sender.sendMessage("No permission to execute this command")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
when (args.size) {
|
||||||
|
0 -> {
|
||||||
|
sender.sendMessage("Missing enchantment parameter")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
1 -> {
|
||||||
|
sender.sendMessage("Missing level parameter")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val enchants = EnchantmentApi.getListByName(args[0].lowercase())
|
||||||
|
if (enchants.isEmpty()) {
|
||||||
|
sender.sendMessage("Enchantment not found: ${args[0]}")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
val enchant = enchants.iterator().next()
|
||||||
|
|
||||||
|
val level = args[1].toIntOrNull()?.coerceIn(0, ConfigOptions.ENCHANT_LIMIT)
|
||||||
|
if (level == null) {
|
||||||
|
sender.sendMessage("Invalid number: ${args[1]}")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
val inHand = sender.inventory.itemInMainHand
|
||||||
|
if (inHand.isAir) {
|
||||||
|
sender.sendMessage("Cannot enchant this item")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level == 0) {
|
||||||
|
enchant.removeFrom(inHand)
|
||||||
|
sender.sendMessage("${enchant.prettyName} removed")
|
||||||
|
} else {
|
||||||
|
enchant.addEnchantmentUnsafe(inHand, level)
|
||||||
|
sender.sendMessage("${enchant.prettyName} set to level $level")
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun tabCompleter(sender: CommandSender, args: Array<out String>, list: MutableList<String>) {
|
||||||
|
if (sender !is HumanEntity) return
|
||||||
|
|
||||||
|
list.addAll(
|
||||||
|
when (args.size) {
|
||||||
|
1 -> allEnchantmentsByName()
|
||||||
|
2 -> findEnchantmentLevels(sender, args[0])
|
||||||
|
|
||||||
|
else -> listOf()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findEnchantmentLevels(
|
||||||
|
sender: HumanEntity,
|
||||||
|
name: String
|
||||||
|
): Collection<String> {
|
||||||
|
val enchants = EnchantmentApi.getListByName(name.lowercase())
|
||||||
|
if (enchants.isEmpty()) return listOf()
|
||||||
|
|
||||||
|
val enchant = enchants.iterator().next()
|
||||||
|
val limit = ConfigOptions.enchantLimit(enchant)
|
||||||
|
val result = mutableListOf<String>()
|
||||||
|
|
||||||
|
for (i in 1 until limit + 1) {
|
||||||
|
result.add(i.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
val inHand = sender.inventory.itemInMainHand
|
||||||
|
if (enchant.isEnchantmentPresent(inHand))
|
||||||
|
result.add("0")
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ package xyz.alexcrea.cuanvil.command
|
||||||
import io.delilaheve.CustomAnvil
|
import io.delilaheve.CustomAnvil
|
||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
import org.bukkit.command.Command
|
import org.bukkit.command.Command
|
||||||
import org.bukkit.command.CommandExecutor
|
|
||||||
import org.bukkit.command.CommandSender
|
import org.bukkit.command.CommandSender
|
||||||
import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent
|
import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,9 @@ permissions:
|
||||||
ca.command.diagnostic:
|
ca.command.diagnostic:
|
||||||
default: op
|
default: op
|
||||||
description: Get debug information about the plugin and server
|
description: Get debug information about the plugin and server
|
||||||
|
ca.command.enchantment:
|
||||||
|
default: op
|
||||||
|
description: Allows to set enchantment to holden item
|
||||||
ca.config.edit:
|
ca.config.edit:
|
||||||
default: op
|
default: op
|
||||||
description: Allow administrator to edit the plugin's config in game
|
description: Allow administrator to edit the plugin's config in game
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue