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

72 lines
2.4 KiB
Java

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.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.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", StringUtils.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;
}
}
}
}