From 323f5aafa55159f6a7433338fdb031b860b9bfd0 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 21 Dec 2019 15:29:31 -0500 Subject: [PATCH] Added the ability to add locations to Geoffrey + All add locations commands are handled by one executor + Added the ability for commands to make GET or POST requests tot he api --- .../geoffrey/Commands/AddLocationCommand.java | 69 +++++++++++++++++++ .../geoffrey/Commands/FindCommand.java | 14 ++-- .../geoffrey/Commands/GeoffreyCommand.java | 14 +++- .../geoffrey/Commands/SellingCommand.java | 27 +++++--- .../com/zerohighdef/geoffrey/GeoffreyMC.java | 9 +++ src/main/resources/plugin.yml | 37 ++++++++++ 6 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/zerohighdef/geoffrey/Commands/AddLocationCommand.java diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/AddLocationCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/AddLocationCommand.java new file mode 100644 index 0000000..502d64c --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/AddLocationCommand.java @@ -0,0 +1,69 @@ +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.Objects.GeoffreyAPICallback; +import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.Map; + +public class AddLocationCommand extends GeoffreyCommand{ + + public AddLocationCommand(GeoffreyMC plugin) { + super(plugin); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + + if ((sender instanceof Player)) { + String commandName = command.getName().split("_", 2)[1]; + + Map params = new HashMap(); + + if (args.length > 0) { + params.put("name", String.join(" ", args)); + } + + Player player = ((Player) sender).getPlayer(); + + params.put("mc_uuid", ((Player) sender).getUniqueId().toString().replace("-", "")); + params.put("x_pos", Integer.toString(player.getLocation().getBlockX())); + params.put("z_pos", Integer.toString(player.getLocation().getBlockZ())); + RunCommand(commandName, params, Method.POST , new CommandCallback(sender)); + } + else { + sender.sendMessage(ChatColor.RED + "Console can not add locations!"); + } + + return true; + } + + private class CommandCallback extends GeoffreyAPICallback { + + CommandCallback(CommandSender sender) { + super(sender); + errors.put("LocationLookUpError", "You have more than one location. Please specify a name."); + } + + @Override + public void invoke(String s) { + try { + JsonElement json = parseJSON(s); + + GeoffreyLocation location = new GeoffreyLocation(json.getAsJsonObject()); + String msg = ChatColor.GREEN + location.getLocationName() + " has been added to Geoffrey"; + commandSender.sendMessage(msg); + } + catch (GeoffreyCommandError e) { + return; + } + } + } +} diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java index d0d4056..7286f7c 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/FindCommand.java @@ -19,11 +19,17 @@ public class FindCommand extends GeoffreyCommand { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - String locationName = args[0]; - Map params = new HashMap(); - params.put("search", locationName); - RunCommand("find_location", params, new CommandCallback(sender, locationName)); + if (args.length > 0) { + String locationName = String.join(" ", args); + Map params = new HashMap(); + + params.put("search", locationName); + RunCommand("find_location", params, Method.GET, new CommandCallback(sender, locationName)); + } + else { + sender.sendMessage(ChatColor.RED + "You must specify a location or player name."); + } return true; } diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/GeoffreyCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/GeoffreyCommand.java index 070b69f..0d11f48 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/GeoffreyCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/GeoffreyCommand.java @@ -12,15 +12,25 @@ import java.util.Map; public class GeoffreyCommand implements CommandExecutor { private GeoffreyMC plugin; + static enum Method { + GET, POST + } + public GeoffreyCommand(GeoffreyMC plugin) { this.plugin = plugin; } - protected void RunCommand(String commandName, Map params, ICallback callback) { + protected void RunCommand(String commandName, Map params, Method requestType, ICallback callback) { String url = plugin.getBaseURL() + "/command/" + commandName + "/"; params.put("api", plugin.getAPIToken()); - CoreWeb.asyncGetCallback(plugin, url, params, callback); + if (requestType == Method.GET) { + CoreWeb.asyncGetCallback(plugin, url, params, callback); + } + else { + CoreWeb.asyncPostCallback(plugin, url, params, callback); + } + } diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java index 430ccd3..50d83e4 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java @@ -20,21 +20,26 @@ public class SellingCommand extends GeoffreyCommand { @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 (args.length > 0) { + String item = String.join(" ", args); + String commandName; - if (sort.compareToIgnoreCase("price") == 0) { - commandName = "selling_price"; + if (command.getName().contains("price")) { + commandName = "selling_price"; } + else { + commandName = "selling"; + } + + Map params = new HashMap(); + + params.put("item_name", item); + RunCommand(commandName, params, Method.GET ,new CommandCallback(sender, item)); + } + else { + sender.sendMessage(ChatColor.RED + "You must specify what item you are looking to buy!"); } - - Map params = new HashMap(); - - params.put("item_name", item); - RunCommand(commandName, params, new CommandCallback(sender, item)); return true; } diff --git a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java index 8a68906..68195d0 100644 --- a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java +++ b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java @@ -1,5 +1,6 @@ package com.zerohighdef.geoffrey; +import com.zerohighdef.geoffrey.Commands.AddLocationCommand; import com.zerohighdef.geoffrey.Commands.FindCommand; import com.zerohighdef.geoffrey.Commands.SellingCommand; import org.bukkit.Bukkit; @@ -28,6 +29,14 @@ public final class GeoffreyMC extends JavaPlugin { SellingCommand sellingCommand = new SellingCommand(this); this.getCommand("geoffrey_selling").setExecutor(sellingCommand); + this.getCommand("geoffrey_selling_price").setExecutor(sellingCommand); + + AddLocationCommand addLocationCommand = new AddLocationCommand(this); + this.getCommand("geoffrey_add_base").setExecutor(addLocationCommand); + this.getCommand("geoffrey_add_shop").setExecutor(addLocationCommand); + this.getCommand("geoffrey_add_town").setExecutor(addLocationCommand); + this.getCommand("geoffrey_add_farm").setExecutor(addLocationCommand); + this.getCommand("geoffrey_add_attraction").setExecutor(addLocationCommand); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 92328f2..63e3c49 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,3 +10,40 @@ commands: geoffrey_selling: description: Finds an item for sale in Geoffrey aliases: [selling] + geoffrey_selling_price: + description: Finds an item for sale in Geoffrey, sorted by price + aliases: [selling_price] + geoffrey_add_base: + description: Adds a base to Geoffrey + aliases: [add_base] + permission: geoffrey.add + usage: Adds a base at your current location to Geoffrey + geoffrey_add_shop: + description: Adds a shop to Geoffrey + aliases: [add_shop] + permission: geoffrey.add + usage: Adds a shop at your current location to Geoffrey + geoffrey_add_farm: + description: Adds a public farm to Geoffrey + aliases: [add_farm] + permission: geoffrey.add + usage: Adds a farm at your current location to Geoffrey + geoffrey_add_attraction: + description: Adds an attraction to Geoffrey + aliases: [add_attraction] + permission: geoffrey.add + usage: Adds an attraction at your current location to Geoffrey + geoffrey_add_town: + description: Adds a town to Geoffrey + aliases: [add_town] + permission: geoffrey.add + usage: Adds a town at your current location to Geoffrey + geoffrey_add_market: + description: Adds a market to Geoffrey + aliases: [add_market] + permission: geoffrey.add + usage: Adds a market at your current location to Geoffrey + +permissions: + add: + description: Permission to add things to Geoffrey