From f59072d53485464ef3aefd87b98e421d2cac63d7 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 29 Dec 2019 18:28:42 -0600 Subject: [PATCH] Added missing files --- pom.xml | 6 ++ .../geoffrey/Commands/AddItemCommand.java | 27 ++++-- .../geoffrey/Commands/AddResourceCommand.java | 22 +++-- .../geoffrey/Commands/DeleteItemCommand.java | 22 +++-- .../Commands/DeleteResourceCommand.java | 25 ++++-- .../geoffrey/Commands/EditNameCommand.java | 84 +++++++++++++++++++ .../geoffrey/Commands/SellingCommand.java | 2 +- .../com/zerohighdef/geoffrey/GeoffreyMC.java | 3 + .../geoffrey/Objects/GeoffreyUtil.java | 49 +++++++++++ src/main/resources/plugin.yml | 5 ++ .../geoffrey/Objects/GeoffreyUtilTest.java | 23 +++++ 11 files changed, 245 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/zerohighdef/geoffrey/Commands/EditNameCommand.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtil.java create mode 100644 src/test/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtilTest.java diff --git a/pom.xml b/pom.xml index 40c7685..825d391 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,12 @@ EtzCore 1.0.5 + + junit + junit + RELEASE + test + diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/AddItemCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/AddItemCommand.java index f4ce54a..25bb75d 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/AddItemCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/AddItemCommand.java @@ -6,14 +6,17 @@ import com.zerohighdef.geoffrey.Models.GeoffreyItemListing; 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 org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.text.ParseException; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; public class AddItemCommand extends GeoffreyCommand { @@ -27,20 +30,30 @@ public class AddItemCommand extends GeoffreyCommand { if ((sender instanceof Player)) { Map params = new HashMap(); + List parsedArgs; - if (args.length > 3) { - String name[] = Arrays.copyOfRange(args, 3, args.length-1); - params.put("shop_name", StringUtils.join(name, " ")); + try { + parsedArgs = GeoffreyUtil.parseArgs(args); } - else if (args.length < 3) { + catch (ParseException e) { + sender.sendMessage(ChatColor.RED + e.getMessage()); + return false; + } + + if (parsedArgs.size() > 3) { + String name = StringUtils.join(parsedArgs.subList(3, parsedArgs.size()), " "); + params.put("shop_name", name); + } + else if (parsedArgs.size() < 3) { sender.sendMessage(ChatColor.RED + "Too few parameters given."); return false; } Player player = ((Player) sender).getPlayer(); - String quantity = args[1]; - String diamondPrice = args[2]; + String itemName = parsedArgs.get(0); + String quantity = parsedArgs.get(1); + String diamondPrice = parsedArgs.get(2); try { Integer.parseInt(quantity); @@ -59,7 +72,7 @@ public class AddItemCommand extends GeoffreyCommand { } params.put("mc_uuid", player.getUniqueId().toString().replace("-", "")); - params.put("item_name", args[0]); + params.put("item_name", itemName); params.put("quantity", quantity); params.put("diamond_price", diamondPrice); RunCommand("add_item", params, Method.POST , new CommandCallback(sender)); diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java index acf9e97..63f8090 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java @@ -6,14 +6,17 @@ import com.zerohighdef.geoffrey.Models.GeoffreyResource; 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 org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.text.ParseException; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; public class AddResourceCommand extends GeoffreyCommand { @@ -27,12 +30,21 @@ public class AddResourceCommand extends GeoffreyCommand { if ((sender instanceof Player)) { Map params = new HashMap(); + List parsedArgs; - if (args.length > 1) { - String name[] = Arrays.copyOfRange(args, 1, args.length); - params.put("farm_name", StringUtils.join(name, " ")); + try { + parsedArgs = GeoffreyUtil.parseArgs(args); } - else if (args.length == 0) { + catch (ParseException e) { + sender.sendMessage(ChatColor.RED + e.getMessage()); + return false; + } + + if (parsedArgs.size() > 1) { + String name = StringUtils.join(parsedArgs.subList(1, parsedArgs.size()), " "); + params.put("farm_name", name); + } + else if (parsedArgs.size() < 1) { sender.sendMessage(ChatColor.RED + "Too few parameters given."); return false; } @@ -40,7 +52,7 @@ public class AddResourceCommand extends GeoffreyCommand { Player player = ((Player) sender).getPlayer(); params.put("mc_uuid", player.getUniqueId().toString().replace("-", "")); - params.put("resource_name", args[0]); + params.put("resource_name", parsedArgs.get(0)); RunCommand("add_resource", params, Method.POST , new CommandCallback(sender)); } diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java index 797d8a0..4ceaa81 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java @@ -6,14 +6,17 @@ import com.zerohighdef.geoffrey.Models.GeoffreyLocation; 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 org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.text.ParseException; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; public class DeleteItemCommand extends GeoffreyCommand { @@ -27,18 +30,27 @@ public class DeleteItemCommand extends GeoffreyCommand { if ((sender instanceof Player)) { Map params = new HashMap(); + List parsedArgs; - if (args.length > 1) { - String name[] = Arrays.copyOfRange(args, 1, args.length); - params.put("shop_name", StringUtils.join(name, " ")); + try { + parsedArgs = GeoffreyUtil.parseArgs(args); } - else if (args.length == 0) { + catch (ParseException e) { + sender.sendMessage(ChatColor.RED + e.getMessage()); + return false; + } + + if (parsedArgs.size() > 1) { + String name = StringUtils.join(parsedArgs.subList(1, parsedArgs.size()), " "); + params.put("shop_name", name); + } + else if (parsedArgs.size() == 0) { sender.sendMessage(ChatColor.RED + "Too few parameters given."); return false; } Player player = ((Player) sender).getPlayer(); - String itemName = args[0]; + String itemName = parsedArgs.get(0); params.put("mc_uuid", player.getUniqueId().toString().replace("-", "")); params.put("item", itemName); diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteResourceCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteResourceCommand.java index 47d3103..c71dac3 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteResourceCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteResourceCommand.java @@ -6,14 +6,17 @@ import com.zerohighdef.geoffrey.Models.GeoffreyLocation; 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 org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.text.ParseException; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; public class DeleteResourceCommand extends GeoffreyCommand { @@ -28,20 +31,32 @@ public class DeleteResourceCommand extends GeoffreyCommand { if ((sender instanceof Player)) { Map params = new HashMap(); - if (args.length > 1) { - String name[] = Arrays.copyOfRange(args, 1, args.length); - params.put("farm_name", StringUtils.join(name, " ")); + List parsedArgs; + + try { + parsedArgs = GeoffreyUtil.parseArgs(args); } - else if (args.length == 0) { + catch (ParseException e) { + sender.sendMessage(ChatColor.RED + e.getMessage()); + return false; + } + + if (parsedArgs.size() > 1) { + String name = StringUtils.join(parsedArgs.subList(1, parsedArgs.size()), " "); + params.put("farm_name", name); + } + else if (parsedArgs.size() < 1) { sender.sendMessage(ChatColor.RED + "Too few parameters given."); return false; } Player player = ((Player) sender).getPlayer(); - String resource = args[0]; + + String resource = parsedArgs.get(0); params.put("mc_uuid", player.getUniqueId().toString().replace("-", "")); params.put("resource", resource); + RunCommand("delete_resource", params, Method.POST , new CommandCallback(sender, resource)); } diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/EditNameCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/EditNameCommand.java new file mode 100644 index 0000000..9caa4f4 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/EditNameCommand.java @@ -0,0 +1,84 @@ +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.GeoffreyCommand; +import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError; +import com.zerohighdef.geoffrey.Objects.GeoffreyUtil; +import org.apache.commons.lang.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.text.ParseException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class EditNameCommand extends GeoffreyCommand { + public EditNameCommand(GeoffreyMC plugin) { + super(plugin); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + + if ((sender instanceof Player)) { + List parsedArgs; + try { + parsedArgs = GeoffreyUtil.parseArgs(args); + } + catch (ParseException e) { + sender.sendMessage(ChatColor.RED + e.getMessage()); + return false; + } + Map params = new HashMap(); + + if (parsedArgs.size() < 2) { + sender.sendMessage(ChatColor.RED + "Not enough parameters!"); + return false; + } + + Player player = ((Player) sender).getPlayer(); + + String oldLocationName = parsedArgs.get(0); + String newLocationName = parsedArgs.get(1); + + params.put("mc_uuid", player.getUniqueId().toString().replace("-", "")); + params.put("new_name", oldLocationName); + params.put("loc_name", newLocationName); + RunCommand("edit_name", params, Method.POST , new CommandCallback(sender, oldLocationName, newLocationName)); + } + else { + sender.sendMessage(ChatColor.RED + "Console can not add locations!"); + } + + return true; + } + + private class CommandCallback extends GeoffreyAPICallback { + + CommandCallback(CommandSender sender, String oldLocationName, String newLocationName) { + super(sender); + errors.put("EntryNameNotUniqueError", "A location is already called " + newLocationName + " you ding dong goober"); + errors.put("LocationLookUpError", "You do not have a location by the name " + oldLocationName + " you ding dong goober."); + } + + @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 updated."; + commandSender.sendMessage(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 index 76cacc6..b1644b0 100644 --- a/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/SellingCommand.java @@ -24,7 +24,7 @@ public class SellingCommand extends GeoffreyCommand { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (args.length > 0) { - String item = StringUtils.join(args, " "); + String item = StringUtils.join(args, " ").replace("\"", ""); String commandName; if (command.getName().contains("price")) { diff --git a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java index c5aa121..ce648ac 100644 --- a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java +++ b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java @@ -64,6 +64,9 @@ public final class GeoffreyMC extends JavaPlugin { DeleteResourceCommand deleteResourceCommand = new DeleteResourceCommand(this); this.getCommand("geoffrey_delete_resource").setExecutor(deleteResourceCommand); + + EditNameCommand editNameCommand = new EditNameCommand(this); + this.getCommand("geoffrey_edit_name").setExecutor(editNameCommand); } } diff --git a/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtil.java b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtil.java new file mode 100644 index 0000000..97ff750 --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtil.java @@ -0,0 +1,49 @@ +package com.zerohighdef.geoffrey.Objects; + +import java.text.ParseException; +import java.util.LinkedList; +import java.util.List; + +public class GeoffreyUtil { + public static List parseArgs(String [] args) throws ParseException { + LinkedList parsedArgs = new LinkedList<>(); + boolean parsingMultiWordArg = false; + StringBuilder parsedArg = new StringBuilder(); + + for (String arg: args) { + boolean foundEndOfString = false; + + if (!parsingMultiWordArg) { + parsedArg = new StringBuilder(); + } + + for (char c: arg.toCharArray()) { + if (c == '"') { + if (parsingMultiWordArg) { + foundEndOfString = true; + parsingMultiWordArg = false; + } + else { + parsingMultiWordArg = true; + } + } + } + + parsedArg.append(arg); + + if (parsingMultiWordArg) { + parsedArg.append(" "); + } + + if (foundEndOfString || !parsingMultiWordArg) { + parsedArgs.add(parsedArg.toString().replace("\"", "")); + } + } + + if (parsingMultiWordArg) { + throw new ParseException("No matching \" found", 0); + } + + return parsedArgs; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1fb7a8c..5095682 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -91,6 +91,11 @@ commands: aliases: [delete_resource] permission: geoffrey.add usage: /delete_resource [resource name] [farm name] + geoffrey_edit_name: + description: Edits the name of a location in Geoffrey + aliases: [edit_name] + permission: geoffrey.add + usage: /edit_name [new name] [old name] permissions: add: diff --git a/src/test/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtilTest.java b/src/test/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtilTest.java new file mode 100644 index 0000000..03c0783 --- /dev/null +++ b/src/test/java/com/zerohighdef/geoffrey/Objects/GeoffreyUtilTest.java @@ -0,0 +1,23 @@ +package com.zerohighdef.geoffrey.Objects; + +import org.junit.runner.RunWith; + +import java.util.List; + +import static org.junit.Assert.*; + +public class GeoffreyUtilTest { + + @org.junit.Test + public void parseArgs() { + String testArgs []= {"\"this", "is", "a", "test"}; + List output; + try { + output = GeoffreyUtil.parseArgs(testArgs); + } + catch (Exception e) { + return; + } + System.out.println(output); + } +}