Moar, not redy for production

This commit is contained in:
Allink 2022-05-10 15:37:13 +01:00
parent ddb5f9ed43
commit 35a5ffe71c
No known key found for this signature in database
GPG Key ID: 77DCA801362E9645
9 changed files with 268 additions and 91 deletions

View File

@ -0,0 +1,14 @@
package dev.plex.nush;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.text.Component;
@Data
@AllArgsConstructor
public class Message {
UUID sender;
Component message;
}

View File

@ -0,0 +1,30 @@
package dev.plex.nush;
import javax.annotation.Nullable;
public enum NushAction {
MUTE("Mute Player", 0),
CANCEL("Cancel", 1),
SMITE("Smite", 2),
BAN("Ban Player", 3),
ACCEPT("Accept", 4);
public final String humanReadable;
public final int ordinal;
NushAction(String humanReadable, int ordinal) {
this.humanReadable = humanReadable;
this.ordinal = ordinal;
}
@Nullable
public static NushAction fromOrdinal(int ordinal) {
for (NushAction value : NushAction.values()) {
if (value.ordinal == ordinal) {
return value;
}
}
return null;
}
}

View File

@ -1,30 +1,38 @@
package dev.plex.nush;
import dev.plex.Plex;
import dev.plex.module.PlexModule;
import dev.plex.nush.handler.impl.CommandHandler;
import dev.plex.nush.handler.impl.ListenerHandler;
import dev.plex.module.PlexModule;
import java.util.Map;
import java.util.Map.Entry;
public class NushModule extends PlexModule
{
public static boolean enabled = false;
private static NushModule INSTANCE;
public class NushModule extends PlexModule {
@Override
public void enable()
{
INSTANCE = this;
getPlex().messages.addDefault("nushToggled", "<aqua>{0} - {1} NUSH.");
new CommandHandler().init(this);
new ListenerHandler().init(this);
}
public static final Map<String, String> messages = Map.ofEntries(
Map.entry("nushToggled", "<aqua>{0} - {1} NUSH."),
Map.entry("nushApply", "<yellow>Applying {0} to {1}!"));
public static boolean enabled = false;
private static NushModule INSTANCE;
@Override
public void disable()
{
// Unregistering listeners / commands is handled by Plex
}
public static NushModule getInstance() {
return INSTANCE;
}
public static NushModule getInstance() {
return INSTANCE;
}
@Override
public void enable() {
INSTANCE = this;
Plex plex = getPlex();
for (Entry<String, String> entry : messages.entrySet()) {
plex.messages.addDefault(entry.getKey(), entry.getValue());
}
new CommandHandler().init(this);
new ListenerHandler().init(this);
}
@Override
public void disable() {
// Unregistering listeners / commands is handled by Plex
}
}

View File

@ -3,11 +3,16 @@ 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.Message;
import dev.plex.nush.NushAction;
import dev.plex.nush.NushModule;
import dev.plex.nush.handler.impl.ActionHandler;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -15,22 +20,56 @@ 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;
}
}
public class NUSHCommand extends PlexCommand {
PlexUtils.broadcastToAdmins(messageComponent("nushToggled", commandSender.getName(), NushModule.enabled ? "Enabling" : "Disabling"));
return null;
}
@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;
}
} else {
if (args[0].equalsIgnoreCase("work")) {
try {
UUID nushIdentifier = UUID.fromString(args[0]);
Message nushMessage = ActionHandler.MAP.get(nushIdentifier);
NushAction action = NushAction.fromOrdinal(Integer.parseInt(args[2]));
if (action == null) {
return null;
}
StringBuilder command = new StringBuilder();
switch (action) {
case ACCEPT:
case CANCEL:
break;
default:
command.append(action.name().toLowerCase());
break;
}
command.append(" ").append(nushMessage.getSender());
if (!command.toString().trim().isEmpty()) {
Bukkit.dispatchCommand(commandSender, command.toString());
}
ActionHandler.resolve(nushIdentifier, action);
return Component.text(action.humanReadable, NamedTextColor.YELLOW);
} catch (Exception ignored) {
return null;
}
}
return null;
}
PlexUtils.broadcastToAdmins(messageComponent("nushToggled", commandSender.getName(),
NushModule.enabled ? "Enabling" : "Disabling"));
return null;
}
}

View File

@ -3,5 +3,6 @@ package dev.plex.nush.handler;
import dev.plex.nush.NushModule;
public interface Handler {
void init(NushModule module);
}

View File

@ -0,0 +1,77 @@
package dev.plex.nush.handler.impl;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.nush.Message;
import dev.plex.nush.NushAction;
import dev.plex.nush.NushModule;
import dev.plex.nush.handler.Handler;
import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import org.bukkit.Bukkit;
public class ActionHandler implements Handler {
public static final Map<UUID, Message> MAP = new HashMap<>();
private final static TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig.builder()
.match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
.replacement(
(matchResult, builder) -> Component.empty().content(matchResult.group()).clickEvent(
ClickEvent.openUrl(matchResult.group()))).build();
private static NushModule MODULE;
public static void resolve(UUID uuid, NushAction action) {
Message message = MAP.get(uuid);
if (message == null) {
return;
}
if (action == NushAction.ACCEPT) {
Audience.audience(Bukkit.getServer())
.sendMessage(Identity.identity(message.getSender()), getMessage(message));
}
MAP.remove(uuid);
}
public static Component getMessage(Message message) {
String text = PlexUtils.getTextFromComponent(message.getMessage());
Plex plex = MODULE.getPlex();
PlexPlayer plexPlayer = DataUtils.getPlayer(message.getSender());
Component prefix = plex.getRankManager().getPrefix(plexPlayer);
Component component = Component.empty();
if (prefix != null) {
component = component.append(prefix);
}
return component.append(Component.space()).append(
PlexUtils.mmDeserialize(plex.config.getString("chat.name-color", "<white>") +
MiniMessage.builder().tags(
TagResolver.resolver(StandardTags.color(), StandardTags.rainbow(),
StandardTags.decorations(), StandardTags.gradient(),
StandardTags.transition()
)).build().serialize(plexPlayer.getPlayer().displayName())))
.append(Component.space())
.append(Component.text("»").color(NamedTextColor.GRAY)).append(Component.space())
.append(
SafeMiniMessage.mmDeserializeWithoutEvents(text))
.replaceText(URL_REPLACEMENT_CONFIG);
}
@Override
public void init(NushModule module) {
MODULE = NushModule.getInstance();
}
}

View File

@ -1,24 +1,28 @@
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.Message;
import dev.plex.nush.NushAction;
import dev.plex.nush.NushModule;
import dev.plex.nush.handler.impl.ActionHandler;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.RankManager;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
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 java.util.UUID;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
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) {
@ -26,25 +30,39 @@ public class ChatListener extends PlexListener {
Instant firstJoined = Instant.ofEpochMilli(player.getFirstPlayed());
Instant rightNow = Instant.now();
long difference = (Duration.between(firstJoined, rightNow).getSeconds() / 60);
if(difference >= 15) return;
if (difference >= 15) {
PlexLog.debug(player.getName(), "has been on the server for", difference,
"minutes, so Nush will skip them.");
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;
}
}
if (rankManager.isAdmin(plexPlayer)) {
PlexLog.debug(player.getName(), "is an admin so Nush will skip them.");
return; // we needn't process the chat message if they're an admin
}
event.setCancelled(true);
UUID key = UUID.randomUUID();
Message message = new Message(event.getPlayer().getUniqueId(), event.originalMessage());
ActionHandler.MAP.put(key, message);
Component component = ActionHandler.getMessage(message);
for (NushAction value : NushAction.values()) {
String command = String.format("/nush work %s %d", key, value.ordinal);
component = component.append(
Component.text(String.format("[%s] ", value.humanReadable))
.clickEvent(ClickEvent.runCommand(command))
.hoverEvent(
Component.text(command, NamedTextColor.YELLOW)
)
);
}
PlexUtils.broadcast(component);
}
}

View File

@ -10,24 +10,22 @@ 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();
public class JoinListener extends PlexListener {
if (!rankManager.isAdmin(plexPlayer))
{
return; // we only want to add admins
}
@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

@ -3,7 +3,6 @@ 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;
@ -11,31 +10,24 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class ReflectionsUtil
{
public class ReflectionsUtil {
@SuppressWarnings("UnstableApiUsage")
public static Set<Class<?>> getClassesFrom(String packageName)
{
public static Set<Class<?>> getClassesFrom(String packageName) {
Set<Class<?>> classes = new HashSet<>();
try
{
try {
ClassPath path = ClassPath.from(Plex.class.getClassLoader());
ImmutableSet<ClassPath.ClassInfo> infoSet = path.getTopLevelClasses(packageName);
infoSet.forEach(info ->
{
try
{
try {
Class<?> clazz = Class.forName(info.getName());
classes.add(clazz);
}
catch (ClassNotFoundException ex)
{
} catch (ClassNotFoundException ex) {
PlexLog.error("Unable to find class " + info.getName() + " in " + packageName);
}
});
}
catch (IOException ex)
{
} catch (IOException ex) {
PlexLog.error("Something went wrong while fetching classes from " + packageName);
throw new RuntimeException(ex);
}
@ -43,15 +35,15 @@ public class ReflectionsUtil
}
@SuppressWarnings("unchecked")
public static <T> Set<Class<? extends T>> getClassesBySubType(String packageName, Class<T> subType)
{
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);
if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces())
.contains(subType)) {
classes.add((Class<? extends T>) clazz);
}
});
return Collections.unmodifiableSet(classes);