Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import org.spongepowered.api.status.StatusClient;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.util.command.CommandResult;
import org.spongepowered.api.util.command.CommandSource;
import org.spongepowered.api.util.event.factory.ClassGeneratorProvider;
import org.spongepowered.api.util.event.factory.EventFactory;
Expand Down Expand Up @@ -904,14 +905,16 @@ public static ProjectileLaunchEvent createProjectileLaunch(Game game, Cause caus
* @param arguments The arguments provided
* @param source The source of the command
* @param command The command name
* @param result The result of the command, or null
* @return A new instance of the event
*/
public static CommandEvent createCommand(Game game, String arguments, CommandSource source, String command) {
public static CommandEvent createCommand(Game game, String arguments, CommandSource source, String command, CommandResult result) {
Map<String, Object> values = Maps.newHashMap();
values.put("game", game);
values.put("arguments", arguments);
values.put("source", source);
values.put("command", command);
values.put("result", Optional.fromNullable(result));
return createEvent(CommandEvent.class, values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.GameEvent;
import org.spongepowered.api.util.command.CommandResult;
import org.spongepowered.api.util.command.CommandSource;
import com.google.common.base.Optional;

import javax.annotation.Nullable;

/**
* Fired when a command has been used and needs to be processed.
Expand Down Expand Up @@ -61,4 +65,19 @@ public interface CommandEvent extends GameEvent, Cancellable {
*/
String getArguments();

/**
* The result of the command. This is only available after the execution
* of the command.
*
* @return The result of the command, if present
*/
Optional<CommandResult> getResult();

/**
* Sets the result of the command.
*
* @param result The result of the command, or null
*/
void setResult(@Nullable CommandResult result);
Copy link
Contributor Author

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)?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.spongepowered.api.util.command.CommandCallable;
import org.spongepowered.api.util.command.CommandException;
import org.spongepowered.api.util.command.CommandMapping;
import org.spongepowered.api.util.command.CommandResult;
import org.spongepowered.api.util.command.CommandSource;
import org.spongepowered.api.util.command.dispatcher.SimpleDispatcher;

Expand Down Expand Up @@ -95,7 +96,11 @@ public SimpleCommandService(PluginManager pluginManager) {
@Subscribe(order = Order.LAST)
public void onCommandEvent(final CommandEvent event) {
try {
if (call(event.getSource(), event.getCommand() + " " + event.getArguments(), Collections.<String>emptyList())) {
CommandResult result = call(event.getSource(), event.getCommand() + " " + event.getArguments(), Collections.<String>emptyList());

event.setResult(result);

if (result.wasProcessed()) {
event.setCancelled(true);
}
} catch (CommandException e) {
Expand Down Expand Up @@ -227,7 +232,7 @@ public boolean containsMapping(CommandMapping mapping) {
}

@Override
public boolean call(CommandSource source, String arguments, List<String> parents) throws CommandException {
public CommandResult call(CommandSource source, String arguments, List<String> parents) throws CommandException {
return this.dispatcher.call(source, arguments, parents);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public interface CommandCallable extends CommandCompleter {
* @param arguments The raw arguments for this command
* @param parents A stack of parent commands, where the first entry is
* the root command
* @return Whether a command was processed
* @return The result of the command
* @throws CommandException Thrown on a command error
*/
boolean call(CommandSource source, String arguments, List<String> parents) throws CommandException;
CommandResult call(CommandSource source, String arguments, List<String> parents) throws CommandException;

/**
* Test whether this command can probably be executed by the given source.
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/org/spongepowered/api/util/command/CommandResult.java
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for optionals and not simply defaulting to zero?

Copy link
Contributor

Choose a reason for hiding this comment

The 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).

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But an motd command being executed would succeed once.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
}
Loading