whitelist

This commit is contained in:
Chayapak 2023-09-25 18:15:44 +07:00
parent 1ab7aa86ed
commit dd7806d0f9
5 changed files with 199 additions and 17 deletions

View file

@ -79,6 +79,7 @@ public class Bot {
public AuthPlugin auth;
public ScreensharePlugin screenshare;
public FormatCheckerPlugin formatChecker;
public WhitelistPlugin whitelist;
public Bot (Configuration.BotOption botOption, List<Bot> bots, Configuration config) {
this.host = botOption.host;
@ -129,6 +130,7 @@ public class Bot {
this.auth = new AuthPlugin(this);
// this.screenshare = new ScreensharePlugin(this);
this.formatChecker = new FormatCheckerPlugin(this);
this.whitelist = new WhitelistPlugin(this);
for (Listener listener : listeners) listener.loadedPlugins();

View file

@ -0,0 +1,103 @@
package land.chipmunk.chayapak.chomens_bot.commands;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.command.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import land.chipmunk.chayapak.chomens_bot.command.CommandException;
import land.chipmunk.chayapak.chomens_bot.command.TrustLevel;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
public class WhitelistCommand extends Command {
public WhitelistCommand () {
super(
"whitelist",
"Manages whitelist",
new String[] { "<ownerHash> enable", "<ownerHash> disable", "<ownerHash> add <player>", "<ownerHash> remove <index>", "<ownerHash> clear", "<ownerHash> list" },
new String[] {},
TrustLevel.OWNER,
false
);
}
@Override
public Component execute(CommandContext context) throws CommandException {
final Bot bot = context.bot;
final String action = context.getString(false, true);
switch (action) {
case "enable" -> {
bot.whitelist.enable();
return Component.text("Enabled whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "disable" -> {
bot.whitelist.disable();
return Component.text("Disabled whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "add" -> {
final String player = context.getString(true, true);
bot.whitelist.add(player);
return Component.translatable(
"Added %s to the whitelist",
Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "remove" -> {
final String player = context.getString(true, true);
if (!bot.whitelist.list.contains(player)) throw new CommandException(Component.text("Player doesn't exist in the list"));
if (player.equals(bot.profile.getName())) throw new CommandException(Component.text("Cannot remove the bot"));
bot.whitelist.remove(player);
return Component.translatable(
"Removed %s from the whitelist",
Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "clear" -> {
bot.whitelist.clear();
return Component.text("Cleared the whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "list" -> {
final List<Component> playersComponent = new ArrayList<>();
int index = 0;
for (String player : bot.whitelist.list) {
playersComponent.add(
Component.translatable(
"%s %s",
Component.text(index).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)),
Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
).color(NamedTextColor.DARK_GRAY)
);
index++;
}
return Component.empty()
.append(Component.text("Whitelisted players ").color(NamedTextColor.GREEN))
.append(Component.text("(").color(NamedTextColor.DARK_GRAY))
.append(Component.text(bot.whitelist.list.size()).color(NamedTextColor.GRAY))
.append(Component.text(")").color(NamedTextColor.DARK_GRAY))
.append(Component.newline())
.append(
Component.join(JoinConfiguration.newlines(), playersComponent)
);
}
default -> throw new CommandException(Component.text("Invalid action"));
}
}
}

View file

@ -53,6 +53,7 @@ public class CommandHandlerPlugin {
registerCommand(new ConsoleCommand());
registerCommand(new PCrashCommand());
// registerCommand(new ScreenshareCommand());
registerCommand(new WhitelistCommand());
}
public boolean disabled = false;

View file

@ -44,7 +44,7 @@ public class FilterPlugin extends PlayersPlugin.Listener {
bot.commandSpy.addListener(new CommandSpyPlugin.Listener() {
@Override
public void commandReceived(PlayerEntry sender, String command) {
FilterPlugin.this.commandSpyMessageReceived(sender, command);
FilterPlugin.this.commandSpyMessageReceived(sender);
}
});
@ -100,21 +100,15 @@ public class FilterPlugin extends PlayersPlugin.Listener {
if (player == null) return;
deOp(target);
mute(target);
gamemode(target);
bot.exploits.kick(target.profile.getId());
doAll(target);
}
public void commandSpyMessageReceived (PlayerEntry sender, String command) {
public void commandSpyMessageReceived (PlayerEntry sender) {
final FilteredPlayer player = getPlayer(sender.profile.getName());
if (player == null) return;
deOp(sender);
gamemode(sender);
mute(sender);
doAll(sender);
}
public void playerMessageReceived (PlayerMessage message) {
@ -124,9 +118,14 @@ public class FilterPlugin extends PlayersPlugin.Listener {
if (player == null || message.sender.profile.getId().equals(new UUID(0L, 0L))) return;
deOp(message.sender);
mute(message.sender);
gamemode(message.sender);
doAll(message.sender);
}
public void doAll (PlayerEntry entry) {
mute(entry);
deOp(entry);
gameMode(entry);
bot.exploits.kick(entry.profile.getId());
}
public void mute (PlayerEntry target) { mute(target, ""); }
@ -138,7 +137,7 @@ public class FilterPlugin extends PlayersPlugin.Listener {
bot.core.run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile.getId()));
}
public void gamemode (PlayerEntry target) {
public void gameMode(PlayerEntry target) {
bot.core.run("minecraft:gamemode adventure " + UUIDUtilities.selector(target.profile.getId()));
}
@ -161,9 +160,7 @@ public class FilterPlugin extends PlayersPlugin.Listener {
if (target == null) return;
deOp(target);
mute(target);
gamemode(target);
doAll(target);
}
public FilteredPlayer remove (int index) {

View file

@ -0,0 +1,79 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry;
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage;
import java.util.ArrayList;
import java.util.List;
public class WhitelistPlugin extends PlayersPlugin.Listener {
private final Bot bot;
public final List<String> list = new ArrayList<>();
private boolean enabled = false;
public WhitelistPlugin (Bot bot) {
this.bot = bot;
bot.players.addListener(this);
bot.chat.addListener(new ChatPlugin.Listener() {
@Override
public void playerMessageReceived(PlayerMessage message) {
WhitelistPlugin.this.playerMessageReceived(message);
}
});
bot.commandSpy.addListener(new CommandSpyPlugin.Listener() {
@Override
public void commandReceived(PlayerEntry sender, String command) {
WhitelistPlugin.this.commandReceived(sender);
}
});
}
public void enable () {
enabled = true;
for (PlayerEntry entry : bot.players.list) {
if (list.contains(entry.profile.getName())) continue;
list.add(entry.profile.getName());
}
}
public void disable () {
enabled = false;
}
public void add (String player) { list.add(player); }
public void remove (String player) {
list.removeIf(eachPlayer -> eachPlayer.equals(player));
handle(bot.players.getEntry(player));
}
public void clear () {
list.removeIf(eachPlayer -> !eachPlayer.equals(bot.profile.getName()));
}
@Override
public void playerJoined(PlayerEntry target) {
handle(target);
}
public void playerMessageReceived (PlayerMessage message) {
handle(message.sender);
}
public void commandReceived (PlayerEntry sender) {
handle(sender);
}
private void handle (PlayerEntry entry) {
if (!enabled || list.contains(entry.profile.getName())) return;
bot.filter.doAll(entry);
}
}