Add a optional payload to DhApiResult

This commit is contained in:
James Seibel
2022-11-13 21:42:11 -06:00
parent 0bd5730128
commit d723a1fa84
3 changed files with 28 additions and 13 deletions
@@ -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<? extends IDhApiEvent> eventInterface, IDhApiEvent eventHandlerImplementation)
public static DhApiResult<Void> on(Class<? extends IDhApiEvent> 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<? extends IDhApiEvent> eventInterface, Class<IDhApiEvent> eventHandlerClass)
public static DhApiResult<Void> off(Class<? extends IDhApiEvent> eventInterface, Class<IDhApiEvent> eventHandlerClass)
{
if (DhApiEventInjector.INSTANCE.unbind(eventInterface, eventHandlerClass))
{
@@ -21,7 +21,7 @@ public class DhApiWorldGeneratorOverrideRegister implements IDhApiWorldGenerator
@Override
public DhApiResult registerWorldGeneratorOverride(IDhApiWorldGenerator worldGenerator)
public DhApiResult<Void> registerWorldGeneratorOverride(IDhApiWorldGenerator worldGenerator)
{
try
{
@@ -35,7 +35,7 @@ public class DhApiWorldGeneratorOverrideRegister implements IDhApiWorldGenerator
}
@Override
public DhApiResult registerWorldGeneratorOverride(IDhApiLevelWrapper levelWrapper, IDhApiWorldGenerator worldGenerator)
public DhApiResult<Void> registerWorldGeneratorOverride(IDhApiLevelWrapper levelWrapper, IDhApiWorldGenerator worldGenerator)
{
try
{
@@ -2,31 +2,46 @@ package com.seibel.lod.api.objects;
/**
* Allows for more descriptive non-critical failure states.
*
*
* @param <T> 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<T>
{
/** 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. <br>
* 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 <Pt> DhApiResult<Pt> createSuccess() { return new DhApiResult<>(true, ""); }
public static <Pt> DhApiResult<Pt> createSuccess(String message) { return new DhApiResult<>(true, message); }
public static <Pt> DhApiResult<Pt> createSuccess(Pt payload) { return new DhApiResult<Pt>(true, "", payload); }
public static <Pt> DhApiResult<Pt> createSuccess(String message, Pt payload) { return new DhApiResult<Pt>(true, message, payload); }
public static DhApiResult createFail() { return new DhApiResult(false, ""); }
public static DhApiResult createFail(String message) { return new DhApiResult(false, message); }
public static <Pt> DhApiResult<Pt> createFail() { return new DhApiResult<>(false, ""); }
public static <Pt> DhApiResult<Pt> createFail(String message) { return new DhApiResult<>(false, message); }
public static <Pt> DhApiResult<Pt> createFail(String message, Pt payload) { return new DhApiResult<Pt>(false, message, payload); }
}