Committing current progress, not ready for production

This commit is contained in:
Allink 2022-05-09 00:44:18 +01:00
parent 61ab59c9e2
commit 76b2b18fe4
No known key found for this signature in database
GPG Key ID: 77DCA801362E9645
14 changed files with 246 additions and 66 deletions

View File

@ -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>("jar") {
archiveBaseName.set("Plex-ExampleModule")
archiveBaseName.set("Plex-NUSH")
archiveVersion.set("")
}

View File

@ -1,2 +1,2 @@
rootProject.name = "plexmodule-template"
rootProject.name = "Module-NUSH"

View File

@ -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
}
}

View File

@ -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");
}
}

View File

@ -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));
}
}

View File

@ -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", "<aqua>{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;
}
}

View File

@ -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 = "/<command> [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;
}
}

View File

@ -0,0 +1,7 @@
package dev.plex.nush.handler;
import dev.plex.nush.NushModule;
public interface Handler {
void init(NushModule module);
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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<Admin, Integer> 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<Admin, Integer> leastWork = null;
for (Entry<Admin, Integer> adminIntegerEntry : work.entrySet()) {
if(leastWork == null) {
leastWork = adminIntegerEntry;
return;
} else {
if(leastWork.getValue() > adminIntegerEntry.getValue()) {
leastWork = adminIntegerEntry;
}
}
}
}
}

View File

@ -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())
}
}

View File

@ -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<Class<?>> getClassesFrom(String packageName)
{
Set<Class<?>> classes = new HashSet<>();
try
{
ClassPath path = ClassPath.from(Plex.class.getClassLoader());
ImmutableSet<ClassPath.ClassInfo> 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 <T> Set<Class<? extends T>> getClassesBySubType(String packageName, Class<T> subType)
{
Set<Class<?>> loadedClasses = getClassesFrom(packageName);
Set<Class<? extends T>> classes = new HashSet<>();
loadedClasses.forEach(clazz ->
{
if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()).contains(subType))
{
classes.add((Class<? extends T>)clazz);
}
});
return Collections.unmodifiableSet(classes);
}
}

View File

@ -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