From e7bddf51cbd67b8d391ab1bf156380b2c1b643ce Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Tue, 7 Jan 2020 17:48:48 -0600 Subject: [PATCH] Added info command + Added objects for location models that have unique info strings + Fixed add_resident --- .../geoffrey/Commands/AddResidentCommand.java | 11 +- .../geoffrey/Commands/InfoCommand.java | 103 ++++++++++++++++++ .../com/zerohighdef/geoffrey/GeoffreyMC.java | 12 +- .../geoffrey/Models/GeoffreyLocation.java | 11 ++ .../geoffrey/Models/GeoffreyMarket.java | 37 +++++++ .../geoffrey/Models/GeoffreyShop.java | 16 +++ .../geoffrey/Models/GeoffreyTown.java | 38 +++++++ .../geoffrey/Objects/GeoffreyCommand.java | 4 +- src/main/resources/config.yml | 1 + src/main/resources/plugin.yml | 4 + 10 files changed, 228 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/zerohighdef/geoffrey/Commands/InfoCommand.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyMarket.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyTown.java diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/AddResidentCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/AddResidentCommand.java index 72cb4f1..e3ef87f 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/AddResidentCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/AddResidentCommand.java @@ -50,21 +50,22 @@ public class AddResidentCommand extends GeoffreyCommand { } private class CommandCallback extends GeoffreyAPICallback { + private String newResidentName; - CommandCallback(CommandSender sender, String new_resident_name) { + CommandCallback(CommandSender sender, String newResidentName) { super(sender); - errors.put("ResidentNotFoundError", "Ain't no one in this darn database named" + new_resident_name + "you goob"); - errors.put("IsResidentError", new_resident_name + " is already a resident, stop having amosia."); + errors.put("ResidentNotFoundError", "Ain't no one in this darn database named" + newResidentName + "you goob"); + errors.put("IsResidentError", newResidentName + " is already a resident, stop having amosia."); errors.put("LocationLookUpError", "You do not have a town by that name you ding dong goober."); + this.newResidentName = newResidentName; } @Override public void invoke(String s) { try { JsonElement json = parseJSON(s); - GeoffreyPlayer player = new GeoffreyPlayer(json.getAsJsonObject()); - String msg = ChatColor.GREEN + player.getUsername() + "has been added to your town"; + String msg = ChatColor.GREEN + newResidentName + "has been added to your town"; commandSender.sendMessage(msg); } catch (GeoffreyCommandError e) { diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/InfoCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/InfoCommand.java new file mode 100644 index 0000000..215ac53 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/InfoCommand.java @@ -0,0 +1,103 @@ +package com.zerohighdef.geoffrey.Commands; + +import com.google.gson.JsonElement; +import com.zerohighdef.geoffrey.GeoffreyMC; +import com.zerohighdef.geoffrey.Models.GeoffreyLocation; +import com.zerohighdef.geoffrey.Models.GeoffreyMarket; +import com.zerohighdef.geoffrey.Models.GeoffreyShop; +import com.zerohighdef.geoffrey.Models.GeoffreyTown; +import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback; +import com.zerohighdef.geoffrey.Objects.GeoffreyCommand; +import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError; +import com.zerohighdef.geoffrey.Objects.GeoffreyUtil; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.text.ParseException; +import java.util.HashMap; +import java.util.Map; + +public class InfoCommand extends GeoffreyCommand { + public InfoCommand(GeoffreyMC plugin) { + super(plugin); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + + if (args.length > 0) { + try { + String locationName = GeoffreyUtil.parseArgs(args).get(0); + Map params = new HashMap(); + + params.put("location_name", locationName); + RunCommand("info", params, Method.GET, new CommandCallback(sender, locationName)); + } + catch (ParseException e) { + sender.sendMessage(ChatColor.RED + e.getMessage()); + } + } + else { + sender.sendMessage(ChatColor.RED + "You must specify a location name."); + return false; + } + + return true; + } + + + private class CommandCallback extends GeoffreyAPICallback { + private String locationName; + + 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) { + try { + JsonElement json = parseJSON(s); + String locationType = json.getAsJsonObject().get("type").getAsString(); + + String infoString; + String url = plugin.getBaseURL(); + + if (locationType.compareToIgnoreCase("shop") == 0) { + GeoffreyShop shop = new GeoffreyShop(json.getAsJsonObject()); + infoString = shop.getInfoString(); + url += shop.getLink(); + } + else if (locationType.compareToIgnoreCase("town") == 0) { + GeoffreyTown town = new GeoffreyTown(json.getAsJsonObject()); + infoString = town.getInfoString(); + url += town.getLink(); + } + else if (locationType.compareToIgnoreCase("market") == 0) { + GeoffreyMarket market = new GeoffreyMarket(json.getAsJsonObject()); + infoString = market.getInfoString(); + url += market.getLink(); + } + else { + GeoffreyLocation location = new GeoffreyLocation(json.getAsJsonObject()); + infoString = location.getInfoString(); + url += location.getLink(); + } + + TextComponent message = new TextComponent(); + + message.setText(infoString); + message.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)); + message.setColor(net.md_5.bungee.api.ChatColor.GREEN); + commandSender.spigot().sendMessage(message); + } + catch (GeoffreyCommandError e) { + return; + } + } + } +} diff --git a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java index 0686309..27b2fa1 100644 --- a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java +++ b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java @@ -1,8 +1,6 @@ package com.zerohighdef.geoffrey; import com.zerohighdef.geoffrey.Commands.*; -import com.zerohighdef.geoffrey.Models.GeoffreyItemListing; -import com.zerohighdef.geoffrey.Models.GeoffreyLocation; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; @@ -11,6 +9,7 @@ import java.util.logging.Logger; public final class GeoffreyMC extends JavaPlugin { private String APIToken; + private String baseAPIURL; private String baseURL; private static FileConfiguration config; private Logger log = Logger.getLogger( "Minecraft" ); @@ -23,7 +22,9 @@ public final class GeoffreyMC extends JavaPlugin { if (isEnabled()) { APIToken = config.getString("geoffrey_api.key"); + baseAPIURL = config.getString("geoffrey_api.api_base_url"); baseURL = config.getString("geoffrey_api.base_url"); + FindCommand findCommand = new FindCommand(this); this.getCommand("geoffrey_find").setExecutor(findCommand); @@ -79,6 +80,9 @@ public final class GeoffreyMC extends JavaPlugin { FindFarmCommand findFarmCommand = new FindFarmCommand(this); this.getCommand("geoffrey_find_farm").setExecutor(findFarmCommand); + + InfoCommand infoCommand = new InfoCommand(this); + this.getCommand("geoffrey_info").setExecutor(infoCommand); } } @@ -103,6 +107,10 @@ public final class GeoffreyMC extends JavaPlugin { return APIToken; } + public String getBaseAPIURL() { + return baseAPIURL; + } + public String getBaseURL() { return baseURL; } diff --git a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyLocation.java b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyLocation.java index d806b9a..e26add7 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyLocation.java +++ b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyLocation.java @@ -2,6 +2,7 @@ package com.zerohighdef.geoffrey.Models; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import org.bukkit.ChatColor; public class GeoffreyLocation { private int xCoord; @@ -10,6 +11,7 @@ public class GeoffreyLocation { private GeoffreyPlayer owner; private String dimension; private String tunnelString = null; + private String link = null; public GeoffreyLocation(JsonObject locationJSON) { this.xCoord = locationJSON.get("x_coord").getAsInt(); @@ -17,6 +19,7 @@ public class GeoffreyLocation { this.locationName = locationJSON.get("name").getAsString(); this.owner = new GeoffreyPlayer(locationJSON.getAsJsonArray("owner").getAsJsonArray().get(0).getAsJsonObject()); this.dimension = locationJSON.get("dimension").getAsString(); + this.link = locationJSON.get("link").getAsString(); JsonElement tunnelElement = locationJSON.get("tunnel"); @@ -47,6 +50,10 @@ public class GeoffreyLocation { return locationString; } + public String getInfoString() { + return ChatColor.BOLD + getFormattedLocationString() + ChatColor.RESET; + } + public int getxCoord() { return xCoord; } @@ -70,4 +77,8 @@ public class GeoffreyLocation { public String getTunnel() { return tunnelString; } + + public String getLink() { + return link; + } } diff --git a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyMarket.java b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyMarket.java new file mode 100644 index 0000000..05e0c66 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyMarket.java @@ -0,0 +1,37 @@ +package com.zerohighdef.geoffrey.Models; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.util.LinkedList; + +public class GeoffreyMarket extends GeoffreyLocation{ + private LinkedList shops; + + public GeoffreyMarket(JsonObject locationJSON) { + super(locationJSON); + this.shops = new LinkedList<>(); + for (JsonElement json : locationJSON.get("shops").getAsJsonArray()) { + this.shops.add(new GeoffreyLocation(json.getAsJsonObject())); + } + } + + public GeoffreyMarket(int xCoord, int zCoord, String locationName, GeoffreyPlayer owner, String dimension, LinkedList shops) { + super(xCoord, zCoord, locationName, owner, dimension); + this.shops = shops; + } + + @Override + public String getInfoString() { + StringBuilder infoString = new StringBuilder(); + infoString.append(super.getInfoString()); + infoString.append("\nShops:"); + + for (GeoffreyLocation shop: shops) { + infoString.append("\n"); + infoString.append(shop.getFormattedLocationString()); + } + + return infoString.toString(); + } +} diff --git a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java index b66dc03..cb30e93 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java +++ b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyShop.java @@ -1,9 +1,11 @@ package com.zerohighdef.geoffrey.Models; +import com.google.common.collect.Iterables; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.awt.event.ItemListener; import java.util.LinkedList; import java.util.List; @@ -35,6 +37,20 @@ public class GeoffreyShop extends GeoffreyLocation{ return items; } + @Override + public String getInfoString() { + StringBuilder infoString = new StringBuilder(); + infoString.append(super.getInfoString()); + infoString.append("\nItems:"); + + for (GeoffreyItemListing item: Iterables.limit(items, 5)) { + infoString.append("\n"); + infoString.append(item.getFormattedItemListing()); + } + + return infoString.toString(); + } + public GeoffreyItemListing getFirstItem() { return items.get(0); } diff --git a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyTown.java b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyTown.java new file mode 100644 index 0000000..7a1a6a9 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyTown.java @@ -0,0 +1,38 @@ +package com.zerohighdef.geoffrey.Models; + +import com.google.common.collect.Iterables; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.util.LinkedList; + +public class GeoffreyTown extends GeoffreyLocation { + private LinkedList residents; + + public GeoffreyTown(JsonObject locationJSON) { + super(locationJSON); + this.residents = new LinkedList<>(); + for (JsonElement json : locationJSON.get("residents").getAsJsonArray()) { + this.residents.add(new GeoffreyPlayer(json.getAsJsonObject())); + } + } + + public GeoffreyTown(int xCoord, int zCoord, String locationName, GeoffreyPlayer owner, String dimension, LinkedList residents) { + super(xCoord, zCoord, locationName, owner, dimension); + this.residents = residents; + } + + @Override + public String getInfoString() { + StringBuilder locationString = new StringBuilder(); + locationString.append(super.getInfoString()); + locationString.append("\nResidents:"); + + for (GeoffreyPlayer resident: Iterables.limit(residents, 5)) { + locationString.append("\n"); + locationString.append(resident.getUsername()); + } + + return locationString.toString(); + } +} diff --git a/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommand.java b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommand.java index ac54358..1ae942d 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyCommand.java @@ -10,7 +10,7 @@ import xyz.etztech.core.web.ICallback; import java.util.Map; public class GeoffreyCommand implements CommandExecutor { - private GeoffreyMC plugin; + protected GeoffreyMC plugin; protected enum Method { GET, POST @@ -21,7 +21,7 @@ public class GeoffreyCommand implements CommandExecutor { } protected void RunCommand(String commandName, Map params, Method requestType, ICallback callback) { - String url = plugin.getBaseURL() + "/command/" + commandName + "/"; + String url = plugin.getBaseAPIURL() + "/command/" + commandName + "/"; params.put("api", plugin.getAPIToken()); if (requestType == Method.GET) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f4a2d96..007b700 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,5 @@ # GeoffreyAPI Settings geoffrey_api: key: '' + api_base_url: '' base_url: '' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1b323a0..0b33dc1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -114,6 +114,10 @@ commands: description: Finds a farm producing a resource aliases: [find_farm] usage: /find_farm [resource name] + geoffrey_info: + description: Gives info on a location + aliases: [info] + usage: /info [location_name] permissions: add: