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

65 lines
1.9 KiB
Java

package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zerohighdef.geoffrey.GeoffreyMC;
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import xyz.etztech.core.web.ICallback;
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) {
String locationName = args[0];
Map <String, String> params = new HashMap<String, String>();
params.put("search", locationName);
RunCommand("find_location", params, new CommandCallback((sender)));
return true;
}
protected String formatedCommand(String string) {
JsonArray list = new JsonParser().parse(string).getAsJsonArray();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("The following locations match:\n");
for (JsonElement element: list) {
JsonObject jsonObject = element.getAsJsonObject();
GeoffreyLocation geoffreyLocation = new GeoffreyLocation(jsonObject);
stringBuilder.append(geoffreyLocation.getFormattedLocationString());
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
private class CommandCallback implements ICallback {
private CommandSender sender;
CommandCallback(CommandSender sender) {
this.sender = sender;
}
@Override
public void invoke(String s) {
String msg = formatedCommand(s);
sender.sendMessage(ChatColor.GREEN + msg);
}
}
}