/* * 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; 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 org.bukkit.command.CommandExecutor; import org.bukkit.event.Listener; import java.lang.reflect.InvocationTargetException; import java.util.function.Consumer; public abstract class TweaksModule { protected abstract void onInit(); void init() { var name = getClass().getSimpleName(); DebugLogger.finer("Initializing " + name); long start = System.nanoTime(); this.onInit(); long end = System.nanoTime(); DebugLogger.fine("Initialized %s in %d µs", name, (end - start) / 1000); } // TODO not static maybe? protected TweaksPlugin getPlugin() { return TweaksPlugin.getInstance(); } protected TweaksConfig getConfig() { return TweaksConfig.getConfig(); } protected void registerEvents(Listener listener) { DebugLogger.finer("Registered listener: " + listener.getClass().getName()); getPlugin().getServer().getPluginManager().registerEvents(listener, getPlugin()); } protected void onPacketSend(PacketType packetType, Consumer consumer) { ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(getPlugin(), ListenerPriority.NORMAL, packetType) { @Override public void onPacketSending(PacketEvent event) { consumer.accept(event); } }); DebugLogger.finer("Registered packet send: " + packetType.name()); } protected void onPacketReceive(PacketType packetType, Consumer consumer) { ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(getPlugin(), ListenerPriority.NORMAL, packetType) { @Override public void onPacketReceiving(PacketEvent event) { consumer.accept(event); } }); DebugLogger.finer("Registered packet receive: " + packetType.name()); } protected void registerCommand(String command, CommandExecutor executor) { getPlugin().getCommand(command).setExecutor(executor); DebugLogger.finer("Registered command: " + command); } public static T init(Class clazz) { T module; try { module = clazz.getDeclaredConstructor().newInstance(); } catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } module.init(); return module; } }