2016-05-12 19:40:39 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.httpd;
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
private final boolean async;
|
|
|
|
|
|
|
|
public ModuleExecutable(boolean async)
|
|
|
|
{
|
|
|
|
this.async = async;
|
|
|
|
}
|
|
|
|
|
|
|
|
public NanoHTTPD.Response execute(final NanoHTTPD.HTTPSession session)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (async)
|
|
|
|
{
|
|
|
|
return getResponse(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync to server thread
|
2021-01-05 00:27:57 +00:00
|
|
|
return Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.getPlugin(), () -> getResponse(session)).get();
|
2016-05-12 19:40:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract NanoHTTPD.Response getResponse(NanoHTTPD.HTTPSession session);
|
|
|
|
|
|
|
|
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-30 03:37:50 +00:00
|
|
|
public boolean isAsync()
|
|
|
|
{
|
|
|
|
return async;
|
|
|
|
}
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|