tweaks724/src/main/java/eu/m724/tweaks/module/TweaksModule.java
Minecon724 a8e67dbe26
Document TweaksModule
Signed-off-by: Minecon724 <minecon724@noreply.git.m724.eu>
2025-05-14 19:06:04 +02:00

137 lines
4.2 KiB
Java

/*
* Copyright (C) 2025 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.module;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import eu.m724.tweaks.DebugLogger;
import eu.m724.tweaks.config.TweaksConfig;
import eu.m724.tweaks.TweaksPlugin;
import org.bukkit.command.CommandExecutor;
import org.bukkit.event.Listener;
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 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) {
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
public void onPacketSending(PacketEvent event) {
consumer.accept(event);
}
});
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
public void onPacketReceiving(PacketEvent event) {
consumer.accept(event);
}
});
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 {
module = clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
module.init();
return module;
}
}