From 1eaefc26bfb422b8ccb3162d34c948a08c80d178 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Thu, 19 Dec 2019 15:21:51 -0600 Subject: [PATCH] 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 --- .../geoffrey/Commands/FindCommand.java | 45 ++++------ .../geoffrey/Commands/SellingCommand.java | 64 +++++++++++++ .../com/zerohighdef/geoffrey/GeoffreyMC.java | 45 +++++++--- .../geoffrey/Models/GeoffreyItemListing.java | 41 +++++++++ .../geoffrey/Models/GeoffreyShop.java | 41 +++++++++ .../geoffrey/Objects/GeoffreyAPICallback.java | 89 +++++++++++++++++++ .../Objects/GeoffreyCommandError.java | 8 ++ src/main/resources/config.yml | 4 + src/main/resources/plugin.yml | 5 +- 9 files changed, 300 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyItemListing.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyAPICallback.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommandError.java create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java index 947a4fb..d0d4056 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java @@ -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 params = new HashMap(); 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; + } } } } diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java new file mode 100644 index 0000000..430ccd3 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java @@ -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 params = new HashMap(); + + 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; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java index fd97e53..8a68906 100644 --- a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java +++ b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java @@ -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(); - FindCommand findCommand = new FindCommand(this); - this.getCommand("find").setExecutor(findCommand); + if (isEnabled()) { + APIToken = config.getString("geoffrey_api.key"); + baseURL = config.getString("geoffrey_api.base_url"); + FindCommand findCommand = new FindCommand(this); + 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; } } diff --git a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyItemListing.java b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyItemListing.java new file mode 100644 index 0000000..5738fd2 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyItemListing.java @@ -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; + } +} diff --git a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java new file mode 100644 index 0000000..b66dc03 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java @@ -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 items; + + public GeoffreyShop(JsonObject locationJSON) { + super(locationJSON); + + items = new 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 LinkedList(); + } + + public List getItems() { + return items; + } + + public GeoffreyItemListing getFirstItem() { + return items.get(0); + } +} diff --git a/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyAPICallback.java b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyAPICallback.java new file mode 100644 index 0000000..f4fe7d5 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyAPICallback.java @@ -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 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); +} diff --git a/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommandError.java b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommandError.java new file mode 100644 index 0000000..7440717 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommandError.java @@ -0,0 +1,8 @@ +package com.zerohighdef.geoffrey.Objects; + +public class GeoffreyCommandError extends Exception{ + + public GeoffreyCommandError () { + super("Command encountered an error"); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..f4a2d96 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,4 @@ +# GeoffreyAPI Settings +geoffrey_api: + key: '' + base_url: '' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index b760e93..92328f2 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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]