Added error handling and selling command

+ Errors are handled when the response is parsed as JSON in GeoffreyAPICallback
+ Each command has a Map of errors and error messages it handles
+ Added new Models GeoffreyShop and GeoffreyItemListing
+ Added support for a config
pull/2/head
Joey Hines 2019-12-19 15:21:51 -06:00
parent b4fc8e5750
commit 1eaefc26bf
9 changed files with 300 additions and 42 deletions

View File

@ -1,15 +1,12 @@
package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zerohighdef.geoffrey.GeoffreyMC;
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import xyz.etztech.core.web.ICallback;
import java.util.HashMap;
import java.util.Map;
@ -26,39 +23,31 @@ public class FindCommand extends GeoffreyCommand {
Map <String, String> params = new HashMap<String, String>();
params.put("search", locationName);
RunCommand("find_location", params, new CommandCallback((sender)));
RunCommand("find_location", params, new CommandCallback(sender, locationName));
return true;
}
protected String formatedCommand(String string) {
JsonArray list = new JsonParser().parse(string).getAsJsonArray();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("The following locations match:\n");
for (JsonElement element: list) {
JsonObject jsonObject = element.getAsJsonObject();
GeoffreyLocation geoffreyLocation = new GeoffreyLocation(jsonObject);
private class CommandCallback extends GeoffreyAPICallback {
private String locationName;
stringBuilder.append(geoffreyLocation.getFormattedLocationString());
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
private class CommandCallback implements ICallback {
private CommandSender sender;
CommandCallback(CommandSender sender) {
this.sender = sender;
CommandCallback(CommandSender sender, String locationName) {
super(sender);
errors.put("LocationLookUpError", "There are no locations that match " + locationName);
this.locationName = locationName;
}
@Override
public void invoke(String s) {
String msg = formatedCommand(s);
sender.sendMessage(ChatColor.GREEN + msg);
try {
JsonElement json = parseJSON(s);
String msg = ChatColor.BOLD + "The following locations match " + locationName + ":\n" + ChatColor.RESET + locationList(json, 5);
commandSender.sendMessage(ChatColor.GREEN + msg);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}

View File

@ -0,0 +1,64 @@
package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonElement;
import com.zerohighdef.geoffrey.GeoffreyMC;
import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.util.HashMap;
import java.util.Map;
public class SellingCommand extends GeoffreyCommand {
public SellingCommand(GeoffreyMC plugin) {
super(plugin);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String item = args[0];
String commandName = "selling";
if (args.length > 1) {
String sort = args[1];
if (sort.compareToIgnoreCase("price") == 0) {
commandName = "selling_price";
}
}
Map <String, String> params = new HashMap<String, String>();
params.put("item_name", item);
RunCommand(commandName, params, new CommandCallback(sender, item));
return true;
}
private class CommandCallback extends GeoffreyAPICallback {
private String item;
CommandCallback(CommandSender sender, String item) {
super(sender);
errors.put("ItemNotFound", "No shop was found selling " + item);
this.item = item;
}
@Override
public void invoke(String s) {
try {
JsonElement json = parseJSON(s);
String msg = ChatColor.BOLD + "The following shops sell " + item + ":\n" + ChatColor.RESET + sellingList(json, 5);
commandSender.sendMessage(ChatColor.GREEN + msg);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}

View File

@ -1,19 +1,39 @@
package com.zerohighdef.geoffrey;
import com.zerohighdef.geoffrey.Commands.FindCommand;
import com.zerohighdef.geoffrey.Commands.SellingCommand;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;
public final class GeoffreyMC extends JavaPlugin {
private String APIToken = "ZIcJJHJTBqkdjAzxqJUmDQfLc";
private String BaseURL = "https://test.zerohighdef.com/GeoffreyApp/api";
private String APIToken;
private String baseURL;
private static FileConfiguration config;
private Logger log = Logger.getLogger( "Minecraft" );
@Override
public void onEnable() {
// Plugin startup logic
saveDefaultConfig();
reloadConfig();
if (isEnabled()) {
APIToken = config.getString("geoffrey_api.key");
baseURL = config.getString("geoffrey_api.base_url");
FindCommand findCommand = new FindCommand(this);
this.getCommand("find").setExecutor(findCommand);
this.getCommand("geoffrey_find").setExecutor(findCommand);
SellingCommand sellingCommand = new SellingCommand(this);
this.getCommand("geoffrey_selling").setExecutor(sellingCommand);
}
}
public void log(String message) {
log.info( "[GeoffreyMC]: " + message );
}
@Override
@ -21,19 +41,18 @@ public final class GeoffreyMC extends JavaPlugin {
// Plugin shutdown logic
}
@Override
public void reloadConfig() {
super.reloadConfig();
config = Bukkit.getPluginManager().getPlugin("GeoffreyMC").getConfig();
}
public String getAPIToken() {
return APIToken;
}
public void setAPIToken(String APIToken) {
this.APIToken = APIToken;
}
public String getBaseURL() {
return BaseURL;
}
public void setBaseURL(String baseURL) {
BaseURL = baseURL;
return baseURL;
}
}

View File

@ -0,0 +1,41 @@
package com.zerohighdef.geoffrey.Models;
import com.google.gson.JsonObject;
public class GeoffreyItemListing {
private int amount;
private int price;
private int daysSinceRestock;
private String itemName;
public GeoffreyItemListing(JsonObject itemListingJSON) {
this.amount = itemListingJSON.get("amount").getAsInt();
this.price = itemListingJSON.get("price").getAsInt();
this.itemName = itemListingJSON.get("item_name").getAsString();
this.daysSinceRestock = itemListingJSON.get("days_since_restock").getAsInt();
}
public GeoffreyItemListing(int amount, int price, int daysSinceRestock, String itemName) {
this.amount = amount;
this.price = price;
this.itemName = itemName;
this.daysSinceRestock = daysSinceRestock;
}
public int getAmount() {
return amount;
}
public String getItemName() {
return itemName;
}
public String getFormattedItemListing() {
return amount + " " + itemName + " for " + price + "D, restocked " + daysSinceRestock + " day(s) ago" ;
}
public int getPrice() {
return price;
}
}

View File

@ -0,0 +1,41 @@
package com.zerohighdef.geoffrey.Models;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.LinkedList;
import java.util.List;
public class GeoffreyShop extends GeoffreyLocation{
private List <GeoffreyItemListing> items;
public GeoffreyShop(JsonObject locationJSON) {
super(locationJSON);
items = new <GeoffreyItemListing>LinkedList();
if (!locationJSON.get("items").isJsonNull()) {
JsonArray itemsJSON = locationJSON.get("items").getAsJsonArray();
for (JsonElement element: itemsJSON) {
JsonObject item = element.getAsJsonObject();
items.add(new GeoffreyItemListing(item));
}
}
}
public GeoffreyShop(int xCoord, int zCoord, String locationName, GeoffreyPlayer owner, String dimension) {
super(xCoord, zCoord, locationName, owner, dimension);
items = new <GeoffreyItemListing>LinkedList();
}
public List getItems() {
return items;
}
public GeoffreyItemListing getFirstItem() {
return items.get(0);
}
}

View File

@ -0,0 +1,89 @@
package com.zerohighdef.geoffrey.Objects;
import com.google.common.collect.Iterables;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
import com.zerohighdef.geoffrey.Models.GeoffreyShop;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import xyz.etztech.core.web.ICallback;
import java.util.HashMap;
import java.util.Map;
public abstract class GeoffreyAPICallback implements ICallback {
protected CommandSender commandSender;
protected HashMap<String, String> errors = new HashMap<>();
public GeoffreyAPICallback(CommandSender commandSender) {
this.commandSender = commandSender;
errors.put("PlayerNotFound", "You are not in the database, you must register first!");
errors.put("NoLocationsInDatabase", "You have no locations in the database, you must add some first!");
errors.put("DataError", "Slow down their slugger, that's a long word or number you are trying to cram into me, try again with something smaller, please");
errors.put("LocationHasTunnelError", "that location already has a tunnel you goober.");
errors.put( "EntryNameNotUniqueError", "that name has already been used, be more creative dingus kong");
}
protected JsonElement parseJSON(String string) throws GeoffreyCommandError {
JsonElement element = new JsonParser().parse(string);
if (element.isJsonObject()) {
JsonObject object = element.getAsJsonObject();
if (object.has("error")) {
String error = object.get("error").getAsString();
String errorMessage = "An internal error has occurred.";
if (errors.containsKey(error)) {
errorMessage = errors.get(error);
}
commandSender.sendMessage(ChatColor.RED + errorMessage);
throw new GeoffreyCommandError();
}
}
return element;
}
protected String locationList(JsonElement jsonElement, int maxLength) {
JsonArray list = jsonElement.getAsJsonArray();
StringBuilder stringBuilder = new StringBuilder();
for (JsonElement element: Iterables.limit(list, maxLength)) {
JsonObject jsonObject = element.getAsJsonObject();
GeoffreyLocation geoffreyLocation = new GeoffreyLocation(jsonObject);
stringBuilder.append(geoffreyLocation.getFormattedLocationString());
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
protected String sellingList(JsonElement jsonElement, int maxLength) {
JsonArray list = jsonElement.getAsJsonArray();
StringBuilder stringBuilder = new StringBuilder();
for (JsonElement element: Iterables.limit(list, maxLength)) {
JsonObject jsonObject = element.getAsJsonObject();
GeoffreyShop geoffreyShop = new GeoffreyShop(jsonObject);
stringBuilder.append(geoffreyShop.getLocationName());
stringBuilder.append(" ");
stringBuilder.append(geoffreyShop.getPositionString());
stringBuilder.append(" ");
stringBuilder.append(geoffreyShop.getFirstItem().getFormattedItemListing());
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
@Override
public abstract void invoke(String s);
}

View File

@ -0,0 +1,8 @@
package com.zerohighdef.geoffrey.Objects;
public class GeoffreyCommandError extends Exception{
public GeoffreyCommandError () {
super("Command encountered an error");
}
}

View File

@ -0,0 +1,4 @@
# GeoffreyAPI Settings
geoffrey_api:
key: ''
base_url: ''

View File

@ -1,4 +1,4 @@
name: version
name: ${name}
version: ${version}
description: Geoffrey API Plugin for PaperMC
main: ${mainClass}
@ -7,3 +7,6 @@ commands:
geoffrey_find:
description: Finds a location in Geoffrey
aliases: [find, find_location]
geoffrey_selling:
description: Finds an item for sale in Geoffrey
aliases: [selling]