2019-12-19 21:21:51 +00:00
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 ;
2019-12-24 23:12:09 +00:00
import java.util.logging.Logger ;
2019-12-19 21:21:51 +00:00
public abstract class GeoffreyAPICallback implements ICallback {
protected CommandSender commandSender ;
protected HashMap < String , String > errors = new HashMap < > ( ) ;
2019-12-24 23:12:09 +00:00
private Logger log = Logger . getLogger ( "Minecraft" ) ;
2019-12-19 21:21:51 +00:00
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" ) ;
2019-12-29 16:46:04 +00:00
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" ) ;
2019-12-19 21:21:51 +00:00
}
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 ) ;
}
2019-12-24 23:12:09 +00:00
else {
log . info ( "[Geoffrey] " + object . get ( "error" ) . getAsString ( ) + " " + object . get ( "error_message" ) . getAsString ( ) ) ;
}
2019-12-19 21:21:51 +00:00
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 ) ;
2020-08-04 01:06:41 +00:00
stringBuilder . append ( geoffreyShop . getFormattedLocationString ( ) ) ;
stringBuilder . append ( "\n" ) ;
2023-08-30 02:25:26 +00:00
stringBuilder . append ( geoffreyShop . getInfoString ( ) . getFormattedItemListing ( ) ) ;
2019-12-19 21:21:51 +00:00
stringBuilder . append ( "\n" ) ;
}
return stringBuilder . toString ( ) ;
}
@Override
public abstract void invoke ( String s ) ;
}