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
master
Joey Hines 2019-12-21 15:29:31 -05:00
parent 1eaefc26bf
commit 323f5aafa5
6 changed files with 153 additions and 17 deletions

View File

@ -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<String, String> params = new HashMap<String, String>();
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;
}
}
}
}

View File

@ -19,11 +19,17 @@ public class FindCommand extends GeoffreyCommand {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String locationName = args[0];
if (args.length > 0) {
String locationName = String.join(" ", args);
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("search", locationName); params.put("search", locationName);
RunCommand("find_location", params, new CommandCallback(sender, 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; return true;
} }

View File

@ -12,16 +12,26 @@ import java.util.Map;
public class GeoffreyCommand implements CommandExecutor { public class GeoffreyCommand implements CommandExecutor {
private GeoffreyMC plugin; private GeoffreyMC plugin;
static enum Method {
GET, POST
}
public GeoffreyCommand(GeoffreyMC plugin) { public GeoffreyCommand(GeoffreyMC plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
protected void RunCommand(String commandName, Map<String, String> params, ICallback callback) { protected void RunCommand(String commandName, Map<String, String> params, Method requestType, ICallback callback) {
String url = plugin.getBaseURL() + "/command/" + commandName + "/"; String url = plugin.getBaseURL() + "/command/" + commandName + "/";
params.put("api", plugin.getAPIToken()); params.put("api", plugin.getAPIToken());
if (requestType == Method.GET) {
CoreWeb.asyncGetCallback(plugin, url, params, callback); CoreWeb.asyncGetCallback(plugin, url, params, callback);
} }
else {
CoreWeb.asyncPostCallback(plugin, url, params, callback);
}
}
@Override @Override

View File

@ -20,21 +20,26 @@ public class SellingCommand extends GeoffreyCommand {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String item = args[0];
String commandName = "selling";
if (args.length > 1) { if (args.length > 0) {
String sort = args[1]; String item = String.join(" ", args);
String commandName;
if (sort.compareToIgnoreCase("price") == 0) { if (command.getName().contains("price")) {
commandName = "selling_price"; commandName = "selling_price";
} }
else {
commandName = "selling";
} }
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("item_name", item); params.put("item_name", item);
RunCommand(commandName, params, new CommandCallback(sender, 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!");
}
return true; return true;
} }

View File

@ -1,5 +1,6 @@
package com.zerohighdef.geoffrey; package com.zerohighdef.geoffrey;
import com.zerohighdef.geoffrey.Commands.AddLocationCommand;
import com.zerohighdef.geoffrey.Commands.FindCommand; import com.zerohighdef.geoffrey.Commands.FindCommand;
import com.zerohighdef.geoffrey.Commands.SellingCommand; import com.zerohighdef.geoffrey.Commands.SellingCommand;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -28,6 +29,14 @@ public final class GeoffreyMC extends JavaPlugin {
SellingCommand sellingCommand = new SellingCommand(this); SellingCommand sellingCommand = new SellingCommand(this);
this.getCommand("geoffrey_selling").setExecutor(sellingCommand); 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);
} }
} }

View File

@ -10,3 +10,40 @@ commands:
geoffrey_selling: geoffrey_selling:
description: Finds an item for sale in Geoffrey description: Finds an item for sale in Geoffrey
aliases: [selling] 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