Document TweaksModule
Signed-off-by: Minecon724 <minecon724@noreply.git.m724.eu>
This commit is contained in:
parent
4f752bef61
commit
a8e67dbe26
1 changed files with 52 additions and 4 deletions
|
@ -21,32 +21,60 @@ import java.lang.reflect.InvocationTargetException;
|
|||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class TweaksModule {
|
||||
/**
|
||||
* Called on module initialize.
|
||||
*/
|
||||
protected abstract void onInit();
|
||||
|
||||
void init() {
|
||||
var name = getClass().getSimpleName();
|
||||
DebugLogger.finer("Initializing " + name);
|
||||
DebugLogger.finer("Initializing module " + name);
|
||||
|
||||
long start = System.nanoTime();
|
||||
|
||||
this.onInit();
|
||||
|
||||
long end = System.nanoTime();
|
||||
|
||||
DebugLogger.fine("Initialized %s in %d µs", name, (end - start) / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the plugin instance.
|
||||
*
|
||||
* @return The plugin instance
|
||||
*/
|
||||
protected TweaksPlugin getPlugin() {
|
||||
return TweaksPlugin.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the plugin config.
|
||||
*
|
||||
* @return The plugin config
|
||||
*/
|
||||
protected TweaksConfig getConfig() {
|
||||
return TweaksConfig.getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an event listener.
|
||||
*
|
||||
* @param listener The event listener
|
||||
*/
|
||||
protected void registerEvents(Listener listener) {
|
||||
DebugLogger.finer("Registered listener: " + listener.getClass().getName());
|
||||
getPlugin().getServer().getPluginManager().registerEvents(listener, getPlugin());
|
||||
|
||||
DebugLogger.finer("Registered event listener: " + listener.getClass().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an OUTGOING packet listener.
|
||||
* Priority is {@link ListenerPriority}.NORMAL.
|
||||
*
|
||||
* @param packetType The {@link PacketType} to listen for
|
||||
* @param consumer The consumer that will be called when the packet is received.
|
||||
*/
|
||||
protected void onPacketSend(PacketType packetType, Consumer<PacketEvent> consumer) {
|
||||
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(getPlugin(), ListenerPriority.NORMAL, packetType) {
|
||||
@Override
|
||||
|
@ -55,9 +83,16 @@ public abstract class TweaksModule {
|
|||
}
|
||||
});
|
||||
|
||||
DebugLogger.finer("Registered packet send: " + packetType.name());
|
||||
DebugLogger.finer("Registered outgoing packet listener: " + packetType.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an INCOMING packet listener.
|
||||
* Priority is {@link ListenerPriority}.NORMAL.
|
||||
*
|
||||
* @param packetType The {@link PacketType} to listen for
|
||||
* @param consumer The consumer that will be called when the packet is received.
|
||||
*/
|
||||
protected void onPacketReceive(PacketType packetType, Consumer<PacketEvent> consumer) {
|
||||
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(getPlugin(), ListenerPriority.NORMAL, packetType) {
|
||||
@Override
|
||||
|
@ -66,14 +101,27 @@ public abstract class TweaksModule {
|
|||
}
|
||||
});
|
||||
|
||||
DebugLogger.finer("Registered packet receive: " + packetType.name());
|
||||
DebugLogger.finer("Registered incoming packet listener: " + packetType.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a command.
|
||||
*
|
||||
* @param command The command
|
||||
* @param executor The command executor
|
||||
*/
|
||||
protected void registerCommand(String command, CommandExecutor executor) {
|
||||
getPlugin().getCommand(command).setExecutor(executor);
|
||||
DebugLogger.finer("Registered command: " + command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a module.
|
||||
*
|
||||
* @param clazz The class of the initialized module
|
||||
* @return The module instance
|
||||
* @param <T> The type of the initialized module
|
||||
*/
|
||||
public static <T extends TweaksModule> T init(Class<T> clazz) {
|
||||
T module;
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue