Skip to content

Commit 03e0940

Browse files
committed
Made combine more consistent for all numeric types
For all boxed primitive numeric types, combine now adds them instead of just integers
1 parent dae8a48 commit 03e0940

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

src/main/java/com/mojang/brigadier/results/CommandResult.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,48 @@ public interface CommandResult {
1818
* @return The combined result
1919
*/
2020
default Object combine(Object other) {
21-
ListCommandResult list = new ListCommandResult();
22-
list.combine(this);
23-
list.combine(other);
24-
return list;
21+
return ListCommandResult.from(this, other);
2522
}
2623

24+
/**
25+
* <p>Combine any objects, even if they are not Command Results.
26+
* Target is the object which will be mutated to contain
27+
* the combined target and source.
28+
*
29+
* <p>If either of the supplied objects are EmptyCommandResult, the
30+
* non-empty result will be returned.
31+
*
32+
* <p>If the target implements CommandResult, the `combine` method
33+
* will be called on the target: `target.combine(source)`
34+
*
35+
* <p>If the target and source are the same type of boxed primitive
36+
* number (ie Integer, Long, Double, etc.), they will be added together.
37+
*
38+
* <p>Otherwise, a new ListCommandResult containing both target and source
39+
* will be returned.
40+
*
41+
* @param target the object which the source will be combined into
42+
* @param source the object to combine into the target
43+
*/
2744
static Object combine(Object target, Object source) {
2845
if (target instanceof CommandResult) {
2946
return ((CommandResult)target).combine(source);
30-
} else if (target instanceof Integer && source instanceof Integer) {
31-
// Backwards compatability
32-
return (Integer)target + (Integer)source;
3347
} else if (source instanceof EmptyCommandResult) {
3448
return target;
49+
} else if (target instanceof Byte && source instanceof Byte) {
50+
return (byte) target + (byte) source;
51+
} else if (target instanceof Short && source instanceof Short) {
52+
return (short) target + (short) source;
53+
} else if (target instanceof Integer && source instanceof Integer) {
54+
return (int) target + (int) source;
55+
} else if (target instanceof Long && source instanceof Long) {
56+
return (long) source + (long) source;
57+
} else if (source instanceof Float && source instanceof Float) {
58+
return (float) source + (float) source;
59+
} else if (source instanceof Double && target instanceof Double) {
60+
return (double) source + (double) target;
3561
} else {
36-
return source;
62+
return ListCommandResult.from(target, source);
3763
}
3864
}
3965
}

src/main/java/com/mojang/brigadier/results/ListCommandResult.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ public ListCommandResult combine(Object other) {
2121

2222
return this;
2323
}
24+
25+
public static ListCommandResult from(Object a, Object b) {
26+
ListCommandResult list = new ListCommandResult();
27+
list = list.combine(a);
28+
list = list.combine(b);
29+
return list;
30+
}
2431
}

0 commit comments

Comments
 (0)