Completed codebase

This commit is contained in:
Focusvity 2024-01-26 18:28:23 +11:00
parent a8a696156c
commit 94ff2fa56e
No known key found for this signature in database
GPG key ID: FC3BE6D52CCDB38F
12 changed files with 310 additions and 67 deletions

View file

@ -18,13 +18,13 @@ repositories {
dependencies { dependencies {
compileOnly("org.projectlombok:lombok:1.18.28") compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28") annotationProcessor("org.projectlombok:lombok:1.18.28")
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT") compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("dev.plex:server:1.4-SNAPSHOT") compileOnly("dev.plex:server:1.4-SNAPSHOT")
} }
group = "dev.plex" group = "dev.plex"
version = "1.0" version = "1.4-SNAPSHOT"
description = "ExampleModule" description = "Module-NUSH"
java { java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17)) toolchain.languageVersion.set(JavaLanguageVersion.of(17))
@ -39,7 +39,7 @@ publishing {
} }
tasks.getByName<Jar>("jar") { tasks.getByName<Jar>("jar") {
archiveBaseName.set("Module-ExampleModule") archiveBaseName.set("Module-NUSH")
archiveVersion.set("") archiveVersion.set("")
} }

View file

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

@ -0,0 +1,51 @@
package dev.plex;
import dev.plex.command.NUSHCommand;
import dev.plex.config.ModuleConfig;
import dev.plex.listener.ChatListener;
import dev.plex.listener.JoinListener;
import dev.plex.module.PlexModule;
import lombok.Getter;
public class NUSHModule extends PlexModule
{
@Getter
private static NUSHModule module;
@Getter
private static ModuleConfig config;
@Getter
private static boolean enabled;
@Getter
private static int time;
@Override
public void load()
{
config = new ModuleConfig(this, "nush/config.yml", "config.yml");
}
@Override
public void enable()
{
module = this;
config.load();
enabled = config.getBoolean("server.enabled", false);
time = config.getInt("server.wait_time", 2);
registerCommand(new NUSHCommand());
registerListener(new JoinListener());
registerListener(new ChatListener());
}
@Override
public void disable()
{
module = null;
}
public static void toggle(boolean toggle)
{
enabled = toggle;
config.set("server.enabled", toggle);
}
}

View file

@ -0,0 +1,60 @@
package dev.plex;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class UserData
{
private static final Map<UUID, UserData> USERS_MAP = new HashMap<>();
private BukkitTask task = null;
public UserData(Player player)
{
USERS_MAP.put(player.getUniqueId(), this);
}
public static void queueNewPlayer(Player player)
{
UserData data = new UserData(player);
data.task = Bukkit.getScheduler().runTaskLater(NUSHModule.getModule().getPlex(),
() ->
{
if (data.isValid())
{
data.task.cancel();
USERS_MAP.remove(player.getUniqueId());
}
},
20L * 60L * NUSHModule.getConfig().getInt("server.wait_time", 2));
}
public static boolean isNewPlayer(Player player)
{
return USERS_MAP.containsKey(player.getUniqueId());
}
public static void removePlayer(Player player)
{
USERS_MAP.get(player.getUniqueId()).task.cancel();
USERS_MAP.remove(player.getUniqueId());
}
public static void clear()
{
USERS_MAP.values().stream().filter(UserData::isValid).forEach(data -> data.task.cancel());
USERS_MAP.clear();
}
public boolean isValid()
{
return task != null;
}
}

View file

@ -1,20 +0,0 @@
package dev.plex.command;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
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(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

@ -0,0 +1,113 @@
package dev.plex.command;
import dev.plex.NUSHModule;
import dev.plex.UserData;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@CommandParameters(name = "nush", usage = "/<command> <on | off | status | time <minutes> | remove <player>>", description = "The main command to manage the NUSH module")
@CommandPermissions(permission = "plex.nush.use")
public class NUSHCommand extends PlexCommand
{
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player player, @NotNull String[] args)
{
if (args.length == 1)
{
switch (args[0].toLowerCase())
{
case "on" ->
{
NUSHModule.toggle(true);
return MiniMessage.miniMessage().deserialize("<gray>The status for NUSH is now <green>enabled</green>.");
}
case "off" ->
{
NUSHModule.toggle(false);
UserData.clear();
return MiniMessage.miniMessage().deserialize("<gray>The status for NUSH is now <red>disabled</red>.");
}
case "status" ->
{
return MiniMessage.miniMessage().deserialize("<gray>The status for NUSH is currently " + (NUSHModule.isEnabled() ? "<green>enabled</green>" : "<red>disabled</red>") + ".");
}
default ->
{
return usage();
}
}
}
else if (args.length == 2)
{
switch (args[0].toLowerCase())
{
case "time" ->
{
int time;
try
{
time = Integer.parseInt(args[1]);
}
catch (NumberFormatException ex)
{
return MiniMessage.miniMessage().deserialize("<red>The time must be a number!");
}
NUSHModule.getConfig().set("server.wait_time", time);
return MiniMessage.miniMessage().deserialize("<gray>The wait time for new players before they can chat is now set to <yellow>" + time + "</yellow> minutes.");
}
case "remove" ->
{
final Player target = getNonNullPlayer(args[1]);
if (UserData.isNewPlayer(target))
{
UserData.removePlayer(target);
return MiniMessage.miniMessage().deserialize("<yellow>" + target.getName() + " <gray>has been removed.");
}
else
{
return MiniMessage.miniMessage().deserialize("<red>That player is currently not NUSH'd");
}
}
default ->
{
return usage();
}
}
}
return usage();
}
@Override
public @NotNull List<String> smartTabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
{
if (args.length == 1 && silentCheckPermission(sender, this.getPermission()))
{
return Arrays.asList("on", "off", "status", "time", "remove");
}
if (args.length == 2 && args[0].equalsIgnoreCase("remove") && silentCheckPermission(sender, this.getPermission()))
{
return PlexUtils.getPlayerNameList();
}
return Collections.emptyList();
}
}

View file

@ -0,0 +1,39 @@
package dev.plex.listener;
import dev.plex.NUSHModule;
import dev.plex.UserData;
import dev.plex.util.PlexLog;
import io.papermc.paper.event.player.AsyncChatEvent;
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.EventPriority;
public class ChatListener extends PlexListener
{
@EventHandler(priority = EventPriority.HIGHEST)
public void onChat(AsyncChatEvent event)
{
final Player player = event.getPlayer();
if (!NUSHModule.isEnabled() || event.isCancelled() || !UserData.isNewPlayer(player))
{
PlexLog.debug("NUSH is disabled, event is cancelled or {0} is not on the list", player.getName());
return;
}
PlexLog.debug("Handling event for player {0}", player.getName());
event.setCancelled(true);
player.sendMessage(event.renderer().render(player, player.displayName(), event.message(), player));
Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission("plex.nush.view")).forEach(p ->
{
Component message = Component.empty().append(Component.text("[NUSH] ").color(NamedTextColor.YELLOW))
.append(event.renderer().render(player, player.displayName(), event.message(), p));
p.sendMessage(message);
});
}
}

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.listener;
import dev.plex.NUSHModule;
import dev.plex.UserData;
import dev.plex.util.PlexLog;
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 onJoin(PlayerJoinEvent event)
{
final Player player = event.getPlayer();
if (!player.hasPlayedBefore() && NUSHModule.isEnabled())
{
PlexLog.debug("Adding {0} to the new player list", player.getName());
UserData.queueNewPlayer(player);
Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission("plex.nush.view")).forEach(p ->
p.sendMessage(Component.text("[NUSH] " + player.getName()
+ " has been marked as a new player and won't be able to chat normally for " + NUSHModule.getTime() + " minutes.")
.color(NamedTextColor.LIGHT_PURPLE)));
}
}
}

View file

@ -1,4 +1,4 @@
name: Module-Example name: Module-NUSH
main: dev.plex.ExampleModule main: dev.plex.NUSHModule
description: An example module for Plex description: A module to prevent raids from new players
version: 1.0 version: 1.4-SNAPSHOT

View file

@ -0,0 +1,8 @@
# NUSH Configuration File #
server:
# Should NUSH be enabled or disabled - this is also toggleable via the /nush command
enabled: false
# How many minutes before a new player can chat with other players
wait_time: 5