add multiple prefixes and cspy prefixes
totallynotskidded™ some codes from ChipmunkBot™
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package me.chayapak1.chomens_bot.plugins;
|
||||
|
||||
import com.github.steveice10.mc.protocol.data.game.PlayerListEntry;
|
||||
import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerInfoPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayersPlugin extends SessionAdapter {
|
||||
private final Bot bot;
|
||||
public List<MutablePlayerListEntry> list = new ArrayList<>();
|
||||
|
||||
public PlayersPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
bot.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetReceived (Session session, Packet packet) {
|
||||
if (packet instanceof ClientboundPlayerInfoPacket) packetReceived((ClientboundPlayerInfoPacket) packet);
|
||||
}
|
||||
|
||||
public void packetReceived (ClientboundPlayerInfoPacket packet) {
|
||||
PlayerListEntryAction action = packet.getAction();
|
||||
for (PlayerListEntry entry : packet.getEntries()) {
|
||||
if (action == PlayerListEntryAction.ADD_PLAYER) addPlayer(entry);
|
||||
else if (action == PlayerListEntryAction.UPDATE_GAMEMODE) updateGamemode(entry);
|
||||
else if (action == PlayerListEntryAction.UPDATE_LATENCY) updateLatency(entry);
|
||||
else if (action == PlayerListEntryAction.UPDATE_DISPLAY_NAME) updateDisplayName(entry);
|
||||
else if (action == PlayerListEntryAction.REMOVE_PLAYER) removePlayer(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public final MutablePlayerListEntry getEntry (UUID uuid) {
|
||||
for (MutablePlayerListEntry candidate : list) {
|
||||
if (candidate.profile().getId().equals(uuid)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public final MutablePlayerListEntry getEntry (String username) {
|
||||
for (MutablePlayerListEntry candidate : list) {
|
||||
if (candidate.profile().getName().equals(username)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public final MutablePlayerListEntry getEntry (Component displayName) {
|
||||
for (MutablePlayerListEntry candidate : list) {
|
||||
if (candidate.displayName() != null && candidate.displayName().equals(displayName)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private MutablePlayerListEntry getEntry (PlayerListEntry other) {
|
||||
return getEntry(other.getProfile().getId());
|
||||
}
|
||||
|
||||
private void addPlayer (PlayerListEntry newEntry) {
|
||||
final MutablePlayerListEntry duplicate = getEntry(newEntry);
|
||||
if (duplicate != null) list.remove(duplicate);
|
||||
|
||||
list.add(new MutablePlayerListEntry(newEntry));
|
||||
}
|
||||
|
||||
private void updateGamemode (PlayerListEntry newEntry) {
|
||||
final MutablePlayerListEntry target = getEntry(newEntry);
|
||||
if (target == null) return;
|
||||
|
||||
target.gamemode(newEntry.getGameMode());
|
||||
}
|
||||
|
||||
private void updateLatency (PlayerListEntry newEntry) {
|
||||
final MutablePlayerListEntry target = getEntry(newEntry);
|
||||
if (target == null) return;
|
||||
|
||||
target.latency(newEntry.getPing());
|
||||
}
|
||||
|
||||
private void updateDisplayName (PlayerListEntry newEntry) {
|
||||
final MutablePlayerListEntry target = getEntry(newEntry);
|
||||
if (target == null) return;
|
||||
|
||||
target.displayName(newEntry.getDisplayName());
|
||||
}
|
||||
|
||||
private void removePlayer (PlayerListEntry newEntry) {
|
||||
final MutablePlayerListEntry target = getEntry(newEntry);
|
||||
if (target == null) return;
|
||||
|
||||
bot.tabComplete().tabComplete("/minecraft:scoreboard players add ").thenApply(packet -> {
|
||||
final String[] matches = packet.getMatches();
|
||||
final Component[] tooltips = packet.getTooltips();
|
||||
final String username = target.profile().getName();
|
||||
|
||||
for (int i = 0; i < matches.length; i++) {
|
||||
if (tooltips[i] != null || !matches[i].equals(username)) continue;
|
||||
return packet;
|
||||
}
|
||||
|
||||
list.remove(target);
|
||||
return packet;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user