-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c64a441
Showing
19 changed files
with
496 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>MCBot</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
eclipse.preferences.version=1 | ||
encoding//src/main/java=UTF-8 | ||
encoding//src/test/java=UTF-8 | ||
encoding/<project>=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||
org.eclipse.jdt.core.compiler.source=1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
activeProfiles= | ||
eclipse.preferences.version=1 | ||
resolveWorkspaceProjects=true | ||
version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>me.alexander.mcbot</groupId> | ||
<artifactId>MCBot</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>MCBot</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>sonatype</id> | ||
<name>Sonatype Nexus</name> | ||
<layout>default</layout> | ||
<url>https://oss.sonatype.org/content/repositories/public/</url> | ||
</repository> | ||
<repository> | ||
<id>spacehq</id> | ||
<name>SpaceHQ Repository</name> | ||
<layout>default</layout> | ||
<url>https://repo.spacehq.org/content/repositories/releases/</url> | ||
</repository> | ||
<repository> | ||
<id>jitpack.io</id> | ||
<url>https://jitpack.io</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.github.steveice10</groupId> | ||
<artifactId>MCProtocolLib</artifactId> | ||
<version>-1.11.2-2-gf0424a2-4</version> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/log4j/log4j --> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.17</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.github.steveice10</groupId> | ||
<artifactId>mcauthlib</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>jline</groupId> | ||
<artifactId>jline</artifactId> | ||
<version>2.11</version> | ||
<type>jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.yaml</groupId> | ||
<artifactId>snakeyaml</artifactId> | ||
<version>1.16</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package me.alexander.mcbot; | ||
|
||
import java.net.Proxy; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import com.github.steveice10.mc.protocol.MinecraftProtocol; | ||
import com.github.steveice10.mc.protocol.data.game.MessageType; | ||
import com.github.steveice10.mc.protocol.data.message.Message; | ||
import com.github.steveice10.mc.protocol.data.message.TranslationMessage; | ||
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; | ||
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket; | ||
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket; | ||
import com.github.steveice10.packetlib.Client; | ||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent; | ||
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent; | ||
import com.github.steveice10.packetlib.event.session.PacketSentEvent; | ||
import com.github.steveice10.packetlib.event.session.SessionAdapter; | ||
import com.github.steveice10.packetlib.event.session.SessionListener; | ||
import com.github.steveice10.packetlib.packet.Packet; | ||
import com.github.steveice10.packetlib.tcp.TcpSessionFactory; | ||
|
||
public class Bot { | ||
|
||
private final Logger logger = Logger.getRootLogger(); | ||
private final String username, password; | ||
private Client client; | ||
private boolean autoReconnect = true; | ||
|
||
public Bot(String username) { | ||
this(username, ""); | ||
} | ||
|
||
public Bot(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
public void connect(String host) { | ||
if (host.contains(":")) { | ||
connect(host.split(":")[0], Integer.valueOf(host.split(":")[1])); | ||
} else { | ||
connect(host, 25565); | ||
} | ||
} | ||
|
||
public void connect(final String host, final int port) { | ||
logger.info("Connnecting to " + host + ":" + port); | ||
MinecraftProtocol protocol = null; | ||
if (!password.equals("")) { | ||
try { | ||
protocol = new MinecraftProtocol(username, password, false); | ||
} catch (Exception ex) { | ||
protocol = new MinecraftProtocol(username); | ||
} | ||
} else { | ||
protocol = new MinecraftProtocol(username); | ||
} | ||
|
||
client = new Client(host, port, protocol, new TcpSessionFactory(Proxy.NO_PROXY)); | ||
client.getSession().addListener(new SessionAdapter() { | ||
|
||
@Override | ||
public void packetReceived(PacketReceivedEvent event) { | ||
if (event.getPacket() instanceof ServerJoinGamePacket) { | ||
logger.info("Connected to " + host + ":" + port); | ||
} else if (event.getPacket() instanceof ServerChatPacket) { | ||
Message message = event.<ServerChatPacket>getPacket().getMessage(); | ||
if (message instanceof TranslationMessage) { | ||
logger.info("Chat message: { Server: " + host + ":" + port + ", Message: " | ||
+ ((TranslationMessage) message).getTranslationParams()[0] + " => " | ||
+ ((TranslationMessage) message).getTranslationParams()[1] + " }"); | ||
} else if (event.<ServerChatPacket>getPacket().getType().equals(MessageType.SYSTEM)) { | ||
logger.info(event.getPacket()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void packetSent(PacketSentEvent event) { | ||
|
||
} | ||
|
||
@Override | ||
public void disconnected(DisconnectedEvent event) { | ||
logger.info("Disconnected from " + event.getSession().getHost() + ":" + event.getSession().getPort() | ||
+ ", reason: " + Message.fromString(event.getReason()).getFullText()); | ||
if (autoReconnect) { | ||
new Thread(() -> { | ||
logger.info("Reconnecting to " + event.getSession().getHost() + ":" | ||
+ event.getSession().getPort() + " in 5 seconds..."); | ||
try { | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
connect(host, port); | ||
}).start(); | ||
} | ||
} | ||
|
||
}); | ||
|
||
client.getSession().connect(); | ||
} | ||
|
||
/* | ||
* Handlers | ||
*/ | ||
|
||
/** | ||
* Disconnects from the current server | ||
* | ||
* @param reason | ||
*/ | ||
public void disconnect(String reason) { | ||
if (client.getSession().isConnected()) { | ||
client.getSession().disconnect(reason); | ||
} | ||
} | ||
|
||
/** | ||
* Sends a chat message | ||
* | ||
* @param message | ||
*/ | ||
public void sendMessage(String message) { | ||
if (client.getSession().isConnected()) { | ||
client.getSession().send(new ClientChatPacket(message)); | ||
} | ||
} | ||
|
||
/** | ||
* Adds a session listener to the bot | ||
* | ||
* @param listener | ||
*/ | ||
public void addListener(SessionListener listener) { | ||
client.getSession().addListener(listener); | ||
} | ||
|
||
/** | ||
* | ||
* Returns null if not connected, returns a Connection object with the | ||
* current server data | ||
* | ||
* @return | ||
*/ | ||
public boolean isConnected() { | ||
if (client != null) { | ||
return client.getSession().isConnected(); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Sends a packet | ||
* | ||
* @param packet | ||
*/ | ||
public void sendPacket(Packet packet) { | ||
if (isConnected()) { | ||
client.getSession().send(packet); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.alexander.mcbot; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; | ||
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket; | ||
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent; | ||
import com.github.steveice10.packetlib.event.session.SessionAdapter; | ||
|
||
public class Main { | ||
|
||
private static final Logger logger = Logger.getLogger(Main.class); | ||
|
||
public static void main(String[] args) { | ||
Utils.initLogger(); | ||
logger.info("hey"); | ||
final Bot b = new Bot("MrBot"); | ||
b.connect("localhost"); | ||
b.addListener(new SessionAdapter() { | ||
@Override | ||
public void packetReceived(PacketReceivedEvent event) { | ||
if (event.getPacket() instanceof ServerJoinGamePacket) { | ||
b.sendPacket(new ClientChatPacket("This is a bot")); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.alexander.mcbot; | ||
|
||
import org.apache.log4j.ConsoleAppender; | ||
import org.apache.log4j.Level; | ||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.PatternLayout; | ||
|
||
public class Utils { | ||
|
||
public static void initLogger() { | ||
Logger rootLogger = Logger.getRootLogger(); | ||
rootLogger.setLevel(Level.DEBUG); | ||
PatternLayout layout = new PatternLayout("%d{HH:mm:ss} => { %m } %n"); | ||
rootLogger.addAppender(new ConsoleAppender(layout)); | ||
} | ||
|
||
} |
Oops, something went wrong.