This commit is contained in:
Paul Reilly 2023-06-21 15:45:55 -05:00
parent 09699ccabb
commit fb97a8e65b
12 changed files with 267 additions and 56 deletions

View File

@ -1,12 +1,19 @@
package me.totalfreedom.datura.cmd;
import me.totalfreedom.base.Patchwork;
import me.totalfreedom.command.Commander;
import me.totalfreedom.command.annotation.Base;
import me.totalfreedom.command.annotation.Info;
import me.totalfreedom.command.annotation.Permissive;
import me.totalfreedom.command.annotation.Subcommand;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
@Info(name = "")
@Permissive(perm = "")
public class AdminChatCommand extends Commander
{
@ -25,7 +32,33 @@ public class AdminChatCommand extends Commander
}
@Base
public void onAdminChat(final CommandSender sender) {
public void onAdminChat(final CommandSender sender)
{
if (!(sender instanceof Player)) return;
final Player player = (Player) sender;
Patchwork.getInstance()
.getAdminChatDisplay()
.toggleChat(player);
final boolean toggled = Patchwork.getInstance()
.getAdminChatDisplay()
.isToggled(player);
String message = "Toggled adminchat ";
message += toggled ? "on" : "off";
player.sendPlainMessage(message + ".");
}
// String here will automatically have all additional args appended to it :)
@Subcommand(permission = "patchwork.adminchat", args = {String.class})
public void sendMessage(final CommandSender sender, final String message)
{
Patchwork.getInstance()
.getAdminChatDisplay()
.adminChatMessage(sender, Component.text(message));
}
}

View File

@ -21,6 +21,7 @@
package me.totalfreedom.datura.cmd;
import me.totalfreedom.base.Shortcuts;
import me.totalfreedom.command.Commander;
import me.totalfreedom.command.annotation.Completion;
import me.totalfreedom.command.annotation.Info;
@ -68,13 +69,15 @@ public class CageCommand extends Commander
{
if (string.equalsIgnoreCase("on"))
{
((Datura) getPlugin()).getCager()
.cagePlayer(player.getUniqueId(), material);
Shortcuts.provideModule(Datura.class)
.getCager()
.cagePlayer(player.getUniqueId(), material);
sender.sendPlainMessage("Caged " + player.getName() + ".");
} else if (string.equalsIgnoreCase("off"))
{
((Datura) getPlugin()).getCager()
.uncagePlayer(player.getUniqueId());
Shortcuts.provideModule(Datura.class)
.getCager()
.uncagePlayer(player.getUniqueId());
sender.sendPlainMessage("Liberated " + player.getName() + ".");
}
}

View File

@ -2,6 +2,7 @@ package me.totalfreedom.datura.cmd;
import me.totalfreedom.base.Shortcuts;
import me.totalfreedom.command.Commander;
import me.totalfreedom.command.annotation.Completion;
import me.totalfreedom.command.annotation.Info;
import me.totalfreedom.command.annotation.Permissive;
import me.totalfreedom.command.annotation.Subcommand;
@ -18,8 +19,7 @@ import org.jetbrains.annotations.NotNull;
@Permissive(perm = "datura.halt")
public class HaltCommand extends Commander
{
private final Datura plugin = Shortcuts.provideModule(Datura.class)
.getModule();
private final Datura plugin = Shortcuts.provideModule(Datura.class);
/**
* Initializes this command object. The provided {@link JavaPlugin} should be the plugin which contains the
@ -36,6 +36,8 @@ public class HaltCommand extends Commander
}
@Completion(index = 0, args = {"%player%", "all"})
@Completion(index = 1, args = {"on", "off"})
@Subcommand(permission = "datura.halt", args = {Player.class, String.class})
public void haltPlayer(final CommandSender sender, final Player target, final String toggle)
{
@ -56,6 +58,7 @@ public class HaltCommand extends Commander
}
}
// No completion needed here since it's already registered.
@Subcommand(permission = "datura.halt.all", args = {String.class, String.class})
public void haltAll(final CommandSender sender, final String all, final String toggle)
{
@ -65,11 +68,8 @@ public class HaltCommand extends Commander
{
Bukkit.getServer()
.getOnlinePlayers()
.forEach(player ->
{
plugin.getHalter()
.halt(player.getUniqueId());
});
.forEach(player -> plugin.getHalter()
.halt(player.getUniqueId()));
final Component message = sender.name()
.append(Component.text(": Freezing all players"))

View File

@ -0,0 +1,51 @@
package me.totalfreedom.datura.cmd;
import me.totalfreedom.command.Commander;
import me.totalfreedom.command.annotation.Completion;
import me.totalfreedom.command.annotation.Info;
import me.totalfreedom.command.annotation.Permissive;
import me.totalfreedom.command.annotation.Subcommand;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
@Info(name = "smite", description = "Smite a player.", usage = "/smite <player>", aliases = {"sm"})
@Permissive(perm = "datura.smite")
public class SmiteCommand extends Commander
{
/**
* Initializes this command object. The provided {@link JavaPlugin} should be the plugin which contains the
* command.
* <p>
* This constructor will automatically register all subcommands and completions for this command. It will also
* automatically infer all required information from the provided {@link Info} and {@link Permissive} annotations.
*
* @param plugin The plugin which contains this command.
*/
protected SmiteCommand(@NotNull final JavaPlugin plugin)
{
super(plugin);
}
@Subcommand(permission = "datura.smite", args = {Player.class})
@Completion(index = 0, args = {"%player%"})
public void smite(final CommandSender sender, final Player player)
{
final double size = 5D;
for (int i = 0; i < size * size * size; i++)
{
final double x = i % size;
final double y = (i / size) % size;
final double z = (i / size / size) % size;
final Location location = player.getLocation()
.clone()
.add(x - size / 2, y - size / 2, z - size / 2);
player.getWorld()
.strikeLightning(location);
}
}
}

View File

@ -27,7 +27,8 @@ public class Halter implements Listener
this.haltedPlayers.remove(uuid);
}
public void clear() {
public void clear()
{
this.haltedPlayers.clear();
}

View File

@ -230,16 +230,16 @@ public class SimpleUserData implements UserData
return hasCustomACFormat;
}
@Override
public AdminChatFormat getCustomACFormat()
{
return AdminChatFormat.deserialize(customACFormat);
}
@Override
public void setCustomACFormat(final String format)
{
this.hasCustomACFormat = format.equals(AdminChatFormat.DEFAULT.serialize());
this.customACFormat = format;
}
@Override
public AdminChatFormat getCustomACFormat()
{
return AdminChatFormat.deserialize(customACFormat);
}
}

View File

@ -1,5 +1,6 @@
package me.totalfreedom.base;
import me.totalfreedom.display.adminchat.AdminChatDisplay;
import me.totalfreedom.event.EventBus;
import me.totalfreedom.service.FreedomExecutor;
import me.totalfreedom.service.SubscriptionProvider;
@ -23,6 +24,10 @@ public class Patchwork extends JavaPlugin
* The {@link FreedomExecutor} for this plugin.
*/
private final FreedomExecutor executor = new FreedomExecutor();
/**
* The {@link AdminChatDisplay} for this plugin.
*/
private final AdminChatDisplay acdisplay = new AdminChatDisplay();
/**
* Provides this plugin instance through a safe static method. This is effectively the same thing as using
@ -89,4 +94,15 @@ public class Patchwork extends JavaPlugin
{
return eventBus;
}
/**
* Gets the {@link AdminChatDisplay} for this plugin. The AdminChatDisplay is used to display messages sent in
* adminchat.
*
* @return the {@link AdminChatDisplay}
*/
public AdminChatDisplay getAdminChatDisplay()
{
return acdisplay;
}
}

View File

@ -1,15 +1,31 @@
package me.totalfreedom.base;
import me.totalfreedom.provider.ModuleProvider;
import me.totalfreedom.api.Context;
import me.totalfreedom.user.User;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public final class Shortcuts
{
private Shortcuts() {
private Shortcuts()
{
throw new AssertionError();
}
public static <T extends JavaPlugin> ModuleProvider<T> provideModule(final Class<T> pluginClass) {
return Patchwork.getInstance().getRegistrations().getModuleRegistry().getProvider(pluginClass);
public static <T extends JavaPlugin> T provideModule(final Class<T> pluginClass)
{
return Patchwork.getInstance()
.getRegistrations()
.getModuleRegistry()
.getProvider(pluginClass)
.getModule();
}
public static User getUser(final Player player)
{
return Patchwork.getInstance()
.getRegistrations()
.getUserRegistry()
.getUser(player);
}
}

View File

@ -69,7 +69,7 @@ public final class BukkitDelegate extends Command implements PluginIdentifiableC
@NotNull final String commandLabel,
@NotNull final String[] args)
{
if (sender instanceof ConsoleCommandSender && noConsole)
if (!(sender instanceof Player) && noConsole)
{
sender.sendMessage(Component.text("This command can only be run by players."));
return true;
@ -101,8 +101,13 @@ public final class BukkitDelegate extends Command implements PluginIdentifiableC
{
try
{
command.getBaseMethod()
.invoke(command, sender);
if (noConsole) {
command.getBaseMethod()
.invoke(command, (Player)sender);
} else {
command.getBaseMethod()
.invoke(command, sender);
}
} catch (Exception ex)
{
FreedomLogger.getLogger("Patchwork")
@ -154,9 +159,15 @@ public final class BukkitDelegate extends Command implements PluginIdentifiableC
}
try
{
command.getSubcommands()
.get(node)
.invoke(command, sender, objects);
if (noConsole) {
command.getSubcommands()
.get(node)
.invoke(command, (Player)sender, objects);
} else {
command.getSubcommands()
.get(node)
.invoke(command, sender, objects);
}
} catch (Exception ex)
{
FreedomLogger.getLogger("Patchwork")

View File

@ -3,9 +3,11 @@ package me.totalfreedom.display.adminchat;
import io.papermc.paper.event.player.AsyncChatEvent;
import me.totalfreedom.base.Patchwork;
import me.totalfreedom.base.Shortcuts;
import me.totalfreedom.data.UserRegistry;
import me.totalfreedom.security.Groups;
import me.totalfreedom.user.UserData;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -19,10 +21,12 @@ import java.util.UUID;
public class AdminChatDisplay
{
protected static final String ACPERM = "patchwork.adminchat";
private final Map<UUID, AdminChatFormat> adminChatFormat = new HashMap<>();
private final Set<UUID> toggledChat = new HashSet<>();
public AdminChatDisplay() {
public AdminChatDisplay()
{
new ACListener(this);
}
@ -61,6 +65,39 @@ public class AdminChatDisplay
return adminChatFormat;
}
public boolean isToggled(final Player player)
{
return toggledChat.contains(player.getUniqueId());
}
public void toggleChat(final Player player)
{
if (toggledChat.contains(player.getUniqueId()))
{
toggledChat.remove(player.getUniqueId());
} else
{
toggledChat.add(player.getUniqueId());
}
}
public void adminChatMessage(final CommandSender sender, final Component message)
{
Bukkit.getOnlinePlayers()
.forEach(player ->
{
if (player.hasPermission(ACPERM))
{
final Component formatted = Component.empty();
formatted.append(getFormat(player).format(sender.getName(), Groups.fromSender(sender)))
.append(Component.space())
.append(message);
player.sendMessage(formatted);
}
});
}
public static final class ACListener implements Listener
{
private final AdminChatDisplay display;
@ -69,26 +106,34 @@ public class AdminChatDisplay
{
this.display = display;
Bukkit.getPluginManager()
.registerEvents(this, Shortcuts.provideModule(Patchwork.class)
.getModule());
.registerEvents(this, Shortcuts.provideModule(Patchwork.class));
}
@EventHandler
public void playerChat(final AsyncChatEvent event) {
if (display.getPlayers().contains(event.getPlayer().getUniqueId())) {
public void playerChat(final AsyncChatEvent event)
{
if (display.isToggled(event.getPlayer()))
{
event.setCancelled(true);
display.adminChatMessage(event.getPlayer(), event.message());
}
}
@EventHandler
public void playerJoin(final PlayerJoinEvent event) {
public void playerJoin(final PlayerJoinEvent event)
{
final Player player = event.getPlayer();
if (player.hasPermission("patchwork.adminchat")) {
final UserData data = Patchwork.getInstance().getRegistrations().getUserRegistry().fromPlayer(player);
if (data.hasCustomACFormat()) {
if (player.hasPermission(ACPERM))
{
final UserData data = Patchwork.getInstance()
.getRegistrations()
.getUserRegistry()
.fromPlayer(player);
if (data.hasCustomACFormat())
{
display.addPlayer(player, data.getCustomACFormat());
} else {
} else
{
display.addPlayer(player, AdminChatFormat.DEFAULT);
}
}

View File

@ -25,15 +25,25 @@ public final class AdminChatFormat
this.rank = Component.text(builder.openBracket(), builder.bracketColor())
.append(Component.text("%rank%", builder.rankColor()))
.append(Component.text(builder.closeBracket(), builder.bracketColor()));
this.chatSplitter = Component.text(builder.chatSplitter(), NamedTextColor.WHITE);
this.fullFormat = prefix.append(Component.space())
// Nice formatting :(
if (builder.chatSplitter()
.equals(":"))
{
this.chatSplitter = Component.text(":", NamedTextColor.WHITE);
} else
{
this.chatSplitter = Component.space()
.append(Component.text(builder.chatSplitter(), NamedTextColor.WHITE));
}
// Formatting because []: is cleaner than [] :, but anything else such as [] >> or [] -> looks better with the space between.
this.fullFormat = prefix.appendSpace()
.append(userName)
.append(Component.space())
.appendSpace()
.append(rank)
.append(Component.space())
.append(chatSplitter)
.append(Component.space());
.appendSpace();
}
public static AdminChatFormat deserialize(final String serialized)

View File

@ -1,20 +1,30 @@
package me.totalfreedom.security;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public enum Groups
{
NON_OP("patchwork.group.non-op"),
OP("patchwork.group.op"),
SUPER_ADMIN("patchwork.group.super"),
SENIOR_ADMIN("patchwork.group.senior"),
DEVELOPER("patchwork.group.dev"),
EXECUTIVE("patchwork.group.exec"),
OWNER("patchwork.group.owner");
NON_OP("patchwork.group.non-op", "Non-OP"),
OP("patchwork.group.op", "OP"),
SUPER_ADMIN("patchwork.group.super", "Super Admin"),
SENIOR_ADMIN("patchwork.group.senior", "Senior Admin"),
DEVELOPER("patchwork.group.dev", "Developer"),
EXECUTIVE("patchwork.group.exec", "Executive"),
OWNER("patchwork.group.owner", "Owner");
private final String permission;
private final String name;
Groups(final String permission)
Groups(final String permission, final String name)
{
this.permission = permission;
this.name = name;
}
public String getName()
{
return this.name;
}
public String getPermission()
@ -27,4 +37,19 @@ public enum Groups
{
return this.permission;
}
public static String fromPlayer(final Player player) {
for (final Groups group : values()) {
if (player.hasPermission(group.getPermission())) {
return group.getName();
}
}
return Groups.NON_OP.getName();
}
public static String fromSender(final CommandSender sender) {
if (!(sender instanceof Player)) return "CONSOLE";
return fromPlayer((Player) sender);
}
}