From d723a1fa8467a57f7d1b48afd4c9e175783a4fce Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sun, 13 Nov 2022 21:42:11 -0600 Subject: [PATCH] Add a optional payload to DhApiResult --- .../methods/events/DhApiEventRegister.java | 4 +-- .../DhApiWorldGeneratorOverrideRegister.java | 4 +-- .../seibel/lod/api/objects/DhApiResult.java | 33 ++++++++++++++----- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/api/src/main/java/com/seibel/lod/api/methods/events/DhApiEventRegister.java b/api/src/main/java/com/seibel/lod/api/methods/events/DhApiEventRegister.java index 4ae6b122c..0d44d4fce 100644 --- a/api/src/main/java/com/seibel/lod/api/methods/events/DhApiEventRegister.java +++ b/api/src/main/java/com/seibel/lod/api/methods/events/DhApiEventRegister.java @@ -18,7 +18,7 @@ public class DhApiEventRegister * If multiple of the same eventHandler are added DhApiResult will return * the name of the already added handler and success = false. */ - public static DhApiResult on(Class eventInterface, IDhApiEvent eventHandlerImplementation) + public static DhApiResult on(Class eventInterface, IDhApiEvent eventHandlerImplementation) { try { @@ -36,7 +36,7 @@ public class DhApiEventRegister * If no eventHandler of the given class has been registered the result will return * success = false. */ - public static DhApiResult off(Class eventInterface, Class eventHandlerClass) + public static DhApiResult off(Class eventInterface, Class eventHandlerClass) { if (DhApiEventInjector.INSTANCE.unbind(eventInterface, eventHandlerClass)) { diff --git a/api/src/main/java/com/seibel/lod/api/methods/override/DhApiWorldGeneratorOverrideRegister.java b/api/src/main/java/com/seibel/lod/api/methods/override/DhApiWorldGeneratorOverrideRegister.java index 6dd21d4a4..2d45f7aa9 100644 --- a/api/src/main/java/com/seibel/lod/api/methods/override/DhApiWorldGeneratorOverrideRegister.java +++ b/api/src/main/java/com/seibel/lod/api/methods/override/DhApiWorldGeneratorOverrideRegister.java @@ -21,7 +21,7 @@ public class DhApiWorldGeneratorOverrideRegister implements IDhApiWorldGenerator @Override - public DhApiResult registerWorldGeneratorOverride(IDhApiWorldGenerator worldGenerator) + public DhApiResult registerWorldGeneratorOverride(IDhApiWorldGenerator worldGenerator) { try { @@ -35,7 +35,7 @@ public class DhApiWorldGeneratorOverrideRegister implements IDhApiWorldGenerator } @Override - public DhApiResult registerWorldGeneratorOverride(IDhApiLevelWrapper levelWrapper, IDhApiWorldGenerator worldGenerator) + public DhApiResult registerWorldGeneratorOverride(IDhApiLevelWrapper levelWrapper, IDhApiWorldGenerator worldGenerator) { try { diff --git a/api/src/main/java/com/seibel/lod/api/objects/DhApiResult.java b/api/src/main/java/com/seibel/lod/api/objects/DhApiResult.java index 285baef31..88cd114a1 100644 --- a/api/src/main/java/com/seibel/lod/api/objects/DhApiResult.java +++ b/api/src/main/java/com/seibel/lod/api/objects/DhApiResult.java @@ -2,31 +2,46 @@ package com.seibel.lod.api.objects; /** * Allows for more descriptive non-critical failure states. - * + * + * @param The payload type this result contains, can be Void if the result is just used to notify success/failure. + * * @author James Seibel - * @version 2022-8-15 + * @version 2022-11-12 */ -public class DhApiResult +public class DhApiResult { /** True if the action succeeded, false otherwise. */ public final boolean success; /** If the action failed this contains the reason as to why. */ - public final String errorMessage; + public final String errorMessage; // TODO rename to just "message" + + /** + * Whatever object the API Method generated/returned.
+ * Will be null/Void if this result is just used to notify success/failure. + */ + public final T payload; - private DhApiResult(boolean newSuccess, String newErrorMessage) + + // these constructors are private because the create... methods below are easier to understand + private DhApiResult(boolean newSuccess, String newErrorMessage) { this(newSuccess, newErrorMessage, null); } + private DhApiResult(boolean newSuccess, String newErrorMessage, T payload) { this.success = newSuccess; this.errorMessage = newErrorMessage; + this.payload = payload; } - public static DhApiResult createSuccess() { return new DhApiResult(true, ""); } - public static DhApiResult createSuccess(String message) { return new DhApiResult(true, message); } + public static DhApiResult createSuccess() { return new DhApiResult<>(true, ""); } + public static DhApiResult createSuccess(String message) { return new DhApiResult<>(true, message); } + public static DhApiResult createSuccess(Pt payload) { return new DhApiResult(true, "", payload); } + public static DhApiResult createSuccess(String message, Pt payload) { return new DhApiResult(true, message, payload); } - public static DhApiResult createFail() { return new DhApiResult(false, ""); } - public static DhApiResult createFail(String message) { return new DhApiResult(false, message); } + public static DhApiResult createFail() { return new DhApiResult<>(false, ""); } + public static DhApiResult createFail(String message) { return new DhApiResult<>(false, message); } + public static DhApiResult createFail(String message, Pt payload) { return new DhApiResult(false, message, payload); } }