-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #400 from HyKurtis/master
Added addon request system to request data from an addon in spigot plugins
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/world/bentobox/bentobox/api/addons/exceptions/AddonRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package world.bentobox.bentobox.api.addons.exceptions; | ||
|
||
import world.bentobox.bentobox.api.addons.request.AddonRequestBuilder; | ||
|
||
import java.util.UUID; | ||
|
||
public class AddonRequestException extends AddonException | ||
{ | ||
private static final long serialVersionUID = -5698456013070166174L; | ||
|
||
public AddonRequestException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/world/bentobox/bentobox/api/addons/request/AddonRequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package world.bentobox.bentobox.api.addons.request; | ||
|
||
import org.apache.commons.lang.Validate; | ||
import world.bentobox.bentobox.BentoBox; | ||
import world.bentobox.bentobox.api.addons.Addon; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class AddonRequestBuilder | ||
{ | ||
private String addonName; | ||
private String requestLabel; | ||
private Map<String, Object> metaData = new HashMap<>(); | ||
|
||
/** | ||
* Define the addon you wish to request. | ||
* | ||
* @param addonName | ||
*/ | ||
public AddonRequestBuilder addon(String addonName) { | ||
this.addonName = addonName; | ||
return this; | ||
} | ||
|
||
/** | ||
* Define label for addon request. | ||
* | ||
* @param requestLabel | ||
*/ | ||
public AddonRequestBuilder label(String requestLabel) { | ||
this.requestLabel = requestLabel; | ||
return this; | ||
} | ||
|
||
/** | ||
* Add meta data to addon request. | ||
* | ||
* @param key | ||
* @param value | ||
*/ | ||
public AddonRequestBuilder addMetaData(String key, Object value) { | ||
metaData.put(key, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* Send request to addon. | ||
* | ||
* @return request response, null if no response. | ||
*/ | ||
public Object request() { | ||
Validate.notNull(addonName); | ||
Validate.notNull(requestLabel); | ||
|
||
Optional<Addon> addonOptional = BentoBox.getInstance().getAddonsManager().getAddonByName(addonName); | ||
if(addonOptional.isPresent()) { | ||
Addon addon = addonOptional.get(); | ||
return addon.request(requestLabel, metaData); | ||
} | ||
return null; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/world/bentobox/bentobox/api/addons/request/AddonRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package world.bentobox.bentobox.api.addons.request; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class AddonRequestHandler | ||
{ | ||
private String label; | ||
|
||
public AddonRequestHandler(String label) { | ||
this.label = label.toLowerCase(); | ||
} | ||
|
||
/** | ||
* Get request handler label. | ||
* | ||
* @return label | ||
*/ | ||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
/** | ||
* Handle an addon request. | ||
* This is used only for Addons to respond to addon requests from plugins. | ||
* Example: request island level from Levels addon. | ||
* | ||
* @param metaData | ||
* @return request response | ||
*/ | ||
public abstract Object handle(Map<String, Object> metaData); | ||
} |