-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from SpigotBasics/separate-tabcomplete
Refactored TabCompleter into own interface; refactored common context methods into own class
- Loading branch information
Showing
5 changed files
with
112 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
core/src/main/kotlin/com/github/spigotbasics/core/command/BasicsCommandContextHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.spigotbasics.core.command | ||
|
||
import com.github.spigotbasics.core.messages.CoreMessages | ||
import com.github.spigotbasics.core.messages.MessageFactory | ||
import org.bukkit.Bukkit | ||
import org.bukkit.command.CommandSender | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.permissions.Permission | ||
|
||
abstract class BasicsCommandContextHandler( | ||
val coreMessages: CoreMessages, | ||
val messageFactory: MessageFactory, | ||
) { | ||
@Throws(BasicsCommandException::class) | ||
fun requirePlayer(name: String): Player { | ||
val player = Bukkit.getPlayer(name) | ||
if (player == null) { | ||
throw BasicsCommandException(CommandResult.playerNotFound(name)) | ||
} | ||
return player | ||
} | ||
|
||
@Throws(BasicsCommandException::class) | ||
fun notFromConsole(sender: CommandSender): Player { | ||
if (sender !is Player) { | ||
throw CommandResult.NOT_FROM_CONSOLE.asException() | ||
} | ||
return sender | ||
} | ||
|
||
// TODO: In 99% of cases, we should just use requirePlayer(CommandSender) instead of this method | ||
@Throws(BasicsCommandException::class) | ||
fun requirePlayerOrMustSpecifyPlayerFromConsole(sender: CommandSender): Player { | ||
val player = sender as? Player | ||
if (player == null) { | ||
throw CommandResult.MUST_BE_PLAYER_OR_SPECIFY_PLAYER_FROM_CONSOLE.asException() | ||
} | ||
return player | ||
} | ||
|
||
@Throws(BasicsCommandException::class) | ||
fun failIfFlagsLeft(context: BasicsCommandContext) { | ||
if (context.flags.isEmpty()) return | ||
throw CommandResult.unknownFlag(context.flags[0]).asException() | ||
} | ||
|
||
@Throws(BasicsCommandException::class) | ||
fun failInvalidArgument(argument: String): CommandResult { | ||
throw CommandResult.invalidArgument(argument).asException() | ||
} | ||
|
||
fun requirePermission( | ||
sender: CommandSender, | ||
permission: Permission, | ||
) { | ||
if (!sender.hasPermission(permission)) { | ||
throw CommandResult.noPermission(permission).asException() | ||
} | ||
} | ||
|
||
// @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") | ||
|
||
fun requireItemInHand(player: Player): ItemStack { | ||
val item = player.inventory.itemInMainHand | ||
if (item.type.isAir) { | ||
throw CommandResult.NO_ITEM_IN_HAND.asException() | ||
} | ||
return item | ||
} | ||
|
||
fun requireItemInHandOther(player: Player): ItemStack { | ||
val item = player.inventory.itemInMainHand | ||
if (item.type.isAir) { | ||
throw CommandResult.noItemInHandOthers(player).asException() | ||
} | ||
return item | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
core/src/main/kotlin/com/github/spigotbasics/core/command/BasicsTabCompleter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.github.spigotbasics.core.command | ||
|
||
interface BasicsTabCompleter { | ||
fun tabComplete(context: BasicsCommandContext): MutableList<String>? | ||
} |