add filter (ignore messy code plz)

This commit is contained in:
Chayapak 2023-04-24 13:19:25 +07:00
parent 7d1bfd62d4
commit 6e91fe82d4
6 changed files with 294 additions and 32 deletions

View file

@ -4,8 +4,13 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="50f184fa-7bed-4956-baf5-7586ff26ea08" name="Changes" comment="put the setter back">
<list default="true" id="50f184fa-7bed-4956-baf5-7586ff26ea08" name="Changes" comment="actually revert greplog update because it breaks stuff">
<change afterPath="$PROJECT_DIR$/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/land/chipmunk/chayapak/chomens_bot/data/FilteredPlayer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -36,26 +41,26 @@
</option>
</component>
<component name="ProblemsViewState">
<option name="selectedTabId" value="ProjectErrors" />
<option name="selectedTabId" value="CurrentFile" />
</component>
<component name="ProjectId" id="2NljChrU0rNqQmqh8CSQG0gdKNt" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "master",
"jdk.selected.JAVA_MODULE": "17",
"last_opened_file_path": "/home/chayapak1/IdeaProjects/CrackUtils",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.0",
"settings.editor.selected.configurable": "discord-application"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;master&quot;,
&quot;jdk.selected.JAVA_MODULE&quot;: &quot;17&quot;,
&quot;last_opened_file_path&quot;: &quot;/home/chayapak1/IdeaProjects/CrackUtils&quot;,
&quot;project.structure.last.edited&quot;: &quot;Modules&quot;,
&quot;project.structure.proportion&quot;: &quot;0.0&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.0&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;discord-application&quot;
}
}]]></component>
}</component>
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/.idea" />
@ -97,20 +102,6 @@
<option name="presentableId" value="Default" />
<updated>1680245437032</updated>
</task>
<task id="LOCAL-00058" summary="bossbar manager actionbar and enabled">
<created>1681535433129</created>
<option name="number" value="00058" />
<option name="presentableId" value="LOCAL-00058" />
<option name="project" value="LOCAL" />
<updated>1681535433130</updated>
</task>
<task id="LOCAL-00059" summary="forgor">
<created>1681535781354</created>
<option name="number" value="00059" />
<option name="presentableId" value="LOCAL-00059" />
<option name="project" value="LOCAL" />
<updated>1681535781354</updated>
</task>
<task id="LOCAL-00060" summary="improve serverinfo">
<created>1681536905476</created>
<option name="number" value="00060" />
@ -440,7 +431,21 @@
<option name="project" value="LOCAL" />
<updated>1682209891989</updated>
</task>
<option name="localTasksCounter" value="107" />
<task id="LOCAL-00107" summary="use serverName instead of host:port for console i guess">
<created>1682223323007</created>
<option name="number" value="00107" />
<option name="presentableId" value="LOCAL-00107" />
<option name="project" value="LOCAL" />
<updated>1682223323007</updated>
</task>
<task id="LOCAL-00108" summary="actually revert greplog update because it breaks stuff">
<created>1682250189952</created>
<option name="number" value="00108" />
<option name="presentableId" value="LOCAL-00108" />
<option name="project" value="LOCAL" />
<updated>1682250189954</updated>
</task>
<option name="localTasksCounter" value="109" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
@ -455,8 +460,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="mabe" />
<MESSAGE value="mess with the pitch correction again to fix the sus problem" />
<MESSAGE value="add a working (or not?) stereo to music" />
<MESSAGE value="improve and fix cmd handler (first commit on arch)&#10;yup this is my first commit on arch linux,.,.,..,.,.,&#10;yes i use arch btw :sunglasses:" />
<MESSAGE value="1.19.4 + add stuff (kinda broken ..,,.)" />
@ -480,7 +483,9 @@
<MESSAGE value="probably lazy fix the sus" />
<MESSAGE value="improve bot options? mabe mabe" />
<MESSAGE value="put the setter back" />
<option name="LAST_COMMIT_MESSAGE" value="put the setter back" />
<MESSAGE value="use serverName instead of host:port for console i guess" />
<MESSAGE value="actually revert greplog update because it breaks stuff" />
<option name="LAST_COMMIT_MESSAGE" value="actually revert greplog update because it breaks stuff" />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />

View file

@ -62,6 +62,7 @@ public class Bot {
@Getter private CloopPlugin cloop;
@Getter private MazePlugin maze;
@Getter private ExploitsPlugin exploits;
@Getter private FilterPlugin filter;
public Bot (Configuration.BotOption botOption, List<Bot> allBots, Configuration config) {
this.host = botOption.host;
@ -102,6 +103,7 @@ public class Bot {
this.cloop = new CloopPlugin(this);
this.maze = new MazePlugin(this);
this.exploits = new ExploitsPlugin(this);
this.filter = new FilterPlugin(this);
reconnect();
}

View file

@ -0,0 +1,141 @@
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.data.FilteredPlayer;
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.Arrays;
import java.util.List;
public class FilterCommand implements Command {
public String name() { return "filter"; }
public String description() {
return "Filter players";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<hash> add <{player}>");
usages.add("<hash> -ignorecase add <{player}>");
usages.add("<hash> -regex add <{player}>");
usages.add("<hash> -ignorecase -regex add <{player}>");
usages.add("<hash> remove <index>");
usages.add("<hash> clear");
usages.add("<hash> list");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("filterplayer");
return aliases;
}
public int trustLevel() {
return 1;
}
// most of these codes are from cloop and greplog
public Component execute(CommandContext context, String[] _args, String[] fullArgs) {
final Bot bot = context.bot();
boolean ignoreCase = false;
boolean regex = false;
String[] args = _args;
// this is a mess
if (_args[0].equals("-ignorecase")) {
ignoreCase = true;
args = Arrays.copyOfRange(_args, 1, _args.length);
} else if (_args[0].equals("-regex")) {
regex = true;
args = Arrays.copyOfRange(_args, 1, _args.length);
}
if (_args.length > 1 && _args[1].equals("-ignorecase")) {
ignoreCase = true;
args = Arrays.copyOfRange(_args, 2, _args.length);
} else if (_args.length > 1 && _args[1].equals("-regex")) {
regex = true;
args = Arrays.copyOfRange(_args, 2, _args.length);
}
switch (args[0]) {
case "add" -> {
final String player = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
bot.filter().add(player, regex, ignoreCase);
context.sendOutput(
Component.translatable(
"Added %s to the filters",
Component.text(player).color(NamedTextColor.AQUA)
)
);
}
case "remove" -> {
try {
final int index = Integer.parseInt(args[1]);
context.sendOutput(
Component.translatable(
"Removed %s from the filters",
Component.text(bot.filter().filteredPlayers().get(index).playerName).color(NamedTextColor.AQUA)
)
);
bot.filter().remove(index);
} catch (IndexOutOfBoundsException | IllegalArgumentException | NullPointerException ignored) {
return Component.text("Invalid index").color(NamedTextColor.RED);
}
}
case "clear" -> {
bot.filter().clear();
context.sendOutput(
Component.text("Cleared the filter")
);
}
case "list" -> {
final List<Component> filtersComponents = new ArrayList<>();
int index = 0;
for (FilteredPlayer player : bot.filter().filteredPlayers()) {
filtersComponents.add(
Component.translatable(
"%s %s",
Component.text(index).color(NamedTextColor.GREEN),
Component.text(player.playerName).color(NamedTextColor.AQUA)
).color(NamedTextColor.DARK_GRAY)
);
index++;
}
final Component component = Component.empty()
.append(Component.text("Filtered players ").color(NamedTextColor.GREEN))
.append(Component.text("(").color(NamedTextColor.DARK_GRAY))
.append(Component.text(bot.filter().filteredPlayers().size()).color(NamedTextColor.GRAY))
.append(Component.text(")").color(NamedTextColor.DARK_GRAY))
.append(Component.newline())
.append(
Component.join(JoinConfiguration.newlines(), filtersComponents)
);
context.sendOutput(component);
}
default -> {
return Component.text("Invalid argument").color(NamedTextColor.RED);
}
}
return Component.text("success");
}
}

View file

@ -0,0 +1,10 @@
package land.chipmunk.chayapak.chomens_bot.data;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class FilteredPlayer {
public String playerName;
public boolean regex;
public boolean ignoreCase;
}

View file

@ -59,6 +59,7 @@ public class CommandHandlerPlugin {
registerCommand(new TranslateCommand());
registerCommand(new KickCommand());
registerCommand(new ClearChatQueueCommand());
registerCommand(new FilterCommand());
}
public void registerCommand (Command command) {

View file

@ -0,0 +1,103 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.chatParsers.data.MutablePlayerListEntry;
import land.chipmunk.chayapak.chomens_bot.data.FilteredPlayer;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
public class FilterPlugin extends PlayersPlugin.PlayerListener {
private final Bot bot;
@Getter private final List<FilteredPlayer> filteredPlayers = new ArrayList<>();
public FilterPlugin (Bot bot) {
this.bot = bot;
bot.players().addListener(this);
bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS);
}
private FilteredPlayer getPlayer (String name) {
// mess
// also regex and ignorecase codes from greplog plugin
FilteredPlayer filteredPlayer = null;
for (FilteredPlayer _filteredPlayer : filteredPlayers) {
if (_filteredPlayer.regex) {
Pattern pattern = null;
if (_filteredPlayer.ignoreCase) {
pattern = Pattern.compile("\\b" + _filteredPlayer.playerName + "\\b", Pattern.CASE_INSENSITIVE);
} else {
try {
pattern = Pattern.compile("\\b" + _filteredPlayer.playerName + "\\b");
} catch (Exception e) {
bot.chat().tellraw(Component.text(e.toString()).color(NamedTextColor.RED));
}
}
if (pattern.matcher(name).find()) {
filteredPlayer = _filteredPlayer;
break;
}
} else {
if (_filteredPlayer.ignoreCase) {
if (_filteredPlayer.playerName.toLowerCase().equals(name)) {
filteredPlayer = _filteredPlayer;
break;
}
} else {
if (_filteredPlayer.playerName.equals(name)) {
filteredPlayer = _filteredPlayer;
break;
}
}
}
}
return filteredPlayer;
}
@Override
public void playerJoined (MutablePlayerListEntry target) {
final FilteredPlayer player = getPlayer(target.profile().getName());
if (player == null) return;
System.out.println("joined event kicking " + target.profile().getName());
/* bot.core().run("essentials:mute " + target.profile().getIdAsString() + " 10y");
bot.core().run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile().getId()));
bot.exploits().kick(target.profile().getId()); */
}
public void tick () {
for (MutablePlayerListEntry target : bot.players().list()) {
final FilteredPlayer player = getPlayer(target.profile().getName());
if (player == null) continue;
System.out.println("kicking " + target.profile().getName());
// bot.exploits().kick(target.profile().getId());
}
}
public void add (String playerName, boolean regex, boolean ignoreCase) {
filteredPlayers.add(new FilteredPlayer(playerName, regex, ignoreCase));
}
public void remove (int index) {
filteredPlayers.remove(index);
}
public void clear () {
filteredPlayers.clear();
}
}