Skip to content

Commit

Permalink
Made combine more consistent for all numeric types
Browse files Browse the repository at this point in the history
For all boxed primitive numeric types, combine now adds them instead of just integers
  • Loading branch information
SerenaLynas committed Dec 4, 2022
1 parent dae8a48 commit 03e0940
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/main/java/com/mojang/brigadier/results/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,48 @@ public interface CommandResult {
* @return The combined result
*/
default Object combine(Object other) {
ListCommandResult list = new ListCommandResult();
list.combine(this);
list.combine(other);
return list;
return ListCommandResult.from(this, other);
}

/**
* <p>Combine any objects, even if they are not Command Results.
* Target is the object which will be mutated to contain
* the combined target and source.
*
* <p>If either of the supplied objects are EmptyCommandResult, the
* non-empty result will be returned.
*
* <p>If the target implements CommandResult, the `combine` method
* will be called on the target: `target.combine(source)`
*
* <p>If the target and source are the same type of boxed primitive
* number (ie Integer, Long, Double, etc.), they will be added together.
*
* <p>Otherwise, a new ListCommandResult containing both target and source
* will be returned.
*
* @param target the object which the source will be combined into
* @param source the object to combine into the target
*/
static Object combine(Object target, Object source) {
if (target instanceof CommandResult) {
return ((CommandResult)target).combine(source);
} else if (target instanceof Integer && source instanceof Integer) {
// Backwards compatability
return (Integer)target + (Integer)source;
} else if (source instanceof EmptyCommandResult) {
return target;
} else if (target instanceof Byte && source instanceof Byte) {
return (byte) target + (byte) source;
} else if (target instanceof Short && source instanceof Short) {
return (short) target + (short) source;
} else if (target instanceof Integer && source instanceof Integer) {
return (int) target + (int) source;
} else if (target instanceof Long && source instanceof Long) {
return (long) source + (long) source;
} else if (source instanceof Float && source instanceof Float) {
return (float) source + (float) source;
} else if (source instanceof Double && target instanceof Double) {
return (double) source + (double) target;
} else {
return source;
return ListCommandResult.from(target, source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ public ListCommandResult combine(Object other) {

return this;
}

public static ListCommandResult from(Object a, Object b) {
ListCommandResult list = new ListCommandResult();
list = list.combine(a);
list = list.combine(b);
return list;
}
}

0 comments on commit 03e0940

Please sign in to comment.