This commit is contained in:
Chipmunk 2022-12-16 18:56:20 -05:00
parent ae1c704379
commit b1255510f9
20 changed files with 774 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View file

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

13
.idea/compiler.xml Normal file
View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="chipmunkbot" />
</profile>
</annotationProcessing>
</component>
</project>

25
.idea/jarRepositories.xml Normal file
View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="opencollab" />
<option name="name" value="opencollab" />
<option name="url" value="https://repo.opencollab.dev/maven-releases/" />
</remote-repository>
</component>
</project>

12
.idea/misc.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="19" project-jdk-type="JavaSDK" />
</project>

124
.idea/uiDesigner.xml Normal file
View file

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

68
pom.xml Normal file
View file

@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>land.chipmunk</groupId>
<artifactId>chipmunkbot</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>chipmunkbot</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>opencollab</id>
<url>https://repo.opencollab.dev/maven-releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.steveice10</groupId>
<artifactId>mcprotocollib</artifactId>
<version>1.19.2-1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>land.chipmunk.chipmunkbot.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,44 @@
package land.chipmunk.chipmunkbot;
import com.github.steveice10.mc.auth.service.AuthenticationService;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.packetlib.ProxyInfo;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Client {
private Session session;
private Map<String, Plugin> plugins = new HashMap();
public Client (ClientOptions options) {
Session session = new TcpClientSession(options.host(), options.port(), options.protocol(), options.proxy());
this.session = session;
session.connect();
}
public Session session () {
return session;
}
public Plugin getPlugin (String id) { return plugins.get(id); }
public void loadPlugin (Class<? extends Plugin> pluginClass) {
try {
Constructor<? extends Plugin> constructor = pluginClass.getConstructor(Client.class);
Plugin plugin = constructor.newInstance(this);
plugins.put(plugin.id, plugin);
} catch (Exception ignored) {
}
}
// TODO: Maybe also add unloading?
public void inject (Injector injector) {
injector.inject(this);
}
}

View file

@ -0,0 +1,70 @@
package land.chipmunk.chipmunkbot;
import com.github.steveice10.mc.auth.service.AuthenticationService;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.packetlib.ProxyInfo;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
public class ClientOptions {
private String host;
private int port;
private MinecraftProtocol protocol;
private ProxyInfo proxy;
public ClientOptions (String host, int port, MinecraftProtocol protocol, ProxyInfo proxy) {
this.host = host;
this.port = port;
this.protocol = protocol;
this.proxy = proxy;
}
public ClientOptions () { // So it can easily be used as a builder
}
public String host () {
return host;
}
public ClientOptions host (String value) {
host = value;
return this;
}
public int port () {
return port;
}
public ClientOptions port (int value) {
port = value;
return this;
}
public MinecraftProtocol protocol () {
return protocol;
}
public ClientOptions protocol (MinecraftProtocol value) {
protocol = value;
return this;
}
/* public ClientOptions profile (GameProfile profile) {
protocol(new MinecraftProtocol(profile));
return this;
} */
public ClientOptions username (String username) {
protocol(new MinecraftProtocol(username));
return this;
}
public ProxyInfo proxy () {
return proxy;
}
public ClientOptions proxy (ProxyInfo value) {
proxy = value;
return this;
}
}

View file

@ -0,0 +1,5 @@
package land.chipmunk.chipmunkbot;
public interface Injector {
public void inject (Client client);
}

View file

@ -0,0 +1,58 @@
package land.chipmunk.chipmunkbot;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import com.google.gson.JsonObject;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import land.chipmunk.chipmunkbot.plugins.ChatPlugin;
import com.google.gson.Gson;
import land.chipmunk.chipmunkbot.plugins.PlayerListPlugin;
public class Main {
private static File CONFIG_FILE = new File("config.json");
private static JsonObject getConfig () throws Exception {
InputStream opt = new FileInputStream(CONFIG_FILE);
BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
return JsonParser.parseReader(reader).getAsJsonObject();
}
public static ClientOptions parseClientOptions (JsonObject options) {
return new ClientOptions()
.host(options.has("host") ? options.get("host").getAsString() : "0.0.0.0")
.port(options.has("port") ? options.get("port").getAsInt() : 25565)
.username(options.has("username") ? options.get("username").getAsString() : "username");
}
public static void main (String[] arguments) {
System.out.println("ChipmunkBot is starting...");
JsonObject config = null;
try {
config = getConfig();
} catch (Exception exception) {
exception.printStackTrace();
System.exit(1);
}
for (JsonElement element : config.get("bots").getAsJsonArray()) {
ClientOptions options = parseClientOptions(element.getAsJsonObject());
Client client = new Client(options); // TODO: Maybe create a list of some sort
client.loadPlugin(PlayerListPlugin.class);
}
}
}

View file

@ -0,0 +1,11 @@
package land.chipmunk.chipmunkbot;
public class Plugin {
public String id;
public Client client;
public Plugin (Client client, String id) {
this.client = client;
this.id = id;
}
}

View file

@ -0,0 +1,37 @@
package land.chipmunk.chipmunkbot.chat;
import net.kyori.adventure.text.Component;
import java.util.UUID;
public class ChatMessage {
// * I do not really care about signatures, they were likely made for the sus chat reporting system anyway, but I still include the sender as it really helps with stuff such as commands.
private Component component;
private UUID sender;
public ChatMessage (Component component, UUID sender) {
this.component = component;
this.sender = sender;
}
public ChatMessage (Component component) {
this(component, new UUID(0, 0));
}
public Component component () {
return component;
}
public ChatMessage component (Component value) {
component = value;
return this;
}
public UUID sender () {
return sender;
}
public ChatMessage sender (UUID value) {
sender = value;
return this;
}
}

View file

@ -0,0 +1,7 @@
package land.chipmunk.chipmunkbot.chat;
import net.kyori.adventure.text.Component;
public interface ChatParser {
public PlayerMessage parseMessage(ChatMessage message);
}

View file

@ -0,0 +1,8 @@
package land.chipmunk.chipmunkbot.chat;
public enum MessageType {
TEXT,
ANNOUNCEMENT,
EMOTE,
WHISPER
}

View file

@ -0,0 +1,37 @@
package land.chipmunk.chipmunkbot.chat;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import java.util.UUID;
import java.util.List;
public class MinecraftChatParser implements ChatParser {
private MessageType getMessageType (TranslatableComponent component) {
String key = component.key();
if (key.equals("chat.type.text")) return MessageType.TEXT;
if (key.equals("chat.type.announcement")) return MessageType.ANNOUNCEMENT;
if (key.equals("chat.type.emote")) return MessageType.EMOTE;
if (key.equals("chat.type.command")) return MessageType.WHISPER;
return null;
}
public PlayerMessage parseMessage (ChatMessage message) {
Component component = message.component();
if (component instanceof TranslatableComponent) return parseTranslatable((TranslatableComponent) component, message.sender());
return null;
}
public PlayerMessage parseTranslatable (TranslatableComponent component, UUID providedSender) {
TranslatableComponent translate = (TranslatableComponent) component;
MessageType type = getMessageType(translate);
if (type == null) return null;
List<Component> args = component.args();
// Component displayName = args.get(0);
Component contents = args.get(1);
return new PlayerMessage(null, contents, type); // TODO: Use player list
}
}

View file

@ -0,0 +1,47 @@
package land.chipmunk.chipmunkbot.chat;
import net.kyori.adventure.text.Component;
import com.github.steveice10.mc.protocol.data.game.PlayerListEntry;
public class PlayerMessage {
private PlayerListEntry sender;
private Component contents;
private MessageType type;
PlayerMessage (PlayerListEntry sender, Component contents, MessageType type) {
this.sender = sender;
this.contents = contents;
this.type = type;
}
PlayerMessage (PlayerListEntry sender, Component contents) {
this(sender, contents, MessageType.TEXT);
}
public PlayerListEntry sender () {
return sender;
}
public PlayerMessage sender (PlayerListEntry value) {
sender = value;
return this;
}
public Component contents () {
return contents;
}
public PlayerMessage contents (Component value) {
contents = value;
return this;
}
public MessageType type () {
return type;
}
public PlayerMessage type (MessageType value) {
type = value;
return this;
}
}

View file

@ -0,0 +1,60 @@
package land.chipmunk.chipmunkbot.data;
import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.protocol.data.game.PlayerListEntry;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import net.kyori.adventure.text.Component;
import java.security.PublicKey;
public class MutablePlayerListEntry {
private GameProfile profile;
private GameMode gamemode;
private int latency;
private Component displayName;
private long expiresAt;
private PublicKey publicKey;
private byte[] keySignature;
public MutablePlayerListEntry (GameProfile profile, GameMode gamemode, int latency, Component displayName, long expiresAt, PublicKey publicKey, byte[] keySignature) {
this.profile = profile;
this.gamemode = gamemode;
this.latency = latency;
this.displayName = displayName;
this.expiresAt = expiresAt;
this.publicKey = publicKey;
this.keySignature = keySignature;
}
public MutablePlayerListEntry (PlayerListEntry entry) {
this(entry.getProfile(), entry.getGameMode(), entry.getPing(), entry.getDisplayName(), entry.getExpiresAt(), entry.getPublicKey(), entry.getKeySignature());
}
public GameProfile profile () { return this.profile; }
public void profile (GameProfile profile) { this.profile = profile; }
public GameMode gamemode () { return this.gamemode; }
public void gamemode (GameMode gamemode) { this.gamemode = gamemode; }
public int latency () { return this.latency; }
public void latency (int latency) { this.latency = latency; }
public Component displayName () { return this.displayName; }
public void displayName (Component displayName) { this.displayName = displayName; }
public long expiresAt () { return this.expiresAt; }
public void expiresAt (long expiresAt) { this.expiresAt = expiresAt; }
public PublicKey publicKey () { return this.publicKey; }
public void publicKey (PublicKey publicKey) { this.publicKey = publicKey; }
public byte[] keySignature () { return this.keySignature; }
public void keySignature (byte[] keySignature) { this.keySignature = keySignature; }
}

View file

@ -0,0 +1,38 @@
package land.chipmunk.chipmunkbot.plugins;
import land.chipmunk.chipmunkbot.Plugin;
import land.chipmunk.chipmunkbot.Client;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import java.util.BitSet;
import java.util.ArrayList;
import java.util.List;
import java.time.Instant;
public class ChatPlugin extends Plugin {
public ChatPlugin (Client client) {
super(client, "chat");
/* client.session().addListener(new SessionAdapter () {
@Override
packetReceived (Session session, Packet packet) {
if (packet instanceof Server)
}
}); */
}
public void message (String message) {
final ServerboundChatPacket packet = new ServerboundChatPacket(message, Instant.now().toEpochMilli(), 0, new byte[0], false, new ArrayList<>(), null);
client.session().send(packet);
}
public void command (String command) {
final ServerboundChatCommandPacket packet = new ServerboundChatCommandPacket(command, Instant.now().toEpochMilli(), 0, new ArrayList<>(), false, new ArrayList<>(), null);
client.session().send(packet);
}
}

View file

@ -0,0 +1,102 @@
package land.chipmunk.chipmunkbot.plugins;
import land.chipmunk.chipmunkbot.Plugin;
import land.chipmunk.chipmunkbot.Client;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerInfoPacket;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.mc.protocol.data.game.PlayerListEntry;
import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction;
import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry;
import java.util.List;
import java.util.ArrayList;
import java.util.UUID;
public class PlayerListPlugin extends Plugin {
public List<MutablePlayerListEntry> list = new ArrayList<MutablePlayerListEntry>();
public PlayerListPlugin (Client client) {
super(client, "player_list");
client.session().addListener(new SessionAdapter() {
@Override
public void packetReceived (Session session, Packet packet) {
if (packet instanceof ClientboundPlayerInfoPacket) {
ClientboundPlayerInfoPacket _packet = (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;
}
private final 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));
System.out.println("Added " + newEntry.getProfile().getName() + " to the player list.");
}
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;
list.remove(target);
}
}