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

62 lines
2.0 KiB
Java

package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonElement;
import com.zerohighdef.geoffrey.GeoffreyMC;
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 java.util.HashMap;
import java.util.Map;
public class FindCommand extends GeoffreyCommand {
public FindCommand(GeoffreyMC plugin) {
super(plugin);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0) {
String locationName = StringUtils.join(args, " ");
Map<String, String> params = new HashMap<String, String>();
params.put("search", locationName);
RunCommand("find_location", params, Method.GET, new CommandCallback(sender, locationName));
}
else {
sender.sendMessage(ChatColor.RED + "You must specify a location or player name.");
}
return true;
}
private class CommandCallback extends GeoffreyAPICallback {
private String locationName;
CommandCallback(CommandSender sender, String locationName) {
super(sender);
errors.put("LocationLookUpError", "There are no locations that match " + locationName);
this.locationName = locationName;
}
@Override
public void invoke(String s) {
try {
JsonElement json = parseJSON(s);
String msg = ChatColor.BOLD + "The following locations match " + locationName + ":\n" + ChatColor.RESET + locationList(json, 5);
commandSender.sendMessage(ChatColor.GREEN + msg);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}