Skip to content

Commit

Permalink
v2.2.0:
Browse files Browse the repository at this point in the history
- Added HasInHandCommand, HasGivenFromHandCommand and HasPermissionCommand
  • Loading branch information
randombyte-developer committed Jun 13, 2018
1 parent e980710 commit e004e53
Show file tree
Hide file tree
Showing 21 changed files with 227 additions and 91 deletions.
27 changes: 22 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2"
}
}
dependencies {
classpath "gradle.plugin.flavor.pie:promptsign:1.0.2"
}
}

plugins {
id "org.jetbrains.kotlin.jvm" version "1.2.41"
id "org.jetbrains.kotlin.kapt" version "1.2.41"
id "com.github.johnrengelman.shadow" version "1.2.4"
id "flavor.pie.promptsign" version "1.0.2"
}

def isJitpack = System.getenv("JITPACK") != null

if (!isJitpack) {
apply plugin: "flavor.pie.promptsign"
}

group "de.randombyte"
version "2.1.5"
version "2.2.0"

repositories {
jcenter()
Expand All @@ -23,9 +39,8 @@ configurations {
dependencies {
shadow "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.41"
kapt "org.spongepowered:spongeapi:7.0.0"
shadow("com.github.randombyte-developer.kosp:kosp:v1.9.2") { transitive = false }
shadow("com.github.randombyte-developer.kosp:kosp:v2.2.2") { transitive = false }
shadow "org.bstats:bstats-sponge:1.2"
compile "com.github.rojo8399:PlaceholderAPI:065a3e43512426428ad185c044b5d610ce6814bd" // 4.5
}

jar.enabled = false
Expand All @@ -40,7 +55,9 @@ shadowJar {
classifier = null // Remove '-all' suffix from output file name
}
build.dependsOn shadowJar
signArchives.dependsOn shadowJar
if (!isJitpack) {
signArchives.dependsOn shadowJar
}

compileKotlin {
kotlinOptions {
Expand Down
50 changes: 42 additions & 8 deletions src/main/kotlin/de/randombyte/commandutils/CommandUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import de.randombyte.commandutils.CommandUtils.Companion.NAME
import de.randombyte.commandutils.CommandUtils.Companion.PLACEHOLDER_API_ID
import de.randombyte.commandutils.CommandUtils.Companion.VERSION
import de.randombyte.commandutils.alias.CommandListener
import de.randombyte.commandutils.conditions.HasMoneyCommand
import de.randombyte.commandutils.conditions.HasPayedCommand
import de.randombyte.commandutils.conditions.IsAfterCommand
import de.randombyte.commandutils.conditions.IsBeforeCommand
import de.randombyte.commandutils.conditions.*
import de.randombyte.commandutils.config.ConfigAccessor
import de.randombyte.commandutils.config.ConfigUpdater
import de.randombyte.commandutils.execute.delay.DelayCommand
Expand All @@ -29,13 +26,13 @@ import org.spongepowered.api.Sponge
import org.spongepowered.api.command.args.GenericArguments.*
import org.spongepowered.api.command.spec.CommandSpec
import org.spongepowered.api.config.ConfigDir
import org.spongepowered.api.data.type.HandTypes
import org.spongepowered.api.event.Listener
import org.spongepowered.api.event.game.GameReloadEvent
import org.spongepowered.api.event.game.state.GameInitializationEvent
import org.spongepowered.api.event.game.state.GamePostInitializationEvent
import org.spongepowered.api.plugin.Dependency
import org.spongepowered.api.plugin.Plugin
import org.spongepowered.api.plugin.PluginContainer
import java.nio.file.Path

@Plugin(id = ID,
Expand All @@ -46,13 +43,12 @@ import java.nio.file.Path
class CommandUtils @Inject constructor(
val logger: Logger,
@ConfigDir(sharedRoot = false) configPath: Path,
val bStats: Metrics,
val pluginContainer: PluginContainer
val bStats: Metrics
) {
companion object {
const val ID = "command-utils"
const val NAME = "CommandUtils"
const val VERSION = "2.1.5"
const val VERSION = "2.2.0"
const val AUTHOR = "RandomByte"

const val PLACEHOLDER_API_ID = "placeholderapi"
Expand All @@ -68,6 +64,9 @@ class CommandUtils @Inject constructor(
const val TIMESTAMP_ARG = "timestamp"
const val PRICE_ARG = "price"
const val MONEY_ARG = "money"
const val QUANTITY_ARG = "quantity"
const val PERMISSION_ARG = "permission"
const val ITEM_ARG = "item"
const val CONDITION_COMMAND_ARG = "condition_command"

private val LAZY_INSTANCE = lazy { Sponge.getPluginManager().getPlugin(ID).get().instance.get() as CommandUtils }
Expand Down Expand Up @@ -147,6 +146,32 @@ class CommandUtils @Inject constructor(
.executor(HasPayedCommand())
.build()

val hasPermissionCommandSpec = CommandSpec.builder()
.permission("$ROOT_PERMISSION.permission")
.arguments(
userUuidFromNameOrUuid,
string(PERMISSION_ARG.toText()))
.executor(HasPermissionCommand())
.build()

val hasInHandCommandSpec = CommandSpec.builder()
.permission("$ROOT_PERMISSION.in-hand")
.arguments(
userUuidFromNameOrUuid,
string(ITEM_ARG.toText()),
optional(integer(QUANTITY_ARG.toText())))
.executor(HasInHandCommand(HandTypes.MAIN_HAND))
.build()

val hasGivenFromHandCommandSpec = CommandSpec.builder()
.permission("$ROOT_PERMISSION.given-from-hand")
.arguments(
userUuidFromNameOrUuid,
string(ITEM_ARG.toText()),
optional(integer(QUANTITY_ARG.toText())))
.executor(HasGivenFromHandCommand(HandTypes.MAIN_HAND))
.build()

val executeParsedCommandSpec = CommandSpec.builder()
.permission("$ROOT_PERMISSION.parsed")
.arguments(
Expand Down Expand Up @@ -177,6 +202,15 @@ class CommandUtils @Inject constructor(
.child(CommandSpec.builder()
.child(hasMoneyCommandSpec, "money")
.child(hasPayedCommandSpec, "payed")
.child(hasPermissionCommandSpec, "permission")
.child(CommandSpec.builder()
.child(hasInHandCommandSpec, "hand")
.build(), "in")
.child(CommandSpec.builder()
.child(CommandSpec.builder()
.child(hasGivenFromHandCommandSpec, "hand")
.build(), "from")
.build(), "given")
.build(), "has")
.child(CommandSpec.builder()
.child(executeWhenOnlineCommandSpec, "whenOnline")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class CommandListener {
arguments + Pair("\$p", commandSource.name)
} else arguments

executeCommand(command, commandSource, replacements)
executeCommand(
command = command,
commandSource = commandSource,
replacements = replacements
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.randombyte.commandutils.conditions

import de.randombyte.commandutils.execute.getPlayer
import de.randombyte.commandutils.execute.isTruthy
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
import org.spongepowered.api.command.args.CommandContext
import org.spongepowered.api.data.type.HandType

class HasGivenFromHandCommand(handType: HandType) : HasInHandCommand(handType) {
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
val hasInHandCommandResult = super.execute(src, args)
if (!hasInHandCommandResult.isTruthy()) return CommandResult.empty()

// safe call due to 'isTruthy()'
val neededQuantity = hasInHandCommandResult.successCount.get()

val player = args.getPlayer()
val itemInHand = player.getItemInHand(handType).get()
itemInHand.quantity = itemInHand.quantity - neededQuantity
player.setItemInHand(handType, itemInHand)

return CommandResult.successCount(neededQuantity)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.randombyte.commandutils.conditions

import de.randombyte.commandutils.CommandUtils
import de.randombyte.commandutils.execute.getPlayer
import de.randombyte.kosp.extensions.orNull
import de.randombyte.kosp.extensions.toText
import de.randombyte.kosp.extensions.tryAsByteItem
import org.spongepowered.api.command.CommandException
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
import org.spongepowered.api.command.args.CommandContext
import org.spongepowered.api.command.spec.CommandExecutor
import org.spongepowered.api.data.type.HandType
import org.spongepowered.api.item.inventory.ItemStack

open class HasInHandCommand(val handType: HandType) : CommandExecutor {
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
val player = args.getPlayer()
val itemString = args.getOne<String>(CommandUtils.ITEM_ARG).get()
val quantity = args.getOne<Int>(CommandUtils.QUANTITY_ARG).orNull()

val requiredItem = itemString.tryAsByteItem().createStack()
val itemInHand = player.getItemInHand(handType).orElse(ItemStack.empty())

if (quantity != null) {
if (quantity < 1) throw CommandException("'quantity' must be greater than zero!".toText())
requiredItem.quantity = quantity
}

val desiredQuantity = requiredItem.quantity

// the quantity of the item in hand has to be at least as large as the req. quantity
if (desiredQuantity > itemInHand.quantity) return CommandResult.empty()

// ignore quantity
val singleItemInHand = itemInHand.copy().apply { setQuantity(1) }
val singleRequiredItem = requiredItem.copy().apply { setQuantity(1) }

// perform 'deep' equals(with data)
return if (singleItemInHand.equalTo(singleRequiredItem)) {
CommandResult.successCount(desiredQuantity)
} else {
CommandResult.empty()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package de.randombyte.commandutils.conditions

import de.randombyte.commandutils.CommandUtils
import de.randombyte.commandutils.execute.getUserUuid
import de.randombyte.kosp.extensions.getServiceOrFail
import de.randombyte.kosp.extensions.toText
import de.randombyte.kosp.getServiceOrFail
import org.spongepowered.api.command.CommandException
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
Expand All @@ -20,16 +20,11 @@ class HasMoneyCommand : CommandExecutor {
throw CommandException("'money' must be greater than 0.0!".toText())
}

val economyService = getServiceOrFail(EconomyService::class)
val economyService = EconomyService::class.getServiceOrFail()
val balance = economyService
.getOrCreateAccount(playerUuid).get()
.getBalance(economyService.defaultCurrency)

return if (balance >= money.toBigDecimal()) {
CommandResult.success()
} else {
CommandResult.empty()
}

return (balance >= money.toBigDecimal()).toCommandResult()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package de.randombyte.commandutils.conditions

import de.randombyte.commandutils.CommandUtils
import de.randombyte.commandutils.execute.getUserUuid
import de.randombyte.kosp.extensions.getServiceOrFail
import de.randombyte.kosp.extensions.toText
import de.randombyte.kosp.getServiceOrFail
import org.spongepowered.api.command.CommandException
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
Expand All @@ -24,16 +24,12 @@ class HasPayedCommand : CommandExecutor {
throw CommandException("'price' must be greater than 0.0!".toText())
}

val economyService = getServiceOrFail(EconomyService::class)
val economyService = EconomyService::class.getServiceOrFail()
val transactionResult = economyService.getOrCreateAccount(playerUuid).get().withdraw(
economyService.defaultCurrency,
BigDecimal.valueOf(price),
Cause.of(EventContext.empty(), CommandUtils.INSTANCE))

return if (transactionResult.result == ResultType.SUCCESS) {
CommandResult.success()
} else {
CommandResult.empty()
}
return (transactionResult.result == ResultType.SUCCESS).toCommandResult()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.randombyte.commandutils.conditions

import de.randombyte.commandutils.CommandUtils
import de.randombyte.commandutils.execute.getUser
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
import org.spongepowered.api.command.args.CommandContext
import org.spongepowered.api.command.spec.CommandExecutor

class HasPermissionCommand : CommandExecutor {
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
val user = args.getUser()
val permission = args.getOne<String>(CommandUtils.PERMISSION_ARG).get()

return user.hasPermission(permission).toCommandResult()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class IsAfterCommand : CommandExecutor {
val timestamp = args.getOne<String>(CommandUtils.TIMESTAMP_ARG).get()
val date = TimeStampUtils.deserialize(timestamp)

return if (TimeStampUtils.now().after(date)) {
CommandResult.success()
} else {
CommandResult.empty()
}
return TimeStampUtils.now().after(date).toCommandResult()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class IsBeforeCommand : CommandExecutor {
val timestamp = args.getOne<String>(CommandUtils.TIMESTAMP_ARG).get()
val date = TimeStampUtils.deserialize(timestamp)

return if (TimeStampUtils.now().before(date)) {
CommandResult.success()
} else {
CommandResult.empty()
}
return TimeStampUtils.now().before(date).toCommandResult()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.randombyte.commandutils.conditions

import org.spongepowered.api.command.CommandResult

fun Boolean.toCommandResult() = if (this) CommandResult.success() else CommandResult.empty()
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ package de.randombyte.commandutils.config

import de.randombyte.kosp.config.ConfigAccessor
import de.randombyte.kosp.config.ConfigHolder
import de.randombyte.kosp.config.ConfigManager
import de.randombyte.kosp.config.toConfigHolder
import de.randombyte.kosp.extensions.toConfigurationLoader
import java.nio.file.Path

class ConfigAccessor(val configPath: Path) : ConfigAccessor(configPath) {
class ConfigAccessor(configPath: Path) : ConfigAccessor(configPath) {

val aliases: ConfigHolder<AliasConfig> = getConfigHolder("aliases.conf")
val executeWhenOnline: ConfigHolder<ExecuteWhenOnlineConfig> = getConfigHolder("execute-when-online.conf")
val executeOnServerStartup: ConfigHolder<ExecuteOnServerStartupConfig> = getConfigHolder("execute-on-server-startup.conf")
val lastAliasExecutions: ConfigHolder<LastAliasExecutionsConfig> = getConfigHolder("last-alias-executions.conf")

override val holders = listOf(aliases, executeWhenOnline, executeOnServerStartup, lastAliasExecutions)

private inline fun <reified T : Any> getConfigHolder(configName: String): ConfigHolder<T> {
return ConfigManager(configPath.resolve(configName).toConfigurationLoader(), T::class.java).toConfigHolder()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class DelayCommand : CommandExecutor {

Task.builder()
.delay(delayMilliseconds, TimeUnit.MILLISECONDS)
.execute { -> executeCommand(command, src) }
.execute { -> executeCommand(
command = command,
commandSource = src
) }
.submit(CommandUtils.INSTANCE)

return CommandResult.success()
Expand Down
Loading

0 comments on commit e004e53

Please sign in to comment.