Added add_resource command

+ Added GeoffreyResource model
pull/2/head
Joey Hines 2019-12-26 15:15:16 -06:00
parent 4cf8400347
commit a3c12f8dd6
4 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,75 @@
package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonElement;
import com.zerohighdef.geoffrey.GeoffreyMC;
import com.zerohighdef.geoffrey.Models.GeoffreyResource;
import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError;
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.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class AddResourceCommand extends GeoffreyCommand {
public AddResourceCommand(GeoffreyMC plugin) {
super(plugin);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if ((sender instanceof Player)) {
Map<String, String> params = new HashMap<String, String>();
if (args.length > 1) {
String name[] = Arrays.copyOfRange(args, 1, args.length);
params.put("farm_name", StringUtils.join(name, " "));
}
else if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Too few parameters given.");
return false;
}
Player player = ((Player) sender).getPlayer();
params.put("mc_uuid", player.getUniqueId().toString().replace("-", ""));
params.put("resource_name", args[0]);
RunCommand("add_resource", params, Method.POST , new CommandCallback(sender));
}
else {
sender.sendMessage(ChatColor.RED + "Console can not add items!");
}
return true;
}
private class CommandCallback extends GeoffreyAPICallback {
CommandCallback(CommandSender sender) {
super(sender);
errors.put("LocationLookUpError", "You do not have a farm by that name, goober.");
errors.put("EntryNameNotUniqueError", "You have more than one location. Please specify a name, dingus.");
}
@Override
public void invoke(String s) {
try {
JsonElement json = parseJSON(s);
GeoffreyResource resource = new GeoffreyResource(json.getAsJsonObject());
String msg = ChatColor.GREEN + resource.getName() + " has been added to your farm.";
commandSender.sendMessage(msg);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}

View File

@ -47,6 +47,9 @@ public final class GeoffreyMC extends JavaPlugin {
AddOwnerCommand addOwnerCommand = new AddOwnerCommand(this);
this.getCommand("geoffrey_add_owner").setExecutor(addOwnerCommand);
AddResourceCommand addResourceCommand = new AddResourceCommand(this);
this.getCommand("geoffrey_add_resource").setExecutor(addResourceCommand);
}
}

View File

@ -0,0 +1,19 @@
package com.zerohighdef.geoffrey.Models;
import com.google.gson.JsonObject;
public class GeoffreyResource {
private String name;
public GeoffreyResource(JsonObject resourceJSON) {
this.name = resourceJSON.get("name").getAsString();
}
GeoffreyResource(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -66,6 +66,11 @@ commands:
aliases: [add_owner]
permission: geoffrey.add
usage: /add_owner [new owner name] [location name]
geoffrey_add_resource:
description: Adds a resource to a farm
aliases: [add_resource]
permission: geoffrey.add
usage: /add_resource [resource name] [farm name]
permissions:
add: