-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added HasInHandCommand, HasGivenFromHandCommand and HasPermissionCommand
- Loading branch information
1 parent
e980710
commit e004e53
Showing
21 changed files
with
227 additions
and
91 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
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
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/de/randombyte/commandutils/conditions/HasGivenFromHandCommand.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,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) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/de/randombyte/commandutils/conditions/HasInHandCommand.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,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() | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/de/randombyte/commandutils/conditions/HasPermissionCommand.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,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() | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/de/randombyte/commandutils/conditions/utils.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 de.randombyte.commandutils.conditions | ||
|
||
import org.spongepowered.api.command.CommandResult | ||
|
||
fun Boolean.toCommandResult() = if (this) CommandResult.success() else CommandResult.empty() |
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
Oops, something went wrong.