From f2f7a9cf58fb9b3796dcb4da337ad961ba672488 Mon Sep 17 00:00:00 2001
From: Gabriel Harris-Rouquette
Date: Tue, 15 Dec 2015 15:30:41 -0800
Subject: [PATCH] Enforce Cause to never be empty, removed now unnecessary
EmptyCause and PresentCause as Cause itself can remain it's own
implementation.
Signed-off-by: Gabriel Harris-Rouquette
---
.../translator/ConfigurateTranslator.java | 1 -
.../org/spongepowered/api/entity/Entity.java | 2 +-
.../api/event/SpongeEventFactory.java | 4 +-
.../spongepowered/api/event/cause/Cause.java | 687 +++++++-----------
.../api/event/world/SaveWorldEvent.java | 3 +-
.../api/item/inventory/EmptyInventory.java | 4 +-
.../InventoryTransactionResult.java | 16 +-
.../spongepowered/api/event/CauseTest.java | 39 +-
.../api/event/SpongeAITaskEventTest.java | 10 +-
.../SpongeAbstractDamageEntityEventTest.java | 14 +-
.../api/event/SpongeAbstractEventTest.java | 6 +-
.../SpongeAbstractHealEntityEventTest.java | 14 +-
.../api/event/SpongeEventFactoryTest.java | 7 +
13 files changed, 295 insertions(+), 512 deletions(-)
diff --git a/src/main/java/org/spongepowered/api/data/translator/ConfigurateTranslator.java b/src/main/java/org/spongepowered/api/data/translator/ConfigurateTranslator.java
index 02bae185286..74347734245 100644
--- a/src/main/java/org/spongepowered/api/data/translator/ConfigurateTranslator.java
+++ b/src/main/java/org/spongepowered/api/data/translator/ConfigurateTranslator.java
@@ -84,7 +84,6 @@ private static void translateMapOrList(ConfigurationNode node, DataView containe
} else if (value != null) {
container.set(of(node.getKey().toString()), value);
}
-
}
@Override
diff --git a/src/main/java/org/spongepowered/api/entity/Entity.java b/src/main/java/org/spongepowered/api/entity/Entity.java
index b4eb9c02f2e..a8ac627684b 100644
--- a/src/main/java/org/spongepowered/api/entity/Entity.java
+++ b/src/main/java/org/spongepowered/api/entity/Entity.java
@@ -303,7 +303,7 @@ public interface Entity extends Identifiable, DataHolder, DataSerializable {
* @return True if damaging the entity was successful
*/
default boolean damage(double damage, DamageSource damageSource) {
- return damage(damage, damageSource, Cause.of());
+ return damage(damage, damageSource, Cause.of(NamedCause.source(damageSource)));
}
/**
diff --git a/src/main/java/org/spongepowered/api/event/SpongeEventFactory.java b/src/main/java/org/spongepowered/api/event/SpongeEventFactory.java
index d1966f0e9c6..68d6b024891 100644
--- a/src/main/java/org/spongepowered/api/event/SpongeEventFactory.java
+++ b/src/main/java/org/spongepowered/api/event/SpongeEventFactory.java
@@ -4459,14 +4459,12 @@ public static LoadWorldEvent createLoadWorldEvent(Cause cause, World targetWorld
* Creates a new instance of
* {@link org.spongepowered.api.event.world.SaveWorldEvent}.
*
- * @param game The game
* @param cause The cause
* @param targetWorld The target world
* @return A new save world event
*/
- public static SaveWorldEvent createSaveWorldEvent(Game game, Cause cause, World targetWorld) {
+ public static SaveWorldEvent createSaveWorldEvent(Cause cause, World targetWorld) {
Map values = Maps.newHashMap();
- values.put("game", game);
values.put("cause", cause);
values.put("targetWorld", targetWorld);
return SpongeEventFactoryUtils.createEventImpl(SaveWorldEvent.class, values);
diff --git a/src/main/java/org/spongepowered/api/event/cause/Cause.java b/src/main/java/org/spongepowered/api/event/cause/Cause.java
index 9fdfd450fe8..c8e8c5f9a00 100644
--- a/src/main/java/org/spongepowered/api/event/cause/Cause.java
+++ b/src/main/java/org/spongepowered/api/event/cause/Cause.java
@@ -28,7 +28,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.commons.lang3.Validate.noNullElements;
-import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -64,18 +63,7 @@
* may not be attempted.
*/
@SuppressWarnings("unchecked")
-public abstract class Cause {
-
- private static final Cause EMPTY = new EmptyCause();
-
- /**
- * Gets an empty {@link Cause}.
- *
- * @return The empty cause
- */
- public static Cause of() {
- return EMPTY;
- }
+public final class Cause {
/**
* Creates a new {@link Cause} of the provided {@link Object}s. Note that
@@ -92,39 +80,71 @@ public static Cause of() {
* @param objects The objects being the cause
* @return The new cause
*/
- public static Cause of(Object... objects) {
+ public static Cause of(Object object, Object... objects) {
+ checkArgument(object != null, "The source object cannot be null!");
checkNotNull(objects, "The objects cannot be null!");
- if (objects.length == 0) {
- return EMPTY;
+ List list = new ArrayList<>();
+ if (object instanceof Object[]) {
+ for (Object arrayObj : ((Object[]) object)) {
+ list.add(checkNotNull(arrayObj));
+ }
+ } else {
+ list.add(object);
}
- noNullElements(objects, "No elements in a cause can be null!");
- return new PresentCause(objects);
+ for (Object context : objects) {
+ list.add(checkNotNull(context));
+ }
+ return new Cause(list.toArray());
}
- public static Cause ofNullable(@Nullable Object... objects) {
- if (objects == null || objects.length == 0) {
- return EMPTY;
- } else {
- List list = new ArrayList<>();
- for (Object object : objects) {
- if (object != null) {
- list.add(object);
- }
+ public static Cause ofNullable(@Nullable Object object, @Nullable Object... objects) {
+ checkArgument(object != null || (objects != null && objects.length != 0), "There must be at least one object in a cause!");
+ List list = new ArrayList<>();
+ if (object instanceof Object[]) {
+ for (Object arrayObj : ((Object[]) object)) {
+ list.add(checkNotNull(arrayObj));
}
- return new PresentCause(list.toArray());
+ } else if (object != null) {
+ list.add(object);
}
+ for (Object cause : objects) {
+ if (object != null) {
+ list.add(object);
+ }
+ }
+ if (list.isEmpty()) {
+ throw new IllegalArgumentException("There must be at least one object within a cause!");
+ }
+ return new Cause(list.toArray());
}
- Cause() {}
+ private final Object[] cause;
+ private final String[] names;
+
+ // lazy load
+ @Nullable private Map namedObjectMap;
+
+ private Cause(Object[] causes) {
+ final List list = new ArrayList<>(causes.length);
+ final List names = new ArrayList<>(causes.length);
+ for (Object aCause : causes) {
+ checkNotNull(aCause, "Null cause element!");
+ if (aCause instanceof NamedCause) {
+ Object object = ((NamedCause) aCause).getCauseObject();
+ checkArgument(!names.contains(((NamedCause) aCause).getName()), "Names need to be unique!"
+ + " There is already a named cause of: "
+ + ((NamedCause) aCause).getName());
+ list.add(object);
+ names.add(((NamedCause) aCause).getName());
+ } else {
+ list.add(aCause);
+ names.add("unknown" + list.size() + aCause.getClass().getName());
+ }
+ }
- /**
- * Gets whether this {@link Cause} is empty of any causes or not. An empty
- * cause may mean the {@link Cause} is not originating from any vanilla
- * interactions, or it may mean the cause is simply not known.
- *
- * @return True if this cause is empty, false otherwise
- */
- public abstract boolean isEmpty();
+ this.cause = list.toArray();
+ this.names = names.toArray(new String[names.size()]);
+ }
/**
* Gets the root {@link Object} of this cause. The root can be anything,
@@ -133,7 +153,9 @@ public static Cause ofNullable(@Nullable Object... objects) {
*
* @return The root object cause for this cause
*/
- public abstract Optional> root();
+ public Object root() {
+ return this.cause[0];
+ }
/**
* Gets the first T
object of this {@link Cause}, if
@@ -143,7 +165,14 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param The type of object being queried for
* @return The first element of the type, if available
*/
- public abstract Optional first(Class target);
+ public Optional first(Class target) {
+ for (Object aCause : this.cause) {
+ if (target.isInstance(aCause)) {
+ return Optional.of((T) aCause);
+ }
+ }
+ return Optional.empty();
+ }
/**
* Gets the last object instance of the {@link Class} of type
@@ -153,7 +182,14 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param The type of object being queried for
* @return The last element of the type, if available
*/
- public abstract Optional last(Class target);
+ public Optional last(Class target) {
+ for (int i = this.cause.length - 1; i >= 0; i--) {
+ if (target.isInstance(this.cause[i])) {
+ return Optional.of((T) this.cause[i]);
+ }
+ }
+ return Optional.empty();
+ }
/**
* Gets the object associated with the provided name. Note that
@@ -163,7 +199,15 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param The type of the object expected
* @return The object, if the type is correct and the name was associated
*/
- public abstract Optional get(String named);
+ public Optional get(String named) {
+ checkArgument(named != null, "The name cannot be null!");
+ for (int i = 0; i < this.names.length; i++) {
+ if (this.names[i].equalsIgnoreCase(named)) {
+ return getCauseAtIndex(i);
+ }
+ }
+ return Optional.empty();
+ }
/**
* Gets the object immediately before the object that is an instance of
@@ -172,7 +216,18 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param clazz The class of the object
* @return The object
*/
- public abstract Optional> before(Class> clazz);
+ public Optional> before(Class> clazz) {
+ checkArgument(clazz != null, "The provided class cannot be null!");
+ if (this.cause.length == 1) {
+ return Optional.empty();
+ }
+ for (int i = 0; i < this.cause.length; i++) {
+ if (clazz.isInstance(this.cause[i]) && i > 0) {
+ return Optional.of(this.cause[i - 1]);
+ }
+ }
+ return Optional.empty();
+ }
/**
* Gets the object immediate before the named object cause. The
@@ -181,7 +236,23 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param named The name associated with the cause object
* @return The object, if available
*/
- public abstract Optional> before(String named);
+ public Optional> before(String named) {
+ checkArgument(named != null, "The name cannot be null!");
+ if (this.cause.length == 1) {
+ return Optional.empty();
+ }
+ for (int i = 0; i < this.names.length; i++) {
+ if (this.names[i].equalsIgnoreCase(named)) {
+ try {
+ final Object object = this.cause[i - 1];
+ return Optional.of(object);
+ } catch (Exception e) {
+ return Optional.empty();
+ }
+ }
+ }
+ return Optional.empty();
+ }
/**
* Gets the object immediately after the object that is an instance of
@@ -190,7 +261,18 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param clazz The class to type check
* @return The object after, if available
*/
- public abstract Optional> after(Class> clazz);
+ public Optional> after(Class> clazz) {
+ checkArgument(clazz != null, "The provided class cannot be null!");
+ if (this.cause.length == 1) {
+ return Optional.empty();
+ }
+ for (int i = 0; i < this.cause.length; i++) {
+ if (clazz.isInstance(this.cause[i]) && i + 1 < this.cause.length) {
+ return Optional.of(this.cause[i + 1]);
+ }
+ }
+ return Optional.empty();
+ }
/**
* Gets the object immediately after the named object cause.
@@ -198,14 +280,39 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param named The name associated with the cause object
* @return The object after, if available
*/
- public abstract Optional> after(String named);
+ public Optional> after(String named) {
+ checkArgument(named != null, "The name cannot be null!");
+ if (this.cause.length == 1) {
+ return Optional.empty();
+ }
+ for (int i = 0; i < this.names.length; i++) {
+ if (this.names[i].equalsIgnoreCase(named) && i + 1 < this.cause.length) {
+ try {
+ final Object object = this.cause[i + 1];
+ return Optional.of(object);
+ } catch (Exception e) {
+ return Optional.empty();
+ }
+ }
+ }
+ return Optional.empty();
+ }
/**
* Returns whether the target class matches any object of this {@link Cause}.
* @param target The class of the target type
* @return True if found, false otherwise
*/
- public abstract boolean any(Class> target);
+ public boolean containsType(Class> target) {
+ checkArgument(target != null, "The provided class cannot be null!");
+ for (Object aCause : this.cause) {
+ if (target.isInstance(aCause)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
/**
* Checks if this cause contains of any of the provided {@link Object}. This
@@ -215,7 +322,14 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param object The object to check if it is contained
* @return True if the object is contained within this cause
*/
- public abstract boolean contains(Object object);
+ public boolean contains(Object object) {
+ for (Object aCause : this.cause) {
+ if (aCause.equals(object)) {
+ return true;
+ }
+ }
+ return false;
+ }
/**
* Returns whether there are any objects associated with the provided name.
@@ -223,7 +337,15 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param named The name associated with a cause object
* @return True if found, false otherwise
*/
- public abstract boolean any(String named);
+ public boolean containsNamed(String named) {
+ checkArgument(named != null, "The name cannot be null!");
+ for (String name : this.names) {
+ if (name.equalsIgnoreCase(named)) {
+ return true;
+ }
+ }
+ return false;
+ }
/**
* Gets an {@link ImmutableList} of all objects that are instances of the
@@ -233,7 +355,15 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param target The class of the target type
* @return An immutable list of the objects queried
*/
- public abstract List allOf(Class target);
+ public List allOf(Class target) {
+ ImmutableList.Builder builder = ImmutableList.builder();
+ for (Object aCause : this.cause) {
+ if (target.isInstance(aCause)) {
+ builder.add((T) aCause);
+ }
+ }
+ return builder.build();
+ }
/**
* Gets an immutable {@link List} with all object causes that are not
@@ -242,23 +372,44 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param ignoredClass The class of object types to ignore
* @return The list of objects not an instance of the provided class
*/
- public abstract List noneOf(Class> ignoredClass);
+ public List noneOf(Class> ignoredClass) {
+ ImmutableList.Builder builder = ImmutableList.builder();
+ for (Object cause : this.cause) {
+ if (!ignoredClass.isInstance(cause)) {
+ builder.add(cause);
+ }
+ }
+ return builder.build();
+ }
/**
* Gets an {@link List} of all causes within this {@link Cause}.
*
* @return An immutable list of all the causes
*/
- public abstract List all();
+ public List all() {
+ return ImmutableList.copyOf(this.cause);
+ }
/**
* Creates a new {@link Cause} where the objects are added at the end of
* the cause array of objects.
*
- * @param additional The additional objects to add
+ * @param additional The additional object to add
+ * @param additionals The remaining objects to add
* @return The new cause
*/
- public abstract Cause with(Object... additional);
+ public Cause with(Object additional, Object... additionals) {
+ checkArgument(additional != null, "No null arguments allowed!");
+ noNullElements(additionals, "No null objects allowed!");
+ List list = new ArrayList<>();
+ list.add(additional);
+ for (Object object : additionals) {
+ checkArgument(object != null, "Cannot add null objects!");
+ list.add(object);
+ }
+ return with(list);
+ }
/**
* Creates a new {@link Cause} where the objects are added at the end of
@@ -267,7 +418,21 @@ public static Cause ofNullable(@Nullable Object... objects) {
* @param iterable The additional objects
* @return The new cause
*/
- public abstract Cause with(Iterable> iterable);
+ public Cause with(Iterable> iterable) {
+ List list = new ArrayList<>();
+ for (int i = 0; i < this.cause.length; i++) {
+ list.add(NamedCause.of(this.names[i], this.cause[i]));
+ }
+ for (Object o : iterable) {
+ checkArgument(o != null, "Cannot add null causes");
+ if (o instanceof NamedCause) {
+ list.add(o);
+ } else {
+ list.add(NamedCause.of("unknown" + (list.size() + 1) + o.getClass().getSimpleName(), o));
+ }
+ }
+ return of(list.toArray());
+ }
/**
* Gets an immutable {@link Map} of the named object causes that can be
@@ -277,402 +442,48 @@ public static Cause ofNullable(@Nullable Object... objects) {
*
* @return An immutable map of the names of cause objects to the objects
*/
- public abstract Map getNamedCauses();
-
- /**
- * Returns {@code true} if {@code object} is a {@code Cause} instance, and
- * either the contained references are {@linkplain Object#equals equal} to
- * each other or both are absent.
- */
- @Override
- public abstract boolean equals(@Nullable Object object);
-
- /**
- * Returns a hash code for this instance.
- */
- @Override
- public abstract int hashCode();
-
- private static final class PresentCause extends Cause {
- private final Object[] cause;
- private final String[] names;
-
- // lazy load
- @Nullable private Map namedObjectMap;
-
- PresentCause(Object... causes) {
- final List list = new ArrayList<>(causes.length);
- final List names = new ArrayList<>(causes.length);
- for (Object aCause : causes) {
- checkNotNull(aCause, "Null cause element!");
- if (aCause instanceof NamedCause) {
- Object object = ((NamedCause) aCause).getCauseObject();
- checkArgument(!names.contains(((NamedCause) aCause).getName()), "Names need to be unique!"
- + " There is already a named cause of: "
- + ((NamedCause) aCause).getName());
- list.add(object);
- names.add(((NamedCause) aCause).getName());
- } else {
- list.add(aCause);
- names.add("unknown" + list.size() + aCause.getClass().getName());
- }
- }
-
- this.cause = list.toArray();
- this.names = names.toArray(new String[names.size()]);
- }
-
- @Override
- public boolean isEmpty() {
- return false;
- }
-
- @Override
- public Optional> root() {
- return Optional.of(this.cause[0]);
- }
-
- @Override
- public Optional first(Class target) {
- for (Object aCause : this.cause) {
- if (target.isInstance(aCause)) {
- return Optional.of((T) aCause);
- }
- }
- return Optional.empty();
- }
-
- @Override
- public Optional get(String named) {
- checkArgument(named != null, "The name cannot be null!");
+ public Map getNamedCauses() {
+ if (this.namedObjectMap == null) {
+ final ImmutableMap.Builder builder = ImmutableMap.builder();
for (int i = 0; i < this.names.length; i++) {
- if (this.names[i].equalsIgnoreCase(named)) {
- return getCauseAtIndex(i);
- }
- }
- return Optional.empty();
- }
-
- @Override
- public List allOf(Class target) {
- ImmutableList.Builder builder = ImmutableList.builder();
- for (Object aCause : this.cause) {
- if (target.isInstance(aCause)) {
- builder.add((T) aCause);
- }
- }
- return builder.build();
- }
-
- @Override
- public List noneOf(Class> ignoredClass) {
- ImmutableList.Builder builder = ImmutableList.builder();
- for (Object cause : this.cause) {
- if (!ignoredClass.isInstance(cause)) {
- builder.add(cause);
- }
- }
- return builder.build();
- }
-
- @Override
- public Optional last(Class target) {
- for (int i = this.cause.length - 1; i >= 0; i--) {
- if (target.isInstance(this.cause[i])) {
- return Optional.of((T) this.cause[i]);
- }
- }
- return Optional.empty();
- }
-
- private Optional getCauseAtIndex(int index) {
- try {
- final Object object = this.cause[index];
- return Optional.of((T) object);
- } catch (Exception e) {
- return Optional.empty();
+ builder.put(this.names[i], this.cause[i]);
}
+ this.namedObjectMap = builder.build();
}
-
- @Override
- public Optional> before(Class> clazz) {
- checkArgument(clazz != null, "The provided class cannot be null!");
- if (this.cause.length == 1) {
- return Optional.empty();
- }
- for (int i = 0; i < this.cause.length; i++) {
- if (clazz.isInstance(this.cause[i]) && i > 0) {
- return Optional.of(this.cause[i - 1]);
- }
- }
- return Optional.empty();
- }
-
- @Override
- public Optional> before(String named) {
- checkArgument(named != null, "The name cannot be null!");
- if (this.cause.length == 1) {
- return Optional.empty();
- }
- for (int i = 0; i < this.names.length; i++) {
- if (this.names[i].equalsIgnoreCase(named)) {
- try {
- final Object object = this.cause[i - 1];
- return Optional.of(object);
- } catch (Exception e) {
- return Optional.empty();
- }
- }
- }
- return Optional.empty();
- }
-
- @Override
- public Optional> after(Class> clazz) {
- checkArgument(clazz != null, "The provided class cannot be null!");
- if (this.cause.length == 1) {
- return Optional.empty();
- }
- for (int i = 0; i < this.cause.length; i++) {
- if (clazz.isInstance(this.cause[i]) && i + 1 < this.cause.length) {
- return Optional.of(this.cause[i + 1]);
- }
- }
- return Optional.empty();
- }
-
- @Override
- public Optional> after(String named) {
- checkArgument(named != null, "The name cannot be null!");
- if (this.cause.length == 1) {
- return Optional.empty();
- }
- for (int i = 0; i < this.names.length; i++) {
- if (this.names[i].equalsIgnoreCase(named) && i + 1 < this.cause.length) {
- try {
- final Object object = this.cause[i + 1];
- return Optional.of(object);
- } catch (Exception e) {
- return Optional.empty();
- }
- }
- }
- return Optional.empty();
- }
-
- @Override
- public boolean any(Class> target) {
- checkArgument(target != null, "The provided class cannot be null!");
- for (Object aCause : this.cause) {
- if (target.isInstance(aCause)) {
- return true;
- }
- }
-
- return false;
- }
-
- @Override
- public boolean contains(Object object) {
- for (Object aCause : this.cause) {
- if (aCause.equals(object)) {
- return true;
- }
- }
- return false;
- }
-
- @Override
- public boolean any(String named) {
- checkArgument(named != null, "The name cannot be null!");
- for (String name : this.names) {
- if (name.equalsIgnoreCase(named)) {
- return true;
- }
- }
- return false;
- }
-
- @Override
- public List all() {
- return ImmutableList.copyOf(this.cause);
- }
-
- @Override
- public Cause with(Object... additional) {
- checkArgument(additional != null, "No null arguments allowed!");
- for (Object object : additional) {
- checkArgument(object != null, "Cannot add null objects!");
- }
- return with(ImmutableList.copyOf(additional));
- }
-
- @Override
- public Cause with(Iterable> iterable) {
- List list = new ArrayList<>();
- for (int i = 0; i < this.cause.length; i++) {
- list.add(NamedCause.of(this.names[i], this.cause[i]));
- }
- for (Object o : iterable) {
- checkArgument(o != null, "Cannot add null causes");
- if (o instanceof NamedCause) {
- list.add(o);
- } else {
- list.add(NamedCause.of("unknown" + (list.size() + 1) + o.getClass().getName(), o));
- }
- }
- return of(list.toArray());
- }
-
- @Override
- public Map getNamedCauses() {
- if (this.namedObjectMap == null) {
- final ImmutableMap.Builder builder = ImmutableMap.builder();
- for (int i = 0; i < this.names.length; i++) {
- builder.put(this.names[i], this.cause[i]);
- }
- this.namedObjectMap = builder.build();
- }
- return this.namedObjectMap;
- }
-
- @Override
- public boolean equals(@Nullable Object object) {
- if (object instanceof PresentCause) {
- PresentCause cause = ((PresentCause) object);
- return Arrays.equals(this.cause, cause.cause);
- }
- return false;
- }
-
- @Override
- public int hashCode() {
- return Objects.hashCode(this.cause);
- }
-
- @Override
- public String toString() {
- String causeString = "Cause[";
- StringJoiner joiner = new StringJoiner(", ");
- for (int i = 0; i < this.cause.length; i++) {
- joiner.add("{Name=" + this.names[i] + ", Object={" + this.cause[i].toString() + "}}");
- }
- return causeString + joiner.toString() + "]";
- }
+ return this.namedObjectMap;
}
- private static final class EmptyCause extends Cause {
-
- EmptyCause() {}
-
- @Override
- public boolean isEmpty() {
- return true;
- }
-
- @Override
- public Optional> root() {
- return Optional.empty();
- }
-
- @Override
- public Optional first(Class target) {
- return Optional.empty();
- }
-
- @Override
- public Optional get(String named) {
- return Optional.empty();
- }
-
- @Override
- public Optional last(Class target) {
- return Optional.empty();
- }
-
- @Override
- public Optional> before(Class> clazz) {
- return Optional.empty();
- }
-
- @Override
- public Optional> before(String named) {
- return Optional.empty();
- }
-
- @Override
- public Optional> after(Class> clazz) {
- return Optional.empty();
- }
-
- @Override
- public Optional> after(String named) {
+ private Optional getCauseAtIndex(int index) {
+ try {
+ final Object object = this.cause[index];
+ return Optional.of((T) object);
+ } catch (Exception e) {
return Optional.empty();
}
+ }
- @Override
- public boolean any(Class> target) {
- return false;
- }
-
- @Override
- public boolean contains(Object object) {
- return false;
- }
-
- @Override
- public boolean any(String named) {
- return false;
- }
-
- @Override
- public List allOf(Class target) {
- return ImmutableList.of();
- }
-
- @Override
- public List noneOf(Class> ignoredClass) {
- return ImmutableList.of();
- }
-
- @Override
- public List all() {
- return ImmutableList.of();
- }
-
- @Override
- public Cause with(Object... additional) {
- return of(additional);
- }
-
- @Override
- public Cause with(Iterable> iterable) {
- List list = new ArrayList<>();
- for (Object o : iterable) {
- list.add(o);
- }
- return of(list.toArray());
- }
-
- @Override
- public Map getNamedCauses() {
- return ImmutableMap.of();
- }
-
- @Override
- public boolean equals(@Nullable Object object) {
- return object == this;
+ @Override
+ public boolean equals(@Nullable Object object) {
+ if (object instanceof Cause) {
+ Cause cause = ((Cause) object);
+ return Arrays.equals(this.cause, cause.cause);
}
+ return false;
+ }
- @Override
- public int hashCode() {
- return 0x39e8a5b;
- }
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(this.cause);
+ }
- @Override
- public String toString() {
- return "Cause[]";
+ @Override
+ public String toString() {
+ String causeString = "Cause[";
+ StringJoiner joiner = new StringJoiner(", ");
+ for (int i = 0; i < this.cause.length; i++) {
+ joiner.add("{Name=" + this.names[i] + ", Object={" + this.cause[i].toString() + "}}");
}
+ return causeString + joiner.toString() + "]";
}
}
diff --git a/src/main/java/org/spongepowered/api/event/world/SaveWorldEvent.java b/src/main/java/org/spongepowered/api/event/world/SaveWorldEvent.java
index b0768e4efed..30f377a5162 100644
--- a/src/main/java/org/spongepowered/api/event/world/SaveWorldEvent.java
+++ b/src/main/java/org/spongepowered/api/event/world/SaveWorldEvent.java
@@ -25,12 +25,11 @@
package org.spongepowered.api.event.world;
import org.spongepowered.api.event.Cancellable;
-import org.spongepowered.api.event.cause.CauseTracked;
import org.spongepowered.api.world.World;
/**
* Base event for when a {@link World} is saved.
*/
-public interface SaveWorldEvent extends TargetWorldEvent, Cancellable, CauseTracked {
+public interface SaveWorldEvent extends TargetWorldEvent, Cancellable {
}
diff --git a/src/main/java/org/spongepowered/api/item/inventory/EmptyInventory.java b/src/main/java/org/spongepowered/api/item/inventory/EmptyInventory.java
index 2d0fcbda6b7..bbc6427c453 100644
--- a/src/main/java/org/spongepowered/api/item/inventory/EmptyInventory.java
+++ b/src/main/java/org/spongepowered/api/item/inventory/EmptyInventory.java
@@ -63,12 +63,12 @@ public ItemStack peek(int limit) {
@Override
public InventoryTransactionResult offer(ItemStack stack) {
- return InventoryTransactionResult.Builder.failNoTransactions();
+ return InventoryTransactionResult.failNoTransactions();
}
@Override
public InventoryTransactionResult set(ItemStack stack) {
- return InventoryTransactionResult.Builder.failNoTransactions();
+ return InventoryTransactionResult.failNoTransactions();
}
@Override
diff --git a/src/main/java/org/spongepowered/api/item/inventory/transaction/InventoryTransactionResult.java b/src/main/java/org/spongepowered/api/item/inventory/transaction/InventoryTransactionResult.java
index 04f88e8bc4a..29c2a0aeb12 100644
--- a/src/main/java/org/spongepowered/api/item/inventory/transaction/InventoryTransactionResult.java
+++ b/src/main/java/org/spongepowered/api/item/inventory/transaction/InventoryTransactionResult.java
@@ -46,6 +46,14 @@ public static Builder builder() {
return new Builder();
}
+ public static InventoryTransactionResult successNoTransactions() {
+ return Builder.builder().result(Type.SUCCESS).build();
+ }
+
+ public static InventoryTransactionResult failNoTransactions() {
+ return Builder.builder().result(Type.ERROR).build();
+ }
+
enum Type {
/**
@@ -131,14 +139,6 @@ public static Builder builder() {
return new Builder();
}
- public static InventoryTransactionResult successNoTransactions() {
- return builder().result(Type.SUCCESS).build();
- }
-
- public static InventoryTransactionResult failNoTransactions() {
- return builder().result(Type.ERROR).build();
- }
-
public Builder result(final Type type) {
this.resultType = checkNotNull(type);
return this;
diff --git a/src/test/java/org/spongepowered/api/event/CauseTest.java b/src/test/java/org/spongepowered/api/event/CauseTest.java
index 6965e0e61ac..8c08f6182a8 100644
--- a/src/test/java/org/spongepowered/api/event/CauseTest.java
+++ b/src/test/java/org/spongepowered/api/event/CauseTest.java
@@ -62,8 +62,9 @@ public void testWithCause() {
final Cause old = Cause.of("foo");
final Cause newCause = old.with("bar");
assertThat(old, not(newCause));
- assertThat(newCause.all().contains("foo"), is(true));
- assertThat(newCause.all().contains("bar"), is(true));
+ List> list = newCause.all();
+ assertThat(list.contains("foo"), is(true));
+ assertThat(list.contains("bar"), is(true));
}
@Test(expected = IllegalArgumentException.class)
@@ -72,12 +73,6 @@ public void testWithNullCause() {
final Cause newCause = old.with((Object) null);
}
- @Test(expected = IllegalArgumentException.class)
- public void testWithNullCauses() {
- final Cause old = Cause.of();
- final Cause newCause = old.with(null, null);
- }
-
@Test
public void testToString() {
final Cause cause = Cause.of("foo", "bar", 1, 2, true, false);
@@ -115,18 +110,6 @@ public void testNoneBefore() {
assertThat(optional.isPresent(), is(false));
}
- @Test
- public void testEmpty() {
- final Cause empty = Cause.of();
- assertThat(empty.isEmpty(), is(true));
- }
-
- @Test
- public void testEmptyWithEmpty() {
- final Cause empty = Cause.of();
- assertThat(empty.with().isEmpty(), is(true));
- }
-
@Test
public void testNoneOf() {
final Cause cause = Cause.of("foo", 1, 2, 3);
@@ -134,15 +117,6 @@ public void testNoneOf() {
assertThat(cause.noneOf(Integer.class).get(0), is("foo"));
}
- @Test
- public void testJustBecauseIcan() {
- final Cause cause = Cause.of();
- final Cause testing = cause.with().with().with().with(new ArrayList<>()).with();
- assertThat(testing.isEmpty(), is(true));
- assertThat(testing.allOf(String.class), hasSize(0));
- assertThat(testing.noneOf(String.class), hasSize(0));
- }
-
@Test
public void testNamedCause() {
final Player player = Mockito.mock(Player.class);
@@ -152,13 +126,6 @@ public void testNamedCause() {
assertThat(optional.isPresent(), is(true));
}
- @Test
- public void testAbsentNamedCause() {
- final Cause emptyCause = Cause.of();
- final Optional optional = emptyCause.get(NamedCause.OWNER);
- assertThat(optional.isPresent(), is(false));
- }
-
@Test(expected = IllegalArgumentException.class)
public void testIllegalNamedCause() {
final Player player = Mockito.mock(Player.class);
diff --git a/src/test/java/org/spongepowered/api/event/SpongeAITaskEventTest.java b/src/test/java/org/spongepowered/api/event/SpongeAITaskEventTest.java
index c14b6733d38..17bebdf36f5 100644
--- a/src/test/java/org/spongepowered/api/event/SpongeAITaskEventTest.java
+++ b/src/test/java/org/spongepowered/api/event/SpongeAITaskEventTest.java
@@ -32,6 +32,8 @@
import org.spongepowered.api.entity.ai.Goal;
import org.spongepowered.api.entity.ai.task.AITask;
import org.spongepowered.api.entity.living.Agent;
+import org.spongepowered.api.event.cause.Cause;
+import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.entity.ai.AITaskEvent;
public class SpongeAITaskEventTest {
@@ -42,8 +44,8 @@ public void testValidTargetAgentAndGoalOwner() {
Goal goal = mock(Goal.class);
Mockito.when(goal.getOwner()).thenReturn(targetEntity);
- SpongeEventFactory.createAITaskEventAdd(mock(Game.class), 0, 0, goal, targetEntity, mock(AITask.class));
- SpongeEventFactory.createAITaskEventRemove(mock(Game.class), goal, targetEntity, mock(AITask.class), 0);
+ SpongeEventFactory.createAITaskEventAdd(Cause.of(NamedCause.source(mock(Game.class))), 0, 0, goal, targetEntity, mock(AITask.class));
+ SpongeEventFactory.createAITaskEventRemove(Cause.of(NamedCause.source(mock(Game.class))), goal, targetEntity, mock(AITask.class), 0);
}
@Test(expected = IllegalArgumentException.class)
@@ -53,7 +55,7 @@ public void testAITaskEventAdd_invalidTargetAgentAndGoalOwner() {
Goal goal = mock(Goal.class);
Mockito.when(goal.getOwner()).thenReturn(secondEntity);
- SpongeEventFactory.createAITaskEventAdd(mock(Game.class), 0, 0, goal, targetEntity, mock(AITask.class));
+ SpongeEventFactory.createAITaskEventAdd(Cause.of(NamedCause.source(mock(Game.class))), 0, 0, goal, targetEntity, mock(AITask.class));
}
@Test(expected = IllegalArgumentException.class)
@@ -63,7 +65,7 @@ public void testAITaskEventRemove_invalidTargetAgentAndGoalOwner() {
Goal goal = mock(Goal.class);
Mockito.when(goal.getOwner()).thenReturn(secondEntity);
- SpongeEventFactory.createAITaskEventRemove(mock(Game.class), goal, targetEntity, mock(AITask.class), 0);
+ SpongeEventFactory.createAITaskEventRemove(Cause.of(NamedCause.source(mock(Game.class))), goal, targetEntity, mock(AITask.class), 0);
}
}
diff --git a/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java b/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java
index f53cc2f1140..d6e3419281d 100644
--- a/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java
+++ b/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java
@@ -51,7 +51,7 @@ public void testParams() {
Entity targetEntity = mockParam(Entity.class);
int originalDamage = 5;
- DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(mockParam(Game.class), Cause.of(), Lists.newArrayList(), targetEntity, originalDamage);
+ DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of("none"), Lists.newArrayList(), targetEntity, originalDamage);
assertThat(event.getOriginalDamage(), is(closeTo(originalDamage, ERROR)));
assertThat(event.getOriginalFinalDamage(), is(closeTo(originalDamage, ERROR)));
@@ -65,7 +65,7 @@ public void testSetBaseDamage() {
Entity targetEntity = mockParam(Entity.class);
int originalDamage = 5;
- DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(mockParam(Game.class), Cause.of(), Lists.newArrayList(), targetEntity, originalDamage);
+ DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of("none"), Lists.newArrayList(), targetEntity, originalDamage);
assertThat(event.getOriginalDamage(), is(closeTo(originalDamage, ERROR)));
assertThat(event.getOriginalFinalDamage(), is(closeTo(originalDamage, ERROR)));
@@ -96,7 +96,7 @@ public void testUseModifiers() {
List>>
originalFunctions = Lists.newArrayList(Tuple.of(firstModifer, p -> p * 2), Tuple.of(secondModifier, p -> p * 5));
- DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity, originalDamage);
+ DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of("none"), originalFunctions, targetEntity, originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
@@ -138,7 +138,7 @@ public void testSetModifiers() {
List>>
originalFunctions = Lists.newArrayList(Tuple.of(firstModifer, p -> p * 2), Tuple.of(secondModifier, p -> p * 5));
- DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity, originalDamage);
+ DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of("none"), originalFunctions, targetEntity, originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
@@ -188,7 +188,7 @@ public void testAddModifier() {
List>> newFunctions = Lists.newArrayList(originalFunctions);
newFunctions.add(Tuple.of(thirdModifier, thirdFunction));
- DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity, originalDamage);
+ DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of("none"), originalFunctions, targetEntity, originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
@@ -223,7 +223,7 @@ public void testModifiersApplicable() {
List>>
originalFunctions = Lists.newArrayList(Tuple.of(firstModifer, p -> p), Tuple.of(secondModifier, p -> p));
- DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity, 0);
+ DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of("none"), originalFunctions, targetEntity, 0);
assertThat(event.isModifierApplicable(firstModifer), is(true));
assertThat(event.isModifierApplicable(secondModifier), is(true));
@@ -232,7 +232,7 @@ public void testModifiersApplicable() {
@Test(expected = IllegalArgumentException.class)
public void testNotApplicableModifer() {
- DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(mockParam(Game.class), Cause.of(), Lists.newArrayList(), mockParam(Entity.class), 0);
+ DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of("none"), Lists.newArrayList(), mockParam(Entity.class), 0);
DamageModifier modifier = mockParam(DamageModifier.class);
diff --git a/src/test/java/org/spongepowered/api/event/SpongeAbstractEventTest.java b/src/test/java/org/spongepowered/api/event/SpongeAbstractEventTest.java
index cb1ad9beadd..e94eb4d809f 100644
--- a/src/test/java/org/spongepowered/api/event/SpongeAbstractEventTest.java
+++ b/src/test/java/org/spongepowered/api/event/SpongeAbstractEventTest.java
@@ -55,7 +55,7 @@ public void testChangeBlockEvent_filter() {
stub(transaction.getOriginal().getLocation()).toReturn(Optional.of(new Location<>(mockParam(World.class), Vector3d.ZERO)));
- ChangeBlockEvent event = SpongeEventFactory.createChangeBlockEvent(mockParam(Game.class), Cause.of(), mockParam(World.class), Lists.newArrayList(transaction));
+ ChangeBlockEvent event = SpongeEventFactory.createChangeBlockEvent(Cause.of("none"), mockParam(World.class), Lists.newArrayList(transaction));
event.filter(location -> false);
assertThat(transaction.isValid(), is(false));
@@ -66,8 +66,8 @@ public void testValueChangeEvent() {
DataTransactionResult original = DataTransactionResult.failNoData();
DataTransactionResult modified = DataTransactionResult.successNoData();
- ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange(mockParam(Game.class),
- original, mockParam(DataHolder.class));
+ ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange(Cause.of("none"), original,
+ mockParam(DataHolder.class));
assertThat(event.getOriginalChanges(), is(equalTo(original)));
diff --git a/src/test/java/org/spongepowered/api/event/SpongeAbstractHealEntityEventTest.java b/src/test/java/org/spongepowered/api/event/SpongeAbstractHealEntityEventTest.java
index eba1aeecacc..61ee8b36a32 100644
--- a/src/test/java/org/spongepowered/api/event/SpongeAbstractHealEntityEventTest.java
+++ b/src/test/java/org/spongepowered/api/event/SpongeAbstractHealEntityEventTest.java
@@ -51,7 +51,7 @@ public void testParams() {
Entity targetEntity = mockParam(Entity.class);
int originalDamage = 5;
- HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(mockParam(Game.class), Cause.of(), Lists.newArrayList(), targetEntity, originalDamage);
+ HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of("none"), Lists.newArrayList(), targetEntity, originalDamage);
assertThat(event.getOriginalHealAmount(), is(closeTo(originalDamage, ERROR)));
assertThat(event.getOriginalFinalHealAmount(), is(closeTo(originalDamage, ERROR)));
@@ -65,7 +65,7 @@ public void testSetBaseHealAmount() {
Entity targetEntity = mockParam(Entity.class);
int originalDamage = 5;
- HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(mockParam(Game.class), Cause.of(), Lists.newArrayList(), targetEntity,
+ HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of("none"), Lists.newArrayList(), targetEntity,
originalDamage);
assertThat(event.getOriginalHealAmount(), is(closeTo(originalDamage, ERROR)));
@@ -97,7 +97,7 @@ public void testUseModifiers() {
List>>
originalFunctions = Lists.newArrayList(Tuple.of(firstModifer, p -> p * 2), Tuple.of(secondModifier, p -> p * 5));
- HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity,
+ HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of("none"), originalFunctions, targetEntity,
originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
@@ -140,7 +140,7 @@ public void testSetModifiers() {
List>>
originalFunctions = Lists.newArrayList(Tuple.of(firstModifer, p -> p * 2), Tuple.of(secondModifier, p -> p * 5));
- HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity,
+ HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of("none"), originalFunctions, targetEntity,
originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
@@ -191,7 +191,7 @@ public void testAddModifier() {
List>> newFunctions = Lists.newArrayList(originalFunctions);
newFunctions.add(Tuple.of(thirdModifier, thirdFunction));
- HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity,
+ HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of("none"), originalFunctions, targetEntity,
originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
@@ -227,7 +227,7 @@ public void testModifiersApplicable() {
List>>
originalFunctions = Lists.newArrayList(Tuple.of(firstModifer, p -> p), Tuple.of(secondModifier, p -> p));
- HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(mockParam(Game.class), Cause.of(), originalFunctions, targetEntity, 0);
+ HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of("none"), originalFunctions, targetEntity, 0);
assertThat(event.isModifierApplicable(firstModifer), is(true));
assertThat(event.isModifierApplicable(secondModifier), is(true));
@@ -236,7 +236,7 @@ public void testModifiersApplicable() {
@Test(expected = IllegalArgumentException.class)
public void testNotApplicableModifer() {
- HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(mockParam(Game.class), Cause.of(), Lists.newArrayList(),
+ HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of("none"), Lists.newArrayList(),
mockParam(Entity.class), 0);
HealthModifier modifier = mockParam(HealthModifier.class);
diff --git a/src/test/java/org/spongepowered/api/event/SpongeEventFactoryTest.java b/src/test/java/org/spongepowered/api/event/SpongeEventFactoryTest.java
index ef80113c3a0..f76cc10c851 100644
--- a/src/test/java/org/spongepowered/api/event/SpongeEventFactoryTest.java
+++ b/src/test/java/org/spongepowered/api/event/SpongeEventFactoryTest.java
@@ -28,6 +28,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;
+import com.flowpowered.math.vector.Vector3d;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
@@ -39,6 +40,8 @@
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.Transaction;
import org.spongepowered.api.entity.Transform;
+import org.spongepowered.api.event.cause.Cause;
+import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.event.entity.HealEntityEvent;
import org.spongepowered.api.event.entity.ai.AITaskEvent;
@@ -183,6 +186,10 @@ public static Object mockParam(final Class> paramType) {
return UUID.randomUUID();
} else if (paramType == DataTransactionResult.class) {
return DataTransactionResult.successNoData();
+ } else if (paramType == Cause.class) {
+ return Cause.of(NamedCause.source("none"));
+ } else if (paramType == Location.class) {
+ return new Location<>(mock(World.class), Vector3d.ZERO);
} else {
return mock(paramType, withSettings().defaultAnswer(Mockito.RETURNS_MOCKS));
}