Skip to content

Commit e004e53

Browse files
v2.2.0:
- Added HasInHandCommand, HasGivenFromHandCommand and HasPermissionCommand
1 parent e980710 commit e004e53

File tree

21 files changed

+227
-91
lines changed

21 files changed

+227
-91
lines changed

build.gradle

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
buildscript {
2+
repositories {
3+
maven {
4+
url "https://plugins.gradle.org/m2"
5+
}
6+
}
7+
dependencies {
8+
classpath "gradle.plugin.flavor.pie:promptsign:1.0.2"
9+
}
10+
}
11+
112
plugins {
213
id "org.jetbrains.kotlin.jvm" version "1.2.41"
314
id "org.jetbrains.kotlin.kapt" version "1.2.41"
415
id "com.github.johnrengelman.shadow" version "1.2.4"
5-
id "flavor.pie.promptsign" version "1.0.2"
16+
}
17+
18+
def isJitpack = System.getenv("JITPACK") != null
19+
20+
if (!isJitpack) {
21+
apply plugin: "flavor.pie.promptsign"
622
}
723

824
group "de.randombyte"
9-
version "2.1.5"
25+
version "2.2.0"
1026

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

3146
jar.enabled = false
@@ -40,7 +55,9 @@ shadowJar {
4055
classifier = null // Remove '-all' suffix from output file name
4156
}
4257
build.dependsOn shadowJar
43-
signArchives.dependsOn shadowJar
58+
if (!isJitpack) {
59+
signArchives.dependsOn shadowJar
60+
}
4461

4562
compileKotlin {
4663
kotlinOptions {

src/main/kotlin/de/randombyte/commandutils/CommandUtils.kt

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import de.randombyte.commandutils.CommandUtils.Companion.NAME
77
import de.randombyte.commandutils.CommandUtils.Companion.PLACEHOLDER_API_ID
88
import de.randombyte.commandutils.CommandUtils.Companion.VERSION
99
import de.randombyte.commandutils.alias.CommandListener
10-
import de.randombyte.commandutils.conditions.HasMoneyCommand
11-
import de.randombyte.commandutils.conditions.HasPayedCommand
12-
import de.randombyte.commandutils.conditions.IsAfterCommand
13-
import de.randombyte.commandutils.conditions.IsBeforeCommand
10+
import de.randombyte.commandutils.conditions.*
1411
import de.randombyte.commandutils.config.ConfigAccessor
1512
import de.randombyte.commandutils.config.ConfigUpdater
1613
import de.randombyte.commandutils.execute.delay.DelayCommand
@@ -29,13 +26,13 @@ import org.spongepowered.api.Sponge
2926
import org.spongepowered.api.command.args.GenericArguments.*
3027
import org.spongepowered.api.command.spec.CommandSpec
3128
import org.spongepowered.api.config.ConfigDir
29+
import org.spongepowered.api.data.type.HandTypes
3230
import org.spongepowered.api.event.Listener
3331
import org.spongepowered.api.event.game.GameReloadEvent
3432
import org.spongepowered.api.event.game.state.GameInitializationEvent
3533
import org.spongepowered.api.event.game.state.GamePostInitializationEvent
3634
import org.spongepowered.api.plugin.Dependency
3735
import org.spongepowered.api.plugin.Plugin
38-
import org.spongepowered.api.plugin.PluginContainer
3936
import java.nio.file.Path
4037

4138
@Plugin(id = ID,
@@ -46,13 +43,12 @@ import java.nio.file.Path
4643
class CommandUtils @Inject constructor(
4744
val logger: Logger,
4845
@ConfigDir(sharedRoot = false) configPath: Path,
49-
val bStats: Metrics,
50-
val pluginContainer: PluginContainer
46+
val bStats: Metrics
5147
) {
5248
companion object {
5349
const val ID = "command-utils"
5450
const val NAME = "CommandUtils"
55-
const val VERSION = "2.1.5"
51+
const val VERSION = "2.2.0"
5652
const val AUTHOR = "RandomByte"
5753

5854
const val PLACEHOLDER_API_ID = "placeholderapi"
@@ -68,6 +64,9 @@ class CommandUtils @Inject constructor(
6864
const val TIMESTAMP_ARG = "timestamp"
6965
const val PRICE_ARG = "price"
7066
const val MONEY_ARG = "money"
67+
const val QUANTITY_ARG = "quantity"
68+
const val PERMISSION_ARG = "permission"
69+
const val ITEM_ARG = "item"
7170
const val CONDITION_COMMAND_ARG = "condition_command"
7271

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

149+
val hasPermissionCommandSpec = CommandSpec.builder()
150+
.permission("$ROOT_PERMISSION.permission")
151+
.arguments(
152+
userUuidFromNameOrUuid,
153+
string(PERMISSION_ARG.toText()))
154+
.executor(HasPermissionCommand())
155+
.build()
156+
157+
val hasInHandCommandSpec = CommandSpec.builder()
158+
.permission("$ROOT_PERMISSION.in-hand")
159+
.arguments(
160+
userUuidFromNameOrUuid,
161+
string(ITEM_ARG.toText()),
162+
optional(integer(QUANTITY_ARG.toText())))
163+
.executor(HasInHandCommand(HandTypes.MAIN_HAND))
164+
.build()
165+
166+
val hasGivenFromHandCommandSpec = CommandSpec.builder()
167+
.permission("$ROOT_PERMISSION.given-from-hand")
168+
.arguments(
169+
userUuidFromNameOrUuid,
170+
string(ITEM_ARG.toText()),
171+
optional(integer(QUANTITY_ARG.toText())))
172+
.executor(HasGivenFromHandCommand(HandTypes.MAIN_HAND))
173+
.build()
174+
150175
val executeParsedCommandSpec = CommandSpec.builder()
151176
.permission("$ROOT_PERMISSION.parsed")
152177
.arguments(
@@ -177,6 +202,15 @@ class CommandUtils @Inject constructor(
177202
.child(CommandSpec.builder()
178203
.child(hasMoneyCommandSpec, "money")
179204
.child(hasPayedCommandSpec, "payed")
205+
.child(hasPermissionCommandSpec, "permission")
206+
.child(CommandSpec.builder()
207+
.child(hasInHandCommandSpec, "hand")
208+
.build(), "in")
209+
.child(CommandSpec.builder()
210+
.child(CommandSpec.builder()
211+
.child(hasGivenFromHandCommandSpec, "hand")
212+
.build(), "from")
213+
.build(), "given")
180214
.build(), "has")
181215
.child(CommandSpec.builder()
182216
.child(executeWhenOnlineCommandSpec, "whenOnline")

src/main/kotlin/de/randombyte/commandutils/alias/CommandListener.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ class CommandListener {
6464
arguments + Pair("\$p", commandSource.name)
6565
} else arguments
6666

67-
executeCommand(command, commandSource, replacements)
67+
executeCommand(
68+
command = command,
69+
commandSource = commandSource,
70+
replacements = replacements
71+
)
6872
}
6973
}
7074

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package de.randombyte.commandutils.conditions
2+
3+
import de.randombyte.commandutils.execute.getPlayer
4+
import de.randombyte.commandutils.execute.isTruthy
5+
import org.spongepowered.api.command.CommandResult
6+
import org.spongepowered.api.command.CommandSource
7+
import org.spongepowered.api.command.args.CommandContext
8+
import org.spongepowered.api.data.type.HandType
9+
10+
class HasGivenFromHandCommand(handType: HandType) : HasInHandCommand(handType) {
11+
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
12+
val hasInHandCommandResult = super.execute(src, args)
13+
if (!hasInHandCommandResult.isTruthy()) return CommandResult.empty()
14+
15+
// safe call due to 'isTruthy()'
16+
val neededQuantity = hasInHandCommandResult.successCount.get()
17+
18+
val player = args.getPlayer()
19+
val itemInHand = player.getItemInHand(handType).get()
20+
itemInHand.quantity = itemInHand.quantity - neededQuantity
21+
player.setItemInHand(handType, itemInHand)
22+
23+
return CommandResult.successCount(neededQuantity)
24+
}
25+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.randombyte.commandutils.conditions
2+
3+
import de.randombyte.commandutils.CommandUtils
4+
import de.randombyte.commandutils.execute.getPlayer
5+
import de.randombyte.kosp.extensions.orNull
6+
import de.randombyte.kosp.extensions.toText
7+
import de.randombyte.kosp.extensions.tryAsByteItem
8+
import org.spongepowered.api.command.CommandException
9+
import org.spongepowered.api.command.CommandResult
10+
import org.spongepowered.api.command.CommandSource
11+
import org.spongepowered.api.command.args.CommandContext
12+
import org.spongepowered.api.command.spec.CommandExecutor
13+
import org.spongepowered.api.data.type.HandType
14+
import org.spongepowered.api.item.inventory.ItemStack
15+
16+
open class HasInHandCommand(val handType: HandType) : CommandExecutor {
17+
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
18+
val player = args.getPlayer()
19+
val itemString = args.getOne<String>(CommandUtils.ITEM_ARG).get()
20+
val quantity = args.getOne<Int>(CommandUtils.QUANTITY_ARG).orNull()
21+
22+
val requiredItem = itemString.tryAsByteItem().createStack()
23+
val itemInHand = player.getItemInHand(handType).orElse(ItemStack.empty())
24+
25+
if (quantity != null) {
26+
if (quantity < 1) throw CommandException("'quantity' must be greater than zero!".toText())
27+
requiredItem.quantity = quantity
28+
}
29+
30+
val desiredQuantity = requiredItem.quantity
31+
32+
// the quantity of the item in hand has to be at least as large as the req. quantity
33+
if (desiredQuantity > itemInHand.quantity) return CommandResult.empty()
34+
35+
// ignore quantity
36+
val singleItemInHand = itemInHand.copy().apply { setQuantity(1) }
37+
val singleRequiredItem = requiredItem.copy().apply { setQuantity(1) }
38+
39+
// perform 'deep' equals(with data)
40+
return if (singleItemInHand.equalTo(singleRequiredItem)) {
41+
CommandResult.successCount(desiredQuantity)
42+
} else {
43+
CommandResult.empty()
44+
}
45+
}
46+
}

src/main/kotlin/de/randombyte/commandutils/conditions/HasMoneyCommand.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package de.randombyte.commandutils.conditions
22

33
import de.randombyte.commandutils.CommandUtils
44
import de.randombyte.commandutils.execute.getUserUuid
5+
import de.randombyte.kosp.extensions.getServiceOrFail
56
import de.randombyte.kosp.extensions.toText
6-
import de.randombyte.kosp.getServiceOrFail
77
import org.spongepowered.api.command.CommandException
88
import org.spongepowered.api.command.CommandResult
99
import org.spongepowered.api.command.CommandSource
@@ -20,16 +20,11 @@ class HasMoneyCommand : CommandExecutor {
2020
throw CommandException("'money' must be greater than 0.0!".toText())
2121
}
2222

23-
val economyService = getServiceOrFail(EconomyService::class)
23+
val economyService = EconomyService::class.getServiceOrFail()
2424
val balance = economyService
2525
.getOrCreateAccount(playerUuid).get()
2626
.getBalance(economyService.defaultCurrency)
2727

28-
return if (balance >= money.toBigDecimal()) {
29-
CommandResult.success()
30-
} else {
31-
CommandResult.empty()
32-
}
33-
28+
return (balance >= money.toBigDecimal()).toCommandResult()
3429
}
3530
}

src/main/kotlin/de/randombyte/commandutils/conditions/HasPayedCommand.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package de.randombyte.commandutils.conditions
22

33
import de.randombyte.commandutils.CommandUtils
44
import de.randombyte.commandutils.execute.getUserUuid
5+
import de.randombyte.kosp.extensions.getServiceOrFail
56
import de.randombyte.kosp.extensions.toText
6-
import de.randombyte.kosp.getServiceOrFail
77
import org.spongepowered.api.command.CommandException
88
import org.spongepowered.api.command.CommandResult
99
import org.spongepowered.api.command.CommandSource
@@ -24,16 +24,12 @@ class HasPayedCommand : CommandExecutor {
2424
throw CommandException("'price' must be greater than 0.0!".toText())
2525
}
2626

27-
val economyService = getServiceOrFail(EconomyService::class)
27+
val economyService = EconomyService::class.getServiceOrFail()
2828
val transactionResult = economyService.getOrCreateAccount(playerUuid).get().withdraw(
2929
economyService.defaultCurrency,
3030
BigDecimal.valueOf(price),
3131
Cause.of(EventContext.empty(), CommandUtils.INSTANCE))
3232

33-
return if (transactionResult.result == ResultType.SUCCESS) {
34-
CommandResult.success()
35-
} else {
36-
CommandResult.empty()
37-
}
33+
return (transactionResult.result == ResultType.SUCCESS).toCommandResult()
3834
}
3935
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package de.randombyte.commandutils.conditions
2+
3+
import de.randombyte.commandutils.CommandUtils
4+
import de.randombyte.commandutils.execute.getUser
5+
import org.spongepowered.api.command.CommandResult
6+
import org.spongepowered.api.command.CommandSource
7+
import org.spongepowered.api.command.args.CommandContext
8+
import org.spongepowered.api.command.spec.CommandExecutor
9+
10+
class HasPermissionCommand : CommandExecutor {
11+
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
12+
val user = args.getUser()
13+
val permission = args.getOne<String>(CommandUtils.PERMISSION_ARG).get()
14+
15+
return user.hasPermission(permission).toCommandResult()
16+
}
17+
}

src/main/kotlin/de/randombyte/commandutils/conditions/IsAfterCommand.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ class IsAfterCommand : CommandExecutor {
1212
val timestamp = args.getOne<String>(CommandUtils.TIMESTAMP_ARG).get()
1313
val date = TimeStampUtils.deserialize(timestamp)
1414

15-
return if (TimeStampUtils.now().after(date)) {
16-
CommandResult.success()
17-
} else {
18-
CommandResult.empty()
19-
}
15+
return TimeStampUtils.now().after(date).toCommandResult()
2016
}
2117
}

src/main/kotlin/de/randombyte/commandutils/conditions/IsBeforeCommand.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ class IsBeforeCommand : CommandExecutor {
1212
val timestamp = args.getOne<String>(CommandUtils.TIMESTAMP_ARG).get()
1313
val date = TimeStampUtils.deserialize(timestamp)
1414

15-
return if (TimeStampUtils.now().before(date)) {
16-
CommandResult.success()
17-
} else {
18-
CommandResult.empty()
19-
}
15+
return TimeStampUtils.now().before(date).toCommandResult()
2016
}
2117
}

0 commit comments

Comments
 (0)