From 76b2b18fe432973e53d40e412d3fd673ee3ef26c Mon Sep 17 00:00:00 2001 From: Allink Date: Mon, 9 May 2022 00:44:18 +0100 Subject: [PATCH] Committing current progress, not ready for production --- build.gradle.kts | 7 ++- settings.gradle.kts | 2 +- src/main/java/dev/plex/ExampleModule.java | 21 ------- .../java/dev/plex/command/ExampleCommand.java | 21 ------- .../dev/plex/listener/ExampleListener.java | 17 ------ src/main/java/dev/plex/nush/NushModule.java | 30 ++++++++++ .../plex/nush/command/impl/NUSHCommand.java | 36 +++++++++++ .../java/dev/plex/nush/handler/Handler.java | 7 +++ .../nush/handler/impl/CommandHandler.java | 13 ++++ .../nush/handler/impl/ListenerHandler.java | 13 ++++ .../plex/nush/listener/impl/ChatListener.java | 50 ++++++++++++++++ .../plex/nush/listener/impl/JoinListener.java | 30 ++++++++++ .../dev/plex/nush/util/ReflectionsUtil.java | 59 +++++++++++++++++++ src/main/resources/module.yml | 6 +- 14 files changed, 246 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/dev/plex/ExampleModule.java delete mode 100644 src/main/java/dev/plex/command/ExampleCommand.java delete mode 100644 src/main/java/dev/plex/listener/ExampleListener.java create mode 100644 src/main/java/dev/plex/nush/NushModule.java create mode 100644 src/main/java/dev/plex/nush/command/impl/NUSHCommand.java create mode 100644 src/main/java/dev/plex/nush/handler/Handler.java create mode 100644 src/main/java/dev/plex/nush/handler/impl/CommandHandler.java create mode 100644 src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java create mode 100644 src/main/java/dev/plex/nush/listener/impl/ChatListener.java create mode 100644 src/main/java/dev/plex/nush/listener/impl/JoinListener.java create mode 100644 src/main/java/dev/plex/nush/util/ReflectionsUtil.java diff --git a/build.gradle.kts b/build.gradle.kts index 20073b3..697aa2c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,12 +19,13 @@ dependencies { compileOnly("org.projectlombok:lombok:1.18.22") annotationProcessor("org.projectlombok:lombok:1.18.22") compileOnly("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT") - compileOnly("dev.plex:Plex:1.0.1-SNAPSHOT") + compileOnly("dev.plex:server:1.1-SNAPSHOT") + compileOnly("dev.plex:api:1.1-SNAPSHOT") } group = "dev.plex" version = "1.0" -description = "ExampleModule" +description = "Stop raiding." java { toolchain.languageVersion.set(JavaLanguageVersion.of(17)) @@ -39,7 +40,7 @@ publishing { } tasks.getByName("jar") { - archiveBaseName.set("Plex-ExampleModule") + archiveBaseName.set("Plex-NUSH") archiveVersion.set("") } diff --git a/settings.gradle.kts b/settings.gradle.kts index 6f497d0..8c6bd0c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,2 +1,2 @@ -rootProject.name = "plexmodule-template" +rootProject.name = "Module-NUSH" diff --git a/src/main/java/dev/plex/ExampleModule.java b/src/main/java/dev/plex/ExampleModule.java deleted file mode 100644 index 5de621d..0000000 --- a/src/main/java/dev/plex/ExampleModule.java +++ /dev/null @@ -1,21 +0,0 @@ -package dev.plex; - -import dev.plex.command.ExampleCommand; -import dev.plex.listener.ExampleListener; -import dev.plex.module.PlexModule; - -public class ExampleModule extends PlexModule -{ - @Override - public void enable() - { - registerCommand(new ExampleCommand()); - registerListener(new ExampleListener()); - } - - @Override - public void disable() - { - // Unregistering listeners / commands is handled by Plex - } -} diff --git a/src/main/java/dev/plex/command/ExampleCommand.java b/src/main/java/dev/plex/command/ExampleCommand.java deleted file mode 100644 index e08610f..0000000 --- a/src/main/java/dev/plex/command/ExampleCommand.java +++ /dev/null @@ -1,21 +0,0 @@ -package dev.plex.command; - -import dev.plex.command.annotation.CommandParameters; -import dev.plex.command.annotation.CommandPermissions; -import dev.plex.rank.enums.Rank; -import net.kyori.adventure.text.Component; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -@CommandParameters(name = "examplemodule", description = "An example command provided by Plex's example module") -@CommandPermissions(level = Rank.OP, permission = "plex.module.command") -public class ExampleCommand extends PlexCommand -{ - @Override - protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] strings) - { - return Component.text("Example module command"); - } -} diff --git a/src/main/java/dev/plex/listener/ExampleListener.java b/src/main/java/dev/plex/listener/ExampleListener.java deleted file mode 100644 index 8f2676f..0000000 --- a/src/main/java/dev/plex/listener/ExampleListener.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.plex.listener; - -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.PlayerJoinEvent; - -public class ExampleListener extends PlexListener -{ - @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) - { - Player player = event.getPlayer(); - player.sendMessage(Component.text("This is a message from Plex's example module!").color(NamedTextColor.GOLD)); - } -} diff --git a/src/main/java/dev/plex/nush/NushModule.java b/src/main/java/dev/plex/nush/NushModule.java new file mode 100644 index 0000000..ddb1fcc --- /dev/null +++ b/src/main/java/dev/plex/nush/NushModule.java @@ -0,0 +1,30 @@ +package dev.plex.nush; + +import dev.plex.nush.handler.impl.CommandHandler; +import dev.plex.nush.handler.impl.ListenerHandler; +import dev.plex.module.PlexModule; + +public class NushModule extends PlexModule +{ + public static boolean enabled = false; + private static NushModule INSTANCE; + + @Override + public void enable() + { + INSTANCE = this; + getPlex().messages.addDefault("nushToggled", "{0} - {1} NUSH."); + new CommandHandler().init(this); + new ListenerHandler().init(this); + } + + @Override + public void disable() + { + // Unregistering listeners / commands is handled by Plex + } + + public static NushModule getInstance() { + return INSTANCE; + } +} diff --git a/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java b/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java new file mode 100644 index 0000000..7fc62c4 --- /dev/null +++ b/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java @@ -0,0 +1,36 @@ +package dev.plex.nush.command.impl; + +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.nush.NushModule; +import dev.plex.rank.enums.Rank; +import dev.plex.util.PlexUtils; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@CommandParameters(name = "nush", aliases = "raidmode", description = "Toggle NUSH on or off.", usage = "/ [on | enable | off | disable | toggle]") +@CommandPermissions(level = Rank.ADMIN, permission = "plex.nush.command") +public class NUSHCommand extends PlexCommand +{ + @Override + protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args) + { + if(args.length == 0) { + NushModule.enabled = !NushModule.enabled; + } else if (args.length > 1) { + switch (args[0].toLowerCase()) { + case "on", "enable" -> NushModule.enabled = true; + case "off", "disable" -> NushModule.enabled = false; + case "toggle" -> NushModule.enabled = !NushModule.enabled; + } + } + + PlexUtils.broadcastToAdmins(messageComponent("nushToggled", commandSender.getName(), NushModule.enabled ? "Enabling" : "Disabling")); + return null; + } +} diff --git a/src/main/java/dev/plex/nush/handler/Handler.java b/src/main/java/dev/plex/nush/handler/Handler.java new file mode 100644 index 0000000..3e937b9 --- /dev/null +++ b/src/main/java/dev/plex/nush/handler/Handler.java @@ -0,0 +1,7 @@ +package dev.plex.nush.handler; + +import dev.plex.nush.NushModule; + +public interface Handler { + void init(NushModule module); +} diff --git a/src/main/java/dev/plex/nush/handler/impl/CommandHandler.java b/src/main/java/dev/plex/nush/handler/impl/CommandHandler.java new file mode 100644 index 0000000..b439e81 --- /dev/null +++ b/src/main/java/dev/plex/nush/handler/impl/CommandHandler.java @@ -0,0 +1,13 @@ +package dev.plex.nush.handler.impl; + +import dev.plex.nush.NushModule; +import dev.plex.nush.command.impl.NUSHCommand; +import dev.plex.nush.handler.Handler; + +public class CommandHandler implements Handler { + + @Override + public void init(NushModule module) { + module.registerCommand(new NUSHCommand()); + } +} diff --git a/src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java b/src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java new file mode 100644 index 0000000..04b5572 --- /dev/null +++ b/src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java @@ -0,0 +1,13 @@ +package dev.plex.nush.handler.impl; + +import dev.plex.nush.NushModule; +import dev.plex.nush.handler.Handler; +import dev.plex.nush.listener.impl.JoinListener; + +public class ListenerHandler implements Handler { + + @Override + public void init(NushModule module) { + module.registerListener(new JoinListener()); + } +} diff --git a/src/main/java/dev/plex/nush/listener/impl/ChatListener.java b/src/main/java/dev/plex/nush/listener/impl/ChatListener.java new file mode 100644 index 0000000..d3e7713 --- /dev/null +++ b/src/main/java/dev/plex/nush/listener/impl/ChatListener.java @@ -0,0 +1,50 @@ +package dev.plex.nush.listener.impl; + +import dev.plex.Plex; +import dev.plex.admin.Admin; +import dev.plex.cache.DataUtils; +import dev.plex.listener.PlexListener; +import dev.plex.nush.NushModule; +import dev.plex.player.PlexPlayer; +import dev.plex.rank.RankManager; +import io.papermc.paper.event.player.AsyncChatEvent; +import java.time.Duration; +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; + +public class ChatListener extends PlexListener { + public static final Map work = new HashMap<>(); + + @EventHandler(priority = EventPriority.HIGHEST) + public void onChat(AsyncChatEvent event) { + Player player = event.getPlayer(); + Instant firstJoined = Instant.ofEpochMilli(player.getFirstPlayed()); + Instant rightNow = Instant.now(); + long difference = (Duration.between(firstJoined, rightNow).getSeconds() / 60); + if(difference >= 15) return; + + NushModule module = NushModule.getInstance(); + Plex plex = module.getPlex(); + PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); + RankManager rankManager = plex.getRankManager(); + + if(rankManager.isAdmin(plexPlayer)) return; // we needn't process the chat message if they're an admin + Entry leastWork = null; + + for (Entry adminIntegerEntry : work.entrySet()) { + if(leastWork == null) { + leastWork = adminIntegerEntry; + return; + } else { + if(leastWork.getValue() > adminIntegerEntry.getValue()) { + leastWork = adminIntegerEntry; + } + } + } + } +} diff --git a/src/main/java/dev/plex/nush/listener/impl/JoinListener.java b/src/main/java/dev/plex/nush/listener/impl/JoinListener.java new file mode 100644 index 0000000..a1aa482 --- /dev/null +++ b/src/main/java/dev/plex/nush/listener/impl/JoinListener.java @@ -0,0 +1,30 @@ +package dev.plex.nush.listener.impl; + +import dev.plex.Plex; +import dev.plex.cache.DataUtils; +import dev.plex.listener.PlexListener; +import dev.plex.nush.NushModule; +import dev.plex.player.PlexPlayer; +import dev.plex.rank.RankManager; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerJoinEvent; + +public class JoinListener extends PlexListener +{ + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) + { + Player player = event.getPlayer(); + NushModule module = NushModule.getInstance(); + Plex plex = module.getPlex(); + PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); + RankManager rankManager = plex.getRankManager(); + + if(!rankManager.isAdmin(plexPlayer)) return; // we only want to add admins + if(ChatListener.work.containsKey()) + } +} diff --git a/src/main/java/dev/plex/nush/util/ReflectionsUtil.java b/src/main/java/dev/plex/nush/util/ReflectionsUtil.java new file mode 100644 index 0000000..57cb46a --- /dev/null +++ b/src/main/java/dev/plex/nush/util/ReflectionsUtil.java @@ -0,0 +1,59 @@ +package dev.plex.nush.util; + +import com.google.common.collect.ImmutableSet; +import com.google.common.reflect.ClassPath; +import dev.plex.Plex; + +import dev.plex.util.PlexLog; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class ReflectionsUtil +{ + @SuppressWarnings("UnstableApiUsage") + public static Set> getClassesFrom(String packageName) + { + Set> classes = new HashSet<>(); + try + { + ClassPath path = ClassPath.from(Plex.class.getClassLoader()); + ImmutableSet infoSet = path.getTopLevelClasses(packageName); + infoSet.forEach(info -> + { + try + { + Class clazz = Class.forName(info.getName()); + classes.add(clazz); + } + catch (ClassNotFoundException ex) + { + PlexLog.error("Unable to find class " + info.getName() + " in " + packageName); + } + }); + } + catch (IOException ex) + { + PlexLog.error("Something went wrong while fetching classes from " + packageName); + throw new RuntimeException(ex); + } + return Collections.unmodifiableSet(classes); + } + + @SuppressWarnings("unchecked") + public static Set> getClassesBySubType(String packageName, Class subType) + { + Set> loadedClasses = getClassesFrom(packageName); + Set> classes = new HashSet<>(); + loadedClasses.forEach(clazz -> + { + if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()).contains(subType)) + { + classes.add((Class)clazz); + } + }); + return Collections.unmodifiableSet(classes); + } +} \ No newline at end of file diff --git a/src/main/resources/module.yml b/src/main/resources/module.yml index 6392159..4217258 100644 --- a/src/main/resources/module.yml +++ b/src/main/resources/module.yml @@ -1,4 +1,4 @@ -name: ExampleModule -main: dev.plex.ExampleModule -description: An example module for Plex +name: NushModule +main: dev.plex.nush.NushModule +description: Stops raiding in its tracks. version: 1.0 \ No newline at end of file