Skip to content

Commit 115baab

Browse files
Added HasGivenFromInventoryCommand and HasInInventoryCommand
1 parent f5447e1 commit 115baab

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ class CommandUtils @Inject constructor(
179179
.executor(HasGivenFromHandCommand(HandTypes.MAIN_HAND))
180180
.build()
181181

182+
val hasInInventoryCommandSpec = CommandSpec.builder()
183+
.permission("$ROOT_PERMISSION.in-inventory")
184+
.arguments(
185+
userUuidFromNameOrUuid,
186+
string(ITEM_ARG.toText()),
187+
optional(integer(QUANTITY_ARG.toText())))
188+
.executor(HasInInventoryCommand())
189+
.build()
190+
191+
val hasGivenFromInventoryCommandSpec = CommandSpec.builder()
192+
.permission("$ROOT_PERMISSION.given-from-inventory")
193+
.arguments(
194+
userUuidFromNameOrUuid,
195+
string(ITEM_ARG.toText()),
196+
optional(integer(QUANTITY_ARG.toText())))
197+
.executor(HasGivenFromInventoryCommand())
198+
.build()
199+
182200
val executeParsedCommandSpec = CommandSpec.builder()
183201
.permission("$ROOT_PERMISSION.parsed")
184202
.arguments(
@@ -213,10 +231,12 @@ class CommandUtils @Inject constructor(
213231
.child(hasChanceCommandSpec, "chance")
214232
.child(CommandSpec.builder()
215233
.child(hasInHandCommandSpec, "hand")
234+
.child(hasInInventoryCommandSpec, "inventory")
216235
.build(), "in")
217236
.child(CommandSpec.builder()
218237
.child(CommandSpec.builder()
219238
.child(hasGivenFromHandCommandSpec, "hand")
239+
.child(hasGivenFromInventoryCommandSpec, "inventory")
220240
.build(), "from")
221241
.build(), "given")
222242
.build(), "has")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package de.randombyte.commandutils.conditions
2+
3+
import de.randombyte.commandutils.CommandUtils
4+
import de.randombyte.commandutils.execute.getPlayer
5+
import de.randombyte.commandutils.execute.isTruthy
6+
import de.randombyte.kosp.extensions.tryAsByteItem
7+
import org.spongepowered.api.command.CommandResult
8+
import org.spongepowered.api.command.CommandSource
9+
import org.spongepowered.api.command.args.CommandContext
10+
import org.spongepowered.api.item.inventory.Inventory
11+
import org.spongepowered.api.item.inventory.query.QueryOperationTypes
12+
13+
class HasGivenFromInventoryCommand : HasInInventoryCommand() {
14+
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
15+
val hasInInventoryCommandResult = super.execute(src, args)
16+
if (!hasInInventoryCommandResult.isTruthy()) return CommandResult.empty()
17+
18+
// safe call due to 'isTruthy()'
19+
val neededQuantity = hasInInventoryCommandResult.successCount.get()
20+
val requiredItem = args.getOne<String>(CommandUtils.ITEM_ARG).get().tryAsByteItem().createStack()
21+
22+
val player = args.getPlayer()
23+
player.inventory
24+
.query<Inventory>(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(requiredItem))
25+
.poll(neededQuantity)
26+
27+
return CommandResult.successCount(neededQuantity)
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.item.inventory.Inventory
14+
import org.spongepowered.api.item.inventory.query.QueryOperationTypes
15+
16+
open class HasInInventoryCommand : 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+
var requiredQuantity = args.getOne<Int>(CommandUtils.QUANTITY_ARG).orNull()
21+
22+
val requiredItem = itemString.tryAsByteItem().createStack()
23+
val foundSlots = player.inventory.query<Inventory>(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(requiredItem))
24+
25+
if (requiredQuantity == null) requiredQuantity = requiredItem.quantity
26+
if (requiredQuantity < 1) throw CommandException("'quantity' must be greater than zero!".toText())
27+
28+
if (foundSlots.totalItems() < requiredQuantity) return CommandResult.empty()
29+
30+
return CommandResult.successCount(requiredQuantity)
31+
}
32+
}

0 commit comments

Comments
 (0)