From 3c15b9e770d894cd5b863055f57b69da2aebb8b9 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 29 Dec 2019 11:02:36 -0600 Subject: [PATCH] Added delete_item command --- .../geoffrey/Commands/DeleteItemCommand.java | 80 +++++++++++++++++++ .../com/zerohighdef/geoffrey/GeoffreyMC.java | 3 + src/main/resources/plugin.yml | 5 ++ 3 files changed, 88 insertions(+) create mode 100644 src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java diff --git a/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java b/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java new file mode 100644 index 0000000..5fd4e9e --- /dev/null +++ b/src/main/java/com/zerohighdef/geoffrey/Commands/DeleteItemCommand.java @@ -0,0 +1,80 @@ +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.Models.GeoffreyShop; +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 DeleteItemCommand extends GeoffreyCommand{ + public DeleteItemCommand(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-1); + params.put("shop_name", StringUtils.join(name, " ")); + } + else if (args.length == 0) { + sender.sendMessage(ChatColor.RED + "Too few parameters given."); + return false; + } + + Player player = ((Player) sender).getPlayer(); + String itemName = args[0]; + + params.put("mc_uuid", player.getUniqueId().toString().replace("-", "")); + params.put("item", itemName); + RunCommand("delete_item", params, Method.POST , new CommandCallback(sender, itemName)); + + } + else { + sender.sendMessage(ChatColor.RED + "Console can not add items!"); + } + + return true; + } + + private class CommandCallback extends GeoffreyAPICallback { + String itemName; + + CommandCallback(CommandSender sender, String itemName) { + super(sender); + this.itemName = itemName; + errors.put("LocationLookUpError", "You do not have a shop by that name, goober."); + errors.put("EntryNameNotUniqueError", "You have more than one location. Please specify a name, dingus."); + errors.put("ItemNotFound", "Your shop does not sell " + itemName + ". Try again buddy boi!"); + } + + @Override + public void invoke(String s) { + try { + JsonElement json = parseJSON(s); + + GeoffreyLocation geoffreyLocation = new GeoffreyLocation(json.getAsJsonObject()); + String msg = ChatColor.GREEN + itemName + " has been deleted from " + geoffreyLocation.getLocationName() + "."; + 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 feac043..e212bab 100644 --- a/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java +++ b/src/main/java/com/zerohighdef/geoffrey/GeoffreyMC.java @@ -56,6 +56,9 @@ public final class GeoffreyMC extends JavaPlugin { DeleteCommand deleteCommand = new DeleteCommand(this); this.getCommand("geoffrey_delete").setExecutor(deleteCommand); + + DeleteItemCommand deleteItemCommand = new DeleteItemCommand(this); + this.getCommand("geoffrey_delete_item").setExecutor(deleteItemCommand); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ba88f28..c9d734a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -81,6 +81,11 @@ commands: aliases: [delete] permission: geoffrey.add usage: /delete [location name] + geoffrey_delete_item: + description: Deletes an item from a shop in Geoffrey + aliases: [delete_item] + permission: geoffrey.add + usage: /delete [item name] [shop name] permissions: add: