From a3c12f8dd65802107b793085cbc08c92b5102012 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Thu, 26 Dec 2019 15:15:16 -0600 Subject: [PATCH] Added add_resource command + Added GeoffreyResource model --- .../geoffrey/Commands/AddResourceCommand.java | 75 +++++++++++++++++++ .../com/zerohighdef/geoffrey/GeoffreyMC.java | 3 + .../geoffrey/Models/GeoffreyResource.java | 19 +++++ src/main/resources/plugin.yml | 5 ++ 4 files changed, 102 insertions(+) create mode 100644 src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java create mode 100644 src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyResource.java diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java new file mode 100644 index 0000000..1eeb90d --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/AddResourceCommand.java @@ -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 params = new HashMap(); + + 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; + } + } + } +} diff --git a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java index 34c3d56..c4e2d47 100644 --- a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java +++ b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java @@ -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); } } diff --git a/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyResource.java b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyResource.java new file mode 100644 index 0000000..d48ca9f --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Models/GeoffreyResource.java @@ -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; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a5871e5..16cbd15 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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: