Geoffrey-MC-Plugin/src/main/java/com/zerohighdef/geoffrey/Commands/AddTunnelCommand.java

110 lines
3.8 KiB
Java

package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.zerohighdef.geoffrey.GeoffreyMC;
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
import com.zerohighdef.geoffrey.Models.GeoffreyTunnel;
import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommand;
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 AddTunnelCommand extends GeoffreyCommand {
public AddTunnelCommand(GeoffreyMC plugin) {
super(plugin);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String commmandName;
if (label.contains("edit")) {
commmandName = "edit_tunnel";
}
else {
commmandName = "add_tunnel";
}
if ((sender instanceof Player)) {
Map<String, String> params = new HashMap<String, String>();
if (args.length > 2) {
String name[] = Arrays.copyOfRange(args, 2, args.length);
// this is a hack
String paramName = (commmandName.compareTo("edit_tunnel") == 0) ? "loc_name" : "location_name";
params.put(paramName, StringUtils.join(name, " "));
}
else {
sender.sendMessage(ChatColor.RED + "Too few parameters given.");
return false;
}
Player player = ((Player) sender).getPlayer();
try {
Integer.parseInt(args[1]);
}
catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Tunnel number must be an integer value!");
return false;
}
String tunnelDirection = args[0];
String tunnelNumber = args[1];
params.put("mc_uuid", player.getUniqueId().toString().replace("-", ""));
params.put("tunnel_direction", tunnelDirection);
params.put("tunnel_number", tunnelNumber);
RunCommand(commmandName, params, GeoffreyCommand.Method.POST , new CommandCallback(sender, tunnelDirection));
}
else {
sender.sendMessage(ChatColor.RED + "Console can not add tunnels!");
}
return true;
}
private class CommandCallback extends GeoffreyAPICallback {
CommandCallback(CommandSender sender, String tunnelDirection) {
super(sender);
errors.put("InvalidTunnelError", tunnelDirection + " is not a valid tunnel direction ya gub.");
errors.put("LocationLookUpError", "You do not have a location 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);
JsonObject jsonObj = json.getAsJsonObject();
String locationName;
if (jsonObj.has("type")) {
locationName = new GeoffreyLocation(jsonObj).getLocationName();
}
else {
locationName = new GeoffreyTunnel(jsonObj).getLocationName();
}
String msg = ChatColor.GREEN + "The tunnel has been updated for " + locationName;
commandSender.sendMessage(msg);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}