forked from Geoffrey/Geoffrey-MC-Plugin
94 lines
3.7 KiB
Java
94 lines
3.7 KiB
Java
package com.zerohighdef.geoffrey.Objects;
|
|
|
|
import com.google.common.collect.Iterables;
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
|
|
import com.zerohighdef.geoffrey.Models.GeoffreyShop;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.command.CommandSender;
|
|
import xyz.etztech.core.web.ICallback;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.logging.Logger;
|
|
|
|
public abstract class GeoffreyAPICallback implements ICallback {
|
|
protected CommandSender commandSender;
|
|
protected HashMap<String, String> errors = new HashMap<>();
|
|
private Logger log = Logger.getLogger( "Minecraft" );
|
|
|
|
|
|
public GeoffreyAPICallback(CommandSender commandSender) {
|
|
this.commandSender = commandSender;
|
|
errors.put("PlayerNotFound", "You are not in the database, you must register first!");
|
|
errors.put("NoLocationsInDatabase", "You have no locations in the database, you must add some first!");
|
|
errors.put("DataError", "Slow down their slugger, that's a long word or number you are trying to cram into me, try again with something smaller, please");
|
|
errors.put("LocationHasTunnelError", "That location already has a tunnel you goober.");
|
|
errors.put( "EntryNameNotUniqueError", "That name has already been used, be more creative dingus kong");
|
|
}
|
|
|
|
protected JsonElement parseJSON(String string) throws GeoffreyCommandError {
|
|
JsonElement element = new JsonParser().parse(string);
|
|
|
|
if (element.isJsonObject()) {
|
|
JsonObject object = element.getAsJsonObject();
|
|
|
|
if (object.has("error")) {
|
|
String error = object.get("error").getAsString();
|
|
String errorMessage = "An internal error has occurred.";
|
|
if (errors.containsKey(error)) {
|
|
errorMessage = errors.get(error);
|
|
}
|
|
else {
|
|
log.info("[Geoffrey] " + object.get("error").getAsString() + " " + object.get("error_message").getAsString());
|
|
}
|
|
|
|
commandSender.sendMessage(ChatColor.RED + errorMessage);
|
|
throw new GeoffreyCommandError();
|
|
}
|
|
}
|
|
|
|
return element;
|
|
}
|
|
|
|
protected String locationList(JsonElement jsonElement, int maxLength) {
|
|
JsonArray list = jsonElement.getAsJsonArray();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
for (JsonElement element: Iterables.limit(list, maxLength)) {
|
|
JsonObject jsonObject = element.getAsJsonObject();
|
|
GeoffreyLocation geoffreyLocation = new GeoffreyLocation(jsonObject);
|
|
|
|
stringBuilder.append(geoffreyLocation.getFormattedLocationString());
|
|
stringBuilder.append("\n");
|
|
}
|
|
|
|
return stringBuilder.toString();
|
|
}
|
|
|
|
protected String sellingList(JsonElement jsonElement, int maxLength) {
|
|
JsonArray list = jsonElement.getAsJsonArray();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
for (JsonElement element: Iterables.limit(list, maxLength)) {
|
|
JsonObject jsonObject = element.getAsJsonObject();
|
|
GeoffreyShop geoffreyShop = new GeoffreyShop(jsonObject);
|
|
|
|
stringBuilder.append(geoffreyShop.getLocationName());
|
|
stringBuilder.append(" ");
|
|
stringBuilder.append(geoffreyShop.getPositionString());
|
|
stringBuilder.append(" ");
|
|
stringBuilder.append(geoffreyShop.getFirstItem().getFormattedItemListing());
|
|
stringBuilder.append("\n");
|
|
}
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
|
|
|
@Override
|
|
public abstract void invoke(String s);
|
|
}
|