mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-12 19:30:50 +01:00
Merge pull request #400 from HyKurtis/master
Added addon request system to request data from an addon in spigot plugins
This commit is contained in:
commit
8da55b6e21
@ -3,6 +3,8 @@ package world.bentobox.bentobox.api.addons;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
@ -15,6 +17,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.bentobox.managers.PlayersManager;
|
||||
|
||||
@ -32,6 +35,7 @@ public abstract class Addon {
|
||||
private FileConfiguration config;
|
||||
private File dataFolder;
|
||||
private File file;
|
||||
private Map<String, AddonRequestHandler> requestHandlers = new HashMap<>();
|
||||
|
||||
public Addon() {
|
||||
state = State.DISABLED;
|
||||
@ -359,4 +363,28 @@ public abstract class Addon {
|
||||
public String getPermissionPrefix() {
|
||||
return this.getDescription().getName().toLowerCase() + ".";
|
||||
}
|
||||
|
||||
/**
|
||||
* Register request handler to answer requests from plugins.
|
||||
* @param handler
|
||||
*/
|
||||
public void registerRequestHandler(AddonRequestHandler handler) {
|
||||
requestHandlers.put(handler.getLabel(), handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request to addon.
|
||||
* @param label
|
||||
* @param metaData
|
||||
* @return request response, null if no response.
|
||||
*/
|
||||
public Object request(String label, Map<String, Object> metaData) {
|
||||
label = label.toLowerCase();
|
||||
AddonRequestHandler handler = requestHandlers.get(label);
|
||||
if(handler != null) {
|
||||
return handler.handle(metaData);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user