-
-
Notifications
You must be signed in to change notification settings - Fork 340
[WIP] Add CommandResult #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8d18385
a8483ba
35073ad
7dd19b4
a5e3102
01c87a3
37e2ede
f7abac6
11054f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered.org <http://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.api.util.command; | ||
|
|
||
| import com.google.common.base.Optional; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Represents the result of a command in Sponge. | ||
| */ | ||
| public class CommandResult { | ||
|
|
||
| private final boolean processed; | ||
| private final Optional<Integer> successCount; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for optionals and not simply defaulting to zero?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Deamon5550 There is a difference between a success count of 0 (no successes) and absent (not applicable).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't having a not-applicable successCount imply that the Command failed to succeed?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gabizou Good for commands where it wouldn't make sense. /motd, for instance. Doesn't make sense for it to succeed or not.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But an motd command being executed would succeed once.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gabizou Yes, you could say it always succeeds, but how is that useful information? There'd be no use in knowing the success count, so why store it?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AlphaModder Command blocks? |
||
| private final Optional<Integer> affectedBlocks; | ||
| private final Optional<Integer> affectedEntities; | ||
| private final Optional<Integer> affectedItems; | ||
| private final Optional<Integer> queryResult; | ||
| private final Map<String, Object> resultInfo; | ||
|
|
||
| /** | ||
| * Constructs a new command result. | ||
| * | ||
| * @param processed True if the command was processed | ||
| * @param successCount The success count | ||
| * @param affectedBlocks The number of affected blocks | ||
| * @param affectedEntities The number of affected entities | ||
| * @param affectedItems The number of affected items | ||
| * @param queryResult The query result | ||
| * @param resultInfo A map storing additional information | ||
| */ | ||
| CommandResult(boolean processed, @Nullable Integer successCount, @Nullable Integer affectedBlocks, @Nullable Integer affectedEntities, | ||
| @Nullable Integer affectedItems, @Nullable Integer queryResult, @Nullable Map<String, Object> resultInfo) { | ||
| this.processed = processed; | ||
| this.successCount = Optional.fromNullable(successCount); | ||
| this.affectedBlocks = Optional.fromNullable(affectedBlocks); | ||
| this.affectedEntities = Optional.fromNullable(affectedEntities); | ||
| this.affectedItems = Optional.fromNullable(affectedItems); | ||
| this.queryResult = Optional.fromNullable(queryResult); | ||
| this.resultInfo = resultInfo != null ? Collections.unmodifiableMap(resultInfo) : Collections.<String, Object>emptyMap(); | ||
| } | ||
|
|
||
| /** | ||
| * Tests if the command was processed. | ||
| * | ||
| * @return True if the command was processed | ||
| */ | ||
| public boolean wasProcessed() { | ||
| return this.processed; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the success count of the command. | ||
| * | ||
| * @return The success count of the command | ||
| */ | ||
| public Optional<Integer> getSuccessCount() { | ||
| return this.successCount; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the number of blocks affected by the command. | ||
| * | ||
| * @return The number of blocks affected by the command, if such a count | ||
| * exists | ||
| */ | ||
| public Optional<Integer> getAffectedBlocks() { | ||
| return this.affectedBlocks; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the number of entities affected by the command. | ||
| * | ||
| * @return The number of entities affected by the command, if such a count | ||
| * exists | ||
| */ | ||
| public Optional<Integer> getAffectedEntities() { | ||
| return this.affectedEntities; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the number of items affected by the command. | ||
| * | ||
| * @return The number of items affected by the command, if such a count | ||
| * exists | ||
| */ | ||
| public Optional<Integer> getAffectedItems() { | ||
| return this.affectedItems; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the query result of the command, e.g. the time of the day, | ||
| * an amount of money or a player's amount of XP. | ||
| * | ||
| * @return The query result of the command, if one exists | ||
| */ | ||
| public Optional<Integer> getQueryResult() { | ||
| return this.queryResult; | ||
| } | ||
|
|
||
| /** | ||
| * Gets a Map used by the command to store information about what it did. | ||
| * | ||
| * @return A Map used by the command to store information about what it did. | ||
| */ | ||
| public Map<String, Object> getResultInfo() { | ||
| return this.resultInfo; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered.org <http://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.api.util.command; | ||
|
|
||
| import com.google.common.collect.Maps; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A builder for {@link CommandResult}s. | ||
| */ | ||
| public class CommandResultBuilder { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could probably also work as an inner class of CommandResult. That's what people usually do for not-huge builders. |
||
|
|
||
| private boolean processed; | ||
| private Integer successCount; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as above about not having defaults |
||
| private Integer affectedBlocks; | ||
| private Integer affectedEntities; | ||
| private Integer affectedItems; | ||
| private Integer queryResult; | ||
| private Map<String, Object> resultInfo; | ||
|
|
||
| /** | ||
| * Sets if the command has been processed. | ||
| * | ||
| * @param processed If the command has been processed | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder processed(boolean processed) { | ||
| this.processed = processed; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets if the command has been processed. | ||
| * | ||
| * @param successCount If the command has been processed | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder successCount(@Nullable Integer successCount) { | ||
| this.successCount = successCount; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the amount of blocks affected by the command. | ||
| * | ||
| * @param affectedBlocks The amount of blocks affected by the command | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder affectedBlocks(@Nullable Integer affectedBlocks) { | ||
| this.affectedBlocks = affectedBlocks; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the amount of entities affected by the command. | ||
| * | ||
| * @param affectedEntities The amount of entities affected by the command | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder affectedEntities(@Nullable Integer affectedEntities) { | ||
| this.affectedEntities = affectedEntities; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the amount of items affected by the command. | ||
| * | ||
| * @param affectedItems The amount of items affected by the command | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder affectedItems(@Nullable Integer affectedItems) { | ||
| this.affectedItems = affectedItems; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the query result of the command, e.g. the time of the day, | ||
| * an amount of money or a player's amount of XP. | ||
| * | ||
| * @param queryResult The query result of the command | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder queryResult(@Nullable Integer queryResult) { | ||
| this.queryResult = queryResult; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a key-value pair to the result info map. | ||
| * | ||
| * @param key The key. | ||
| * @param value The value. | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder resultInfo(String key, Object value) { | ||
| if (this.resultInfo == null) { | ||
| this.resultInfo = Maps.<String, Object>newHashMap(); | ||
| } | ||
| this.resultInfo.put(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a set of key-value pairs result info map. | ||
| * | ||
| * @param resultInfo A map of several key-value pairs | ||
| * @return This builder, for chaining | ||
| */ | ||
| public CommandResultBuilder resultInfo(@Nullable Map<String, Object> resultInfo) { | ||
| this.resultInfo = resultInfo; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the {@link CommandResult}. | ||
| * | ||
| * @return A CommandResult with the specified settings | ||
| */ | ||
| public CommandResult build() { | ||
| return new CommandResult(this.processed, this.successCount, this.affectedBlocks, this.affectedEntities, this.affectedItems, this.queryResult, | ||
| this.resultInfo); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now there is a problem with this setter (tests fail).
Should I change it to
setResult(Optional<CommandResult> result)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Afaik the optional is only for return types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but the event system does not work if the setter does not use the same type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You haven't added the key 'result' to the
SpongeEventFactory