2016-05-12 19:40:39 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.httpd;
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
2020-12-25 19:46:43 +00:00
|
|
|
import java.util.Objects;
|
2016-05-12 19:40:39 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
|
|
|
import me.totalfreedom.totalfreedommod.httpd.module.HTTPDModule;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
|
|
|
public abstract class ModuleExecutable
|
|
|
|
{
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
private final boolean async;
|
|
|
|
|
|
|
|
public ModuleExecutable(boolean async)
|
|
|
|
{
|
|
|
|
this.async = async;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ModuleExecutable forClass(final TotalFreedomMod plugin, Class<? extends HTTPDModule> clazz, boolean async)
|
|
|
|
{
|
|
|
|
final Constructor<? extends HTTPDModule> cons;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cons = clazz.getConstructor(TotalFreedomMod.class, NanoHTTPD.HTTPSession.class);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
throw new IllegalArgumentException("Improperly defined module!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ModuleExecutable(async)
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public NanoHTTPD.Response getResponse(NanoHTTPD.HTTPSession session)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return cons.newInstance(plugin, session).getResponse();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
public NanoHTTPD.Response execute(final NanoHTTPD.HTTPSession session)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (async)
|
|
|
|
{
|
|
|
|
return getResponse(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync to server thread
|
|
|
|
return Bukkit.getScheduler().callSyncMethod(Objects.requireNonNull(TotalFreedomMod.plugin()), () -> getResponse(session)).get();
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract NanoHTTPD.Response getResponse(NanoHTTPD.HTTPSession session);
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|