Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/kotlin/xyz/atrius/waystones/command/BaseCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ abstract class BaseCommand<S : SubCommand>(
.build(literal(command.name))

command.basePermission?.let { permission ->
subCommand.requires { it.sender.hasPermission(permission) }
val existing = subCommand.requirement
subCommand.requires { (existing == null || existing.test(it)) && it.sender.hasPermission(permission) }
}

base.then(subCommand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xyz.atrius.waystones.command.waystones
import com.mojang.brigadier.Command
import com.mojang.brigadier.arguments.IntegerArgumentType
import com.mojang.brigadier.builder.ArgumentBuilder
import com.mojang.brigadier.context.CommandContext
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands.argument
import io.papermc.paper.command.brigadier.argument.ArgumentTypes
Expand All @@ -14,6 +15,7 @@ import xyz.atrius.waystones.manager.LocalizationManager
import xyz.atrius.waystones.provider.DefaultKeyProvider
import xyz.atrius.waystones.utility.getArgument
import xyz.atrius.waystones.utility.message
import xyz.atrius.waystones.utility.senderTypeName

@Single
class KeyCommand(
Expand All @@ -28,17 +30,12 @@ class KeyCommand(
override fun build(base: ArgumentBuilder<CommandSourceStack, *>): ArgumentBuilder<CommandSourceStack, *> {
val base = base
.requires { it.sender is Player }
.executes {
val sender = it.source.sender as Player
command(sender, 1, sender)
}
.executes { executeWithPlayer(it, 1) }
val count = argument("count", IntegerArgumentType.integer())
.requires { it.sender is Player }
.executes {
val sender = it.source.sender as Player
val amount = it.getArgument<Int>("count")

command(sender, amount, sender)
executeWithPlayer(it, amount)
}
val target = argument("target", ArgumentTypes.player())
.requires { it.sender.hasPermission("waystones.getkey.all") }
Expand All @@ -56,6 +53,18 @@ class KeyCommand(
return base.then(count)
}

private fun executeWithPlayer(context: CommandContext<CommandSourceStack>, amount: Int): Int {
val sender = context.source.sender
val player = sender as? Player

if (player == null) {
sender.message(localization["command-bad-sender", sender.senderTypeName(localization)])
return Command.SINGLE_SUCCESS
}

return command(sender, amount, player)
}

private fun command(sender: CommandSender, amount: Int, target: Player): Int {
target.inventory.addItem(defaultKeyProvider.getKey(target, amount))
sender.message(localization["command-give-key", amount, target.name])
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/xyz/atrius/waystones/utility/CommandUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package xyz.atrius.waystones.utility

import com.mojang.brigadier.context.CommandContext
import io.papermc.paper.command.brigadier.CommandSourceStack
import org.bukkit.command.BlockCommandSender
import org.bukkit.command.CommandSender
import org.bukkit.command.ConsoleCommandSender
import org.bukkit.command.RemoteConsoleCommandSender
import org.bukkit.entity.Player
import xyz.atrius.waystones.manager.LocalizationManager

inline fun <reified T> CommandContext<CommandSourceStack>.getArgument(name: String): T =
getArgument(name, T::class.java)
Expand All @@ -28,3 +34,16 @@ fun <T> CommandContext<CommandSourceStack>.getArguments(
}
}
}

fun CommandSender.senderTypeName(localization: LocalizationManager): String {
val player = this as? Player
val message = when (this) {
is RemoteConsoleCommandSender -> localization["sender-type-rcon"]
is ConsoleCommandSender -> localization["sender-type-console"]
is BlockCommandSender -> localization["sender-type-block"]
is Player -> localization["sender-type-player"]
else -> localization["sender-type-unknown"]
}

return message.format(player)
}
2 changes: 1 addition & 1 deletion src/main/kotlin/xyz/atrius/waystones/utility/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ operator fun <T> ItemMeta.set(key: String, type: PersistentDataType<T, T>, value
.set(key.toKey(), type, value ?: error("Value must be provided!"))

fun PlayerInventory.addItemNaturally(original: ItemStack, new: ItemStack) {
val player = holder as Player
val player = holder as? Player ?: return
// Add item to inventory
if (player.immortal) {
addItem(new)
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/locale-en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,9 @@ migrate-waystone-migrations-failed: "&c{0} waystone {0,choice,1#migration|1<migr
waystone-list-entry: "&d&l[&7{0}@{1}:{2}:{3}&d&l]: &r&b&o{4}"
waystone-list-empty: "&7[&dWaystones&7] &cNo waystones currently exist!"
waystone-list-no-entries: "&7[&dWaystones&7] &cNo waystone entries present for page {0}"
waystone-list-hover: "Teleport to location"
waystone-list-hover: "Teleport to location"
sender-type-console: "Console"
sender-type-block: "Command block"
sender-type-rcon: "RCON"
sender-type-player: "Player"
sender-type-unknown: "Unknown source"
5 changes: 5 additions & 0 deletions src/main/resources/locale-zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ clean-energy: "干净能源"
clean-energy-desc: "使用一个被信标充能的传送石"
how-low-can-you-go: "你能下到多深处?"
how-low-can-you-go-desc: "在传送中误入混沌世界"
sender-type-console: "控制台"
sender-type-block: "命令方块"
sender-type-rcon: "远程控制"
sender-type-player: "玩家"
sender-type-unknown: "未知来源"
Loading