feat: re-add icu self-care that is more accurate with code RIPPED from craftbukkit.
This commit is contained in:
@@ -162,6 +162,8 @@ public class Configuration {
|
||||
public String prefix = "&8[&eChomeNS Bot&8]";
|
||||
}
|
||||
|
||||
public boolean icu = true;
|
||||
|
||||
public boolean username = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -152,15 +152,16 @@ public class PlayersPlugin implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
public final PlayerEntry getEntry (final String username) {
|
||||
public final PlayerEntry getEntry (final String username) { return getEntry(username, true, true); }
|
||||
public final PlayerEntry getEntry (final String username, final boolean checkUUID, final boolean checkPastUsernames) {
|
||||
synchronized (list) {
|
||||
for (final PlayerEntry candidate : list) {
|
||||
if (
|
||||
candidate != null &&
|
||||
(
|
||||
candidate.profile.getIdAsString().equals(username) || // checks for a string UUID
|
||||
candidate.profile.getName().equals(username) ||
|
||||
candidate.usernames.contains(username)
|
||||
candidate.profile.getName().equals(username)
|
||||
|| (checkUUID && candidate.profile.getIdAsString().equals(username))
|
||||
|| (checkPastUsernames && candidate.usernames.contains(username))
|
||||
)
|
||||
) {
|
||||
return candidate;
|
||||
@@ -171,6 +172,32 @@ public class PlayersPlugin implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
// RIPPED from craftbukkit (literally)
|
||||
// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/CraftServer.java#592
|
||||
public final PlayerEntry getEntryTheBukkitWay (final String username) {
|
||||
PlayerEntry found = getEntry(username, false, false);
|
||||
// Try for an exact match first.
|
||||
if (found != null) {
|
||||
return found;
|
||||
}
|
||||
|
||||
final String lowerName = username.toLowerCase(Locale.ROOT);
|
||||
int delta = Integer.MAX_VALUE;
|
||||
synchronized (list) {
|
||||
for (final PlayerEntry player : list) {
|
||||
if (player.profile.getName().toLowerCase(Locale.ROOT).startsWith(lowerName)) {
|
||||
final int curDelta = Math.abs(player.profile.getName().length() - lowerName.length());
|
||||
if (curDelta < delta) {
|
||||
found = player;
|
||||
delta = curDelta;
|
||||
}
|
||||
if (curDelta == 0) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
public final PlayerEntry getEntry (final Component displayName) {
|
||||
synchronized (list) {
|
||||
for (final PlayerEntry candidate : list) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package me.chayapak1.chomens_bot.plugins;
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.Configuration;
|
||||
import me.chayapak1.chomens_bot.data.listener.Listener;
|
||||
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.geysermc.mcprotocollib.network.Session;
|
||||
import org.geysermc.mcprotocollib.network.packet.Packet;
|
||||
@@ -22,6 +23,7 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.S
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerCommandPacket;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SelfCarePlugin implements Listener {
|
||||
@@ -134,6 +136,33 @@ public class SelfCarePlugin implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandSpyMessageReceived (final PlayerEntry sender, final String command) {
|
||||
if (!bot.config.selfCare.icu || !bot.serverFeatures.hasIControlU) return;
|
||||
|
||||
final String[] args = command.split("\\s+");
|
||||
|
||||
if (args.length < 3) return;
|
||||
|
||||
if (
|
||||
(!args[0].equals("/icontrolu:icu") && !args[0].equals("/icu"))
|
||||
|| !args[1].equalsIgnoreCase("control")
|
||||
) return;
|
||||
|
||||
// interestingly, icu only uses the third argument for the player, and not a greedy string
|
||||
final String player = args[2];
|
||||
|
||||
PlayerEntry target = bot.players.getEntryTheBukkitWay(player);
|
||||
|
||||
if (target == null && args[2].matches("([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})")) {
|
||||
target = bot.players.getEntry(UUID.fromString(args[2]));
|
||||
}
|
||||
|
||||
if (target == null || !target.profile.getId().equals(bot.profile.getId())) return;
|
||||
|
||||
bot.core.run("essentials:sudo " + sender.profile.getIdAsString() + " icu stop");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetReceived (final Session session, final Packet packet) {
|
||||
switch (packet) {
|
||||
|
||||
@@ -16,6 +16,7 @@ public class ServerFeaturesPlugin implements Listener {
|
||||
|
||||
public boolean hasEssentials = false;
|
||||
public boolean hasExtras = false;
|
||||
public boolean hasIControlU = false;
|
||||
|
||||
public ServerFeaturesPlugin (final Bot bot) {
|
||||
this.bot = bot;
|
||||
@@ -57,6 +58,7 @@ public class ServerFeaturesPlugin implements Listener {
|
||||
switch (key) {
|
||||
case "extras" -> hasExtras = true;
|
||||
case "essentials" -> hasEssentials = true;
|
||||
case "icontrolu" -> hasIControlU = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user