Dev/remove standalone #86
No files matched your search
@@ -1,8 +1,9 @@
|
||||
package com.shanebeestudios.mcbots.api;
|
||||
|
||||
import com.shanebeestudios.mcbots.api.timer.GravityTimer;
|
||||
import com.shanebeestudios.mcbots.api.generator.NickGenerator;
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.timer.GravityTimer;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
@@ -10,57 +11,120 @@ import java.net.InetSocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Base bot manager
|
||||
*/
|
||||
public abstract class BotManager {
|
||||
|
||||
// Fields
|
||||
private final Info info;
|
||||
private final InetSocketAddress inetAddr;
|
||||
protected int autoRespawnDelay;
|
||||
protected boolean minimal = false;
|
||||
protected boolean mostMinimal = false;
|
||||
protected String nickPath = null;
|
||||
protected String nickPrefix = "";
|
||||
protected boolean hasGravity;
|
||||
protected ArrayList<String> joinMessages = new ArrayList<>();
|
||||
protected InetSocketAddress inetAddr;
|
||||
protected final ArrayList<Bot> bots = new ArrayList<>();
|
||||
protected NickGenerator nickGenerator;
|
||||
protected final GravityTimer gravityTimer;
|
||||
|
||||
public BotManager(Info info) {
|
||||
this.info = info;
|
||||
this.inetAddr = createInetAddress();
|
||||
this.nickGenerator = new NickGenerator(info.getNickPath(), info.getNickPrefix(), info.isUseRealNicknames());
|
||||
public BotManager() {
|
||||
this.gravityTimer = new GravityTimer(this);
|
||||
}
|
||||
|
||||
// Methods
|
||||
protected InetSocketAddress createInetAddress() {
|
||||
protected InetSocketAddress createInetAddress(String address, int port) {
|
||||
try {
|
||||
InetAddress inetAddress = InetAddress.getByName(this.info.getAddress());
|
||||
return new InetSocketAddress(inetAddress.getHostAddress(), this.info.getPort());
|
||||
InetAddress inetAddress = InetAddress.getByName(address);
|
||||
return new InetSocketAddress(inetAddress.getHostAddress(), port);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Info getInfoBase() {
|
||||
return this.info;
|
||||
public boolean isMinimal() {
|
||||
return this.minimal;
|
||||
}
|
||||
|
||||
public boolean isMostMinimal() {
|
||||
return this.mostMinimal;
|
||||
}
|
||||
|
||||
public int getAutoRespawnDelay() {
|
||||
return this.autoRespawnDelay;
|
||||
}
|
||||
|
||||
public String getNickPath() {
|
||||
return this.nickPath;
|
||||
}
|
||||
|
||||
public String getNickPrefix() {
|
||||
return this.nickPrefix;
|
||||
}
|
||||
|
||||
public boolean hasGravity() {
|
||||
return this.hasGravity;
|
||||
}
|
||||
|
||||
public ArrayList<String> getJoinMessages() {
|
||||
return this.joinMessages;
|
||||
}
|
||||
|
||||
public InetSocketAddress getInetAddr() {
|
||||
return this.inetAddr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all loaded bots
|
||||
*
|
||||
* @return All loaded bots
|
||||
*/
|
||||
public ArrayList<Bot> getBots() {
|
||||
return this.bots;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get instance of nick generator
|
||||
*
|
||||
* @return Nick generator
|
||||
*/
|
||||
public NickGenerator getNickGenerator() {
|
||||
return this.nickGenerator;
|
||||
}
|
||||
|
||||
public void logBotCreated(String name) {
|
||||
Logger.info("Bot '" + name + "' created");
|
||||
}
|
||||
|
||||
public void logBotDisconnected(String name) {
|
||||
Logger.info("Bot '" + name + "' disconnected");
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a bot
|
||||
*
|
||||
* @param bot Bot to remove
|
||||
*/
|
||||
public void removeBot(Bot bot) {
|
||||
this.bots.remove(bot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect and remove a bot
|
||||
*
|
||||
* @param bot Bot to disconnect and remove
|
||||
*/
|
||||
public void disconnectBot(Bot bot) {
|
||||
bot.disconnect();
|
||||
this.bots.remove(bot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a bot by name
|
||||
*
|
||||
* @param text Name of player to get bot from
|
||||
* @return Bot from name
|
||||
*/
|
||||
@Nullable
|
||||
public Bot findBotByName(String text) {
|
||||
for (Bot bot : this.getBots()) {
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.shanebeestudios.mcbots.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class Info {
|
||||
|
||||
protected int autoRespawnDelay;
|
||||
protected boolean minimal = false;
|
||||
protected boolean mostMinimal = false;
|
||||
protected boolean useRealNicknames;
|
||||
protected String nickPath = null;
|
||||
protected String nickPrefix = "";
|
||||
protected String address;
|
||||
protected int port;
|
||||
protected boolean hasGravity;
|
||||
protected ArrayList<String> joinMessages = new ArrayList<>();
|
||||
|
||||
public Info() {
|
||||
}
|
||||
|
||||
public boolean isMinimal() {
|
||||
return this.minimal;
|
||||
}
|
||||
|
||||
public boolean isMostMinimal() {
|
||||
return this.mostMinimal;
|
||||
}
|
||||
|
||||
public int getAutoRespawnDelay() {
|
||||
return this.autoRespawnDelay;
|
||||
}
|
||||
|
||||
public boolean isUseRealNicknames() {
|
||||
return this.useRealNicknames;
|
||||
}
|
||||
|
||||
public String getNickPath() {
|
||||
return this.nickPath;
|
||||
}
|
||||
|
||||
public String getNickPrefix() {
|
||||
return this.nickPrefix;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public boolean hasGravity() {
|
||||
return this.hasGravity;
|
||||
}
|
||||
|
||||
public ArrayList<String> getJoinMessages() {
|
||||
return this.joinMessages;
|
||||
}
|
||||
|
||||
}
|
||||
+17
-13
@@ -1,4 +1,4 @@
|
||||
package com.shanebeestudios.mcbots.bot;
|
||||
package com.shanebeestudios.mcbots.api.bot;
|
||||
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.auth.service.SessionService;
|
||||
@@ -12,9 +12,8 @@ import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.github.steveice10.packetlib.tcp.TcpClientSession;
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.listener.ClientListener;
|
||||
import com.shanebeestudios.mcbots.listener.MessageListener;
|
||||
import com.shanebeestudios.mcbots.api.listener.BaseSessionListener;
|
||||
import com.shanebeestudios.mcbots.api.listener.MessageListener;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Instant;
|
||||
@@ -31,12 +30,13 @@ public class Bot {
|
||||
private double lastX, lastY, lastZ = -1;
|
||||
private boolean connected;
|
||||
private boolean manualDisconnecting = false;
|
||||
private BaseSessionListener sessionListener;
|
||||
|
||||
public Bot(BotManager botManager, String nickname, InetSocketAddress address, ProxyInfo proxy) {
|
||||
this.botManager = botManager;
|
||||
this.nickname = nickname;
|
||||
|
||||
Logger.info("Creating bot", nickname);
|
||||
botManager.logBotCreated(nickname);
|
||||
this.protocol = new MinecraftProtocol(nickname);
|
||||
this.client = new TcpClientSession(address.getHostString(), address.getPort(), this.protocol, proxy);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class Bot {
|
||||
this.botManager = botManager;
|
||||
this.nickname = authService.getUsername();
|
||||
|
||||
Logger.info("Creating bot", nickname);
|
||||
botManager.logBotCreated(this.nickname);
|
||||
this.protocol = new MinecraftProtocol(authService.getSelectedProfile(), authService.getAccessToken());
|
||||
|
||||
this.client = new TcpClientSession(address.getHostString(), address.getPort(), this.protocol, proxy);
|
||||
@@ -54,13 +54,17 @@ public class Bot {
|
||||
this.client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
|
||||
}
|
||||
|
||||
public void setupSessionListener(BaseSessionListener listener) {
|
||||
this.sessionListener = listener.init(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect the bot to the server
|
||||
*/
|
||||
public void connect() {
|
||||
new Thread(() -> {
|
||||
if (!this.botManager.getInfoBase().isMinimal()) {
|
||||
this.client.addListener(new ClientListener(this));
|
||||
if (!this.botManager.isMinimal() && this.sessionListener != null) {
|
||||
this.client.addListener(this.sessionListener);
|
||||
}
|
||||
this.client.connect();
|
||||
}).start();
|
||||
@@ -90,6 +94,10 @@ public class Bot {
|
||||
return this.manualDisconnecting;
|
||||
}
|
||||
|
||||
public boolean hasMainListener() {
|
||||
return this.hasMainListener;
|
||||
}
|
||||
|
||||
public void sendChat(String text) {
|
||||
// timeStamp will provide when this message was sent by the user. If this value was not set or was set to 0,
|
||||
// The server console will print out that the message was "expired". To avoid this, set timeStamp as now.
|
||||
@@ -107,13 +115,9 @@ public class Bot {
|
||||
this.client.send(packet);
|
||||
}
|
||||
|
||||
public boolean hasMainListener() {
|
||||
return this.hasMainListener;
|
||||
}
|
||||
|
||||
public void registerMainListener() {
|
||||
this.hasMainListener = true;
|
||||
if (this.botManager.getInfoBase().isMinimal()) return;
|
||||
if (this.botManager.isMinimal()) return;
|
||||
this.client.addListener(new MessageListener(this));
|
||||
}
|
||||
|
||||
+10
-11
@@ -1,9 +1,9 @@
|
||||
package com.shanebeestudios.mcbots.bot;
|
||||
package com.shanebeestudios.mcbots.api.bot;
|
||||
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.packetlib.ProxyInfo;
|
||||
import com.shanebeestudios.mcbots.standalone.StandaloneBotManager;
|
||||
import com.shanebeestudios.mcbots.standalone.StandaloneInfo;
|
||||
import com.shanebeestudios.mcbots.standalone.bot.StandaloneSessionListener;
|
||||
import com.shanebeestudios.mcbots.standalone.bot.StandaloneBotManager;
|
||||
import com.shanebeestudios.mcbots.api.generator.NickGenerator;
|
||||
import com.shanebeestudios.mcbots.api.generator.ProxyGenerator;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
@@ -15,7 +15,6 @@ import java.util.Random;
|
||||
public class BotLoader {
|
||||
|
||||
private final StandaloneBotManager standaloneLoader;
|
||||
private final StandaloneInfo standaloneInfo;
|
||||
private final int delayMin;
|
||||
private final int delayMax;
|
||||
private final Random random;
|
||||
@@ -24,22 +23,21 @@ public class BotLoader {
|
||||
|
||||
public BotLoader(StandaloneBotManager standaloneLoader) {
|
||||
this.standaloneLoader = standaloneLoader;
|
||||
this.standaloneInfo = standaloneLoader.getMainInfo();
|
||||
this.delayMin = this.standaloneInfo.getDelayMin();
|
||||
this.delayMax = this.standaloneInfo.getDelayMax();
|
||||
this.delayMin = standaloneLoader.getDelayMin();
|
||||
this.delayMax = standaloneLoader.getDelayMax();
|
||||
this.random = new Random();
|
||||
}
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public void spin() {
|
||||
int botCount = this.standaloneInfo.getBotCount();
|
||||
boolean minimal = this.standaloneInfo.isMinimal();
|
||||
boolean mostMinimal = this.standaloneInfo.isMostMinimal();
|
||||
int botCount = this.standaloneLoader.getBotCount();
|
||||
boolean minimal = this.standaloneLoader.isMinimal();
|
||||
boolean mostMinimal = this.standaloneLoader.isMostMinimal();
|
||||
InetSocketAddress inetAddr = this.standaloneLoader.getInetAddr();
|
||||
NickGenerator nickGenerator = this.standaloneLoader.getNickGenerator();
|
||||
AuthenticationService authService = this.standaloneLoader.getAuthenticationService();
|
||||
|
||||
ProxyGenerator proxyGenerator = this.standaloneInfo.getProxyGenerator();
|
||||
ProxyGenerator proxyGenerator = this.standaloneLoader.getProxyGenerator();
|
||||
int proxyCount = proxyGenerator != null ? proxyGenerator.getProxies().size() : 0;
|
||||
ArrayList<InetSocketAddress> proxies = proxyGenerator != null ? proxyGenerator.getProxies() : null;
|
||||
|
||||
@@ -70,6 +68,7 @@ public class BotLoader {
|
||||
} else {
|
||||
bot = new Bot(this.standaloneLoader, nickGenerator.nextNick(), inetAddr, proxyInfo);
|
||||
}
|
||||
bot.setupSessionListener(new StandaloneSessionListener());
|
||||
bot.connect();
|
||||
|
||||
if (!mostMinimal) this.standaloneLoader.getBots().add(bot);
|
||||
@@ -2,19 +2,17 @@ package com.shanebeestudios.mcbots.api.generator;
|
||||
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jline.utils.Log;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("FieldCanBeLocal")
|
||||
public class NickGenerator {
|
||||
@@ -42,7 +40,7 @@ public class NickGenerator {
|
||||
}
|
||||
} else {
|
||||
if (useRealNicknames && this.lines == null) {
|
||||
loadLines();
|
||||
loadDefaultFile();
|
||||
Logger.info("Loaded %s nicknames", this.linesSize);
|
||||
}
|
||||
this.real = useRealNicknames;
|
||||
@@ -54,44 +52,35 @@ public class NickGenerator {
|
||||
private int loadFromFile(String filePath) {
|
||||
this.lines = new ArrayList<>();
|
||||
try {
|
||||
Scanner scanner = new Scanner(new File(filePath));
|
||||
while (scanner.hasNextLine()) {
|
||||
try {
|
||||
String line = scanner.nextLine().trim();
|
||||
|
||||
//add only valid nicknames
|
||||
if (line.matches("^[a-zA-Z0-9_]{3,16}$")) {
|
||||
this.lines.add(line);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
scanner.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
Logger.error("Invalid nicknames list file path");
|
||||
System.exit(1);
|
||||
this.lines = getListFromReader(new FileReader(filePath));
|
||||
} catch (FileNotFoundException ignore) {
|
||||
Logger.error("Invalid nicknames list file path, loading default file!");
|
||||
loadDefaultFile();
|
||||
}
|
||||
|
||||
this.linesSize = this.lines.size();
|
||||
Logger.info("Loaded " + this.linesSize + " valid nicknames");
|
||||
this.real = true;
|
||||
|
||||
return this.linesSize;
|
||||
}
|
||||
|
||||
private void loadLines() {
|
||||
try (InputStream resource = getClass().getResourceAsStream("/files/nicks.txt")) {
|
||||
private void loadDefaultFile() {
|
||||
try (InputStream resource = getClass().getResourceAsStream("/nicks.txt")) {
|
||||
assert resource != null;
|
||||
lines = new BufferedReader(
|
||||
new InputStreamReader(resource, StandardCharsets.UTF_8)).lines().collect(Collectors.toList()
|
||||
);
|
||||
linesSize = lines.size();
|
||||
this.lines = getListFromReader(new InputStreamReader(resource, StandardCharsets.UTF_8));
|
||||
this.linesSize = this.lines.size();
|
||||
} catch (Exception e) {
|
||||
Logger.error(e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getListFromReader(Reader reader) {
|
||||
return new BufferedReader(reader)
|
||||
.lines()
|
||||
.filter(line -> line.matches("^[a-zA-Z0-9_]{3," + DEFAULT_NICK_LENGTH + "}$"))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private String generateRandom(int len) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < len; i++) {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.shanebeestudios.mcbots.api.listener;
|
||||
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BaseSessionListener extends SessionAdapter {
|
||||
|
||||
protected Bot bot;
|
||||
protected Session client;
|
||||
protected BotManager botManager;
|
||||
protected ArrayList<String> joinMessages;
|
||||
protected int autoRespawnDelay;
|
||||
|
||||
public BaseSessionListener init(Bot bot) {
|
||||
this.bot = bot;
|
||||
this.client = bot.getClient();
|
||||
this.botManager = bot.getBotManager();
|
||||
this.joinMessages = this.botManager.getJoinMessages();
|
||||
this.autoRespawnDelay = this.botManager.getAutoRespawnDelay();
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
+7
-36
@@ -1,29 +1,24 @@
|
||||
package com.shanebeestudios.mcbots.listener;
|
||||
package com.shanebeestudios.mcbots.api.listener;
|
||||
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectingEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketErrorEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketSendingEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionListener;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.standalone.StandaloneInfo;
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.util.Utils;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.standalone.bot.StandaloneBotManager;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
|
||||
public class MessageListener implements SessionListener {
|
||||
public class MessageListener extends SessionAdapter {
|
||||
|
||||
private final boolean coloredChat;
|
||||
|
||||
public MessageListener(Bot bot) {
|
||||
this.coloredChat = !(bot.getBotManager().getInfoBase() instanceof StandaloneInfo standaloneInfo) || standaloneInfo.isColoredChat();
|
||||
this.coloredChat = !(bot.getBotManager() instanceof StandaloneBotManager botManager) || botManager.isColoredChat();
|
||||
Logger.info("MessageListener registered for: " + bot.getNickname());
|
||||
}
|
||||
|
||||
@@ -64,28 +59,4 @@ public class MessageListener implements SessionListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSending(PacketSendingEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSent(Session session, Packet packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetError(PacketErrorEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connected(ConnectedEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnecting(DisconnectingEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(DisconnectedEvent event) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.shanebeestudios.mcbots.api.timer;
|
||||
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
|
||||
import java.util.Timer;
|
||||
|
||||
@@ -13,9 +13,14 @@ import net.kyori.adventure.text.format.Style;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -25,7 +30,7 @@ import static com.diogonunes.jcolor.Ansi.colorize;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class Utils {
|
||||
|
||||
private static JsonObject lang;
|
||||
private static JsonObject MINECRAFT_LANG;
|
||||
|
||||
public static String getFullText(TextComponent message, boolean colored) {
|
||||
if (!message.children().isEmpty()) {
|
||||
@@ -77,13 +82,10 @@ public class Utils {
|
||||
} else {
|
||||
outString.append(message.content());
|
||||
}
|
||||
|
||||
return outString.toString();
|
||||
} else {
|
||||
if (sender == null)
|
||||
return message.content();
|
||||
else
|
||||
return sender.content() + " : " + message.content();
|
||||
if (sender == null) return message.content();
|
||||
else return sender.content() + " : " + message.content();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,11 +127,11 @@ public class Utils {
|
||||
}
|
||||
|
||||
public static String translate(TranslatableComponent message) {
|
||||
if (lang == null) {
|
||||
if (MINECRAFT_LANG == null) {
|
||||
try {
|
||||
InputStream resource = Utils.class.getResourceAsStream("/files/lang.json");
|
||||
if (resource == null) return null;
|
||||
lang = new JsonParser().parse(new InputStreamReader(resource)).getAsJsonObject();
|
||||
MINECRAFT_LANG = new JsonParser().parse(new InputStreamReader(resource)).getAsJsonObject();
|
||||
} catch (Exception e) {
|
||||
Logger.critical(e);
|
||||
}
|
||||
@@ -138,7 +140,7 @@ public class Utils {
|
||||
String key = message.key();
|
||||
String base = key;
|
||||
|
||||
JsonElement value = lang.get(key);
|
||||
JsonElement value = MINECRAFT_LANG.get(key);
|
||||
if (value != null) {
|
||||
base = value.getAsString();
|
||||
}
|
||||
@@ -158,4 +160,23 @@ public class Utils {
|
||||
return base;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a UUID from a player name
|
||||
*
|
||||
* @param playerName Player name to fetch UUID from
|
||||
* @return UUID from player name if available
|
||||
*/
|
||||
@Nullable
|
||||
public static String nameToUUID(String playerName) {
|
||||
try {
|
||||
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + playerName);
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject json = (JSONObject) parser.parse(new InputStreamReader(url.openStream()));
|
||||
return (String) json.get("id");
|
||||
} catch (ParseException | IOException ignore) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.shanebeestudios.mcbots.api.util.logging;
|
||||
import com.shanebeestudios.mcbots.plugin.McBotPlugin;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -27,6 +28,10 @@ public class PluginLogger extends BaseLogger {
|
||||
return ChatColor.translateAlternateColorCodes('&', string);
|
||||
}
|
||||
|
||||
public static void logToSender(CommandSender sender, String format, Object... objects) {
|
||||
sender.sendMessage(getColString(PREFIX + " &7" + String.format(format, objects)));
|
||||
}
|
||||
|
||||
private final Logger bukkitLogger;
|
||||
|
||||
public PluginLogger(McBotPlugin plugin) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.shanebeestudios.mcbots.plugin;
|
||||
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.plugin.bot.PluginBotManager;
|
||||
import com.shanebeestudios.mcbots.plugin.command.Command;
|
||||
import dev.jorel.commandapi.CommandAPI;
|
||||
import dev.jorel.commandapi.CommandAPIBukkitConfig;
|
||||
@@ -9,11 +10,11 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class McBotPlugin extends JavaPlugin {
|
||||
|
||||
private static McBotPlugin instance;
|
||||
private boolean commandApiCanLoad;
|
||||
private PluginInfo pluginInfo;
|
||||
private PluginBotManager pluginBotManager;
|
||||
|
||||
@Override
|
||||
@@ -34,8 +35,8 @@ public class McBotPlugin extends JavaPlugin {
|
||||
Logger.setupBukkitLogging(this);
|
||||
|
||||
PluginManager pluginManager = Bukkit.getPluginManager();
|
||||
if (!commandApiCanLoad) {
|
||||
Logger.error("CommandAPI could not be loaded"); // TODO better message
|
||||
if (!this.commandApiCanLoad) {
|
||||
Logger.error("CommandAPI could not be loaded, plugin disabling!");
|
||||
pluginManager.disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
@@ -46,6 +47,7 @@ public class McBotPlugin extends JavaPlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
loadNicknameFile();
|
||||
setupBotLogic();
|
||||
setupCommand();
|
||||
|
||||
@@ -59,9 +61,12 @@ public class McBotPlugin extends JavaPlugin {
|
||||
CommandAPI.onDisable();
|
||||
}
|
||||
|
||||
private void loadNicknameFile() {
|
||||
this.saveResource("nicks.txt", false);
|
||||
}
|
||||
|
||||
private void setupBotLogic() {
|
||||
this.pluginInfo = new PluginInfo();
|
||||
this.pluginBotManager = new PluginBotManager(this.pluginInfo);
|
||||
this.pluginBotManager = new PluginBotManager();
|
||||
}
|
||||
|
||||
private void setupCommand() {
|
||||
@@ -74,10 +79,6 @@ public class McBotPlugin extends JavaPlugin {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public PluginInfo getPluginInfo() {
|
||||
return this.pluginInfo;
|
||||
}
|
||||
|
||||
public PluginBotManager getPluginBotManager() {
|
||||
return this.pluginBotManager;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,23 @@
|
||||
package com.shanebeestudios.mcbots.plugin;
|
||||
|
||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||
import com.shanebeestudios.mcbots.api.util.Utils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@SuppressWarnings({"FieldCanBeLocal", "unused"})
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
private final McBotPlugin plugin;
|
||||
private final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||
|
||||
private final Map<UUID,PlayerProfile> profileMap = new HashMap<>();
|
||||
private final Map<UUID, PlayerProfile> profileMap = new HashMap<>();
|
||||
|
||||
public PlayerListener(McBotPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
@@ -34,8 +26,7 @@ public class PlayerListener implements Listener {
|
||||
@EventHandler
|
||||
private void onPreJoin(AsyncPlayerPreLoginEvent event) {
|
||||
String name = event.getName();
|
||||
|
||||
String uuidString = nameToUUID(name);
|
||||
String uuidString = Utils.nameToUUID(name);
|
||||
if (uuidString != null) {
|
||||
UUID uuid = UUID.fromString(uuidString.replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5"));
|
||||
PlayerProfile profile = Bukkit.createProfile(uuid, name);
|
||||
@@ -54,16 +45,4 @@ public class PlayerListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String nameToUUID(String playerName) {
|
||||
try {
|
||||
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + playerName);
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject json = (JSONObject) parser.parse(new InputStreamReader(url.openStream()));
|
||||
return (String) json.get("id");
|
||||
} catch (ParseException | IOException ignore) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.shanebeestudios.mcbots.plugin;
|
||||
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
import com.shanebeestudios.mcbots.api.Info;
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PluginBotManager extends BotManager {
|
||||
|
||||
public PluginBotManager(Info info) {
|
||||
super(info);
|
||||
}
|
||||
|
||||
public void createBot(@Nullable String name) {
|
||||
if (name != null && name.length() > 16) {
|
||||
return;
|
||||
}
|
||||
|
||||
String botname = name != null ? name : getNickGenerator().nextNick();
|
||||
Bot bot = new Bot(this, botname, getInetAddr(), null);
|
||||
this.bots.add(bot);
|
||||
bot.connect();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.shanebeestudios.mcbots.plugin;
|
||||
|
||||
import com.shanebeestudios.mcbots.api.Info;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class PluginInfo extends Info {
|
||||
|
||||
public PluginInfo() {
|
||||
super();
|
||||
this.autoRespawnDelay = 3000;
|
||||
this.useRealNicknames = true;
|
||||
this.address = getServerAddress();
|
||||
this.port = Bukkit.getPort();
|
||||
this.hasGravity = true;
|
||||
}
|
||||
|
||||
private String getServerAddress() {
|
||||
try {
|
||||
return Inet4Address.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.shanebeestudios.mcbots.plugin.bot;
|
||||
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.generator.NickGenerator;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* Bot manager for server plugin
|
||||
*/
|
||||
public class PluginBotManager extends BotManager {
|
||||
|
||||
public PluginBotManager() {
|
||||
this.autoRespawnDelay = 3000;
|
||||
this.inetAddr = createInetAddress(getServerAddress(), Bukkit.getPort());
|
||||
this.nickGenerator = new NickGenerator("plugins/McBots/nicks.txt", this.nickPrefix, true);
|
||||
this.hasGravity = true;
|
||||
this.gravityTimer.startTimer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a bot
|
||||
*
|
||||
* @param name Name of bot or null to create random named bot
|
||||
* Name must be between 1 and 16 characters
|
||||
* @return Bot if was created, else null
|
||||
*/
|
||||
@Nullable
|
||||
public Bot createBot(@Nullable String name) {
|
||||
if (name != null && name.length() > 16) return null;
|
||||
|
||||
String botname = name != null ? name : getNickGenerator().nextNick();
|
||||
Bot bot = new Bot(this, botname, getInetAddr(), null);
|
||||
this.bots.add(bot);
|
||||
bot.setupSessionListener(new PluginSessionListener());
|
||||
bot.connect();
|
||||
return bot;
|
||||
}
|
||||
|
||||
private String getServerAddress() {
|
||||
try {
|
||||
return Inet4Address.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logBotCreated(String name) {
|
||||
Logger.info("Bot '&b" + name + "&7' created");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logBotDisconnected(String name) {
|
||||
Logger.info("Bot '&b" + name + "&7' disconnected");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.shanebeestudios.mcbots.plugin.bot;
|
||||
|
||||
import com.github.steveice10.mc.protocol.data.game.ClientCommand;
|
||||
import com.github.steveice10.mc.protocol.data.game.level.notify.GameEvent;
|
||||
import com.github.steveice10.mc.protocol.data.game.level.notify.RespawnScreenValue;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerCombatKillPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundGameEventPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundClientCommandPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.shanebeestudios.mcbots.api.listener.BaseSessionListener;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public class PluginSessionListener extends BaseSessionListener {
|
||||
|
||||
@Override
|
||||
public void packetReceived(Session session, Packet packet) {
|
||||
if (packet instanceof ClientboundGameEventPacket gameEventPacket) {
|
||||
if (gameEventPacket.getNotification() == GameEvent.ENABLE_RESPAWN_SCREEN) {
|
||||
RespawnScreenValue respawnScreenValue = (RespawnScreenValue) gameEventPacket.getValue();
|
||||
if (respawnScreenValue == RespawnScreenValue.IMMEDIATE_RESPAWN) {
|
||||
this.autoRespawnDelay = 0;
|
||||
} else {
|
||||
this.autoRespawnDelay = this.botManager.getAutoRespawnDelay();
|
||||
}
|
||||
}
|
||||
} else if (packet instanceof ClientboundLoginPacket loginPacket) {
|
||||
if (!loginPacket.isEnableRespawnScreen()) {
|
||||
this.autoRespawnDelay = 0;
|
||||
}
|
||||
this.bot.setConnected(true);
|
||||
|
||||
// Might implement this later
|
||||
if (!this.joinMessages.isEmpty()) {
|
||||
for (String msg : joinMessages) {
|
||||
this.bot.sendChat(msg);
|
||||
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (packet instanceof ClientboundPlayerPositionPacket positionPacket) {
|
||||
this.bot.setLastPosition(positionPacket.getX(), positionPacket.getY(), positionPacket.getZ());
|
||||
this.client.send(new ServerboundAcceptTeleportationPacket(positionPacket.getTeleportId()));
|
||||
} else if (packet instanceof ClientboundPlayerCombatKillPacket) {
|
||||
if (this.autoRespawnDelay >= 0) {
|
||||
new Timer().schedule(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
PluginSessionListener.this.client.send(new ServerboundClientCommandPacket(ClientCommand.RESPAWN));
|
||||
}
|
||||
}, this.autoRespawnDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(DisconnectedEvent event) {
|
||||
this.bot.setConnected(false);
|
||||
this.botManager.logBotDisconnected(this.bot.getNickname());
|
||||
this.botManager.removeBot(this.bot);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.shanebeestudios.mcbots.plugin.command;
|
||||
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.PluginLogger;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.plugin.McBotPlugin;
|
||||
import com.shanebeestudios.mcbots.plugin.PluginBotManager;
|
||||
import com.shanebeestudios.mcbots.plugin.bot.PluginBotManager;
|
||||
import dev.jorel.commandapi.CommandTree;
|
||||
import dev.jorel.commandapi.arguments.EntitySelectorArgument;
|
||||
import dev.jorel.commandapi.arguments.GreedyStringArgument;
|
||||
@@ -30,14 +31,19 @@ public class Command {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void registerCommand() {
|
||||
CommandTree command = new CommandTree("mcbot")
|
||||
CommandTree command = new CommandTree("mcbots")
|
||||
// Create a bot
|
||||
.then(new LiteralArgument("create")
|
||||
.then(new LiteralArgument("named")
|
||||
.then(new StringArgument("name")
|
||||
.executes((sender, args) -> {
|
||||
String name = (String) args.get("name");
|
||||
this.botManager.createBot(name);
|
||||
Bot bot = this.botManager.createBot(name);
|
||||
if (bot != null) {
|
||||
PluginLogger.logToSender(sender, "Created new bot '&b" + bot.getNickname() + "&7'");
|
||||
} else {
|
||||
PluginLogger.logToSender(sender, "&cFailed to create bot '&b" + name + "&7'");
|
||||
}
|
||||
})))
|
||||
.then(new LiteralArgument("random")
|
||||
.then(new IntegerArgument("amount")
|
||||
@@ -49,7 +55,12 @@ public class Command {
|
||||
long delay = (int) args.getOrDefault("delay-ticks", 20);
|
||||
for (int i = 0; i < amount; i++) {
|
||||
this.scheduler.runTaskLater(this.plugin, () -> {
|
||||
this.botManager.createBot(null);
|
||||
Bot bot = this.botManager.createBot(null);
|
||||
if (bot != null) {
|
||||
PluginLogger.logToSender(sender, "Created new bot '&b" + bot.getNickname() + "&7'");
|
||||
} else {
|
||||
PluginLogger.logToSender(sender, "&cFailed to create random bot!");
|
||||
}
|
||||
}, delay * i);
|
||||
}
|
||||
})))))
|
||||
@@ -64,6 +75,7 @@ public class Command {
|
||||
Bot bot = this.botManager.findBotByName(player.getName());
|
||||
if (bot != null) {
|
||||
this.botManager.disconnectBot(bot);
|
||||
PluginLogger.logToSender(sender, "Removed bot &7'" + bot.getNickname() + "&7'");
|
||||
}
|
||||
});
|
||||
})))
|
||||
@@ -79,9 +91,7 @@ public class Command {
|
||||
assert players != null;
|
||||
players.forEach(player -> {
|
||||
Bot bot = this.botManager.findBotByName(player.getName());
|
||||
if (bot != null) {
|
||||
bot.sendChat(message);
|
||||
}
|
||||
if (bot != null) bot.sendChat(message);
|
||||
});
|
||||
}))));
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.shanebeestudios.mcbots.standalone;
|
||||
|
||||
import com.shanebeestudios.mcbots.standalone.bot.StandaloneBotManager;
|
||||
import org.apache.commons.cli.Option;
|
||||
import org.apache.commons.cli.Options;
|
||||
|
||||
@@ -36,8 +37,7 @@ public class Main {
|
||||
|
||||
options.addOption("ar", "auto-respawn", true, "Set autorespawn delay (-1 to disable)");
|
||||
|
||||
StandaloneInfo standaloneInfo = new StandaloneInfo(options, args);
|
||||
new StandaloneBotManager(standaloneInfo);
|
||||
new StandaloneBotManager(options, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
package com.shanebeestudios.mcbots.standalone;
|
||||
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
import com.shanebeestudios.mcbots.api.ServerInfo;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.bot.BotLoader;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class StandaloneBotManager extends BotManager {
|
||||
|
||||
private final StandaloneInfo standaloneInfo;
|
||||
private final HashSet<Bot> controlledBots = new HashSet<>();
|
||||
private final AuthenticationService authenticationService;
|
||||
private final BotLoader botLoader;
|
||||
|
||||
private boolean isMainListenerMissing = true;
|
||||
|
||||
public StandaloneBotManager(StandaloneInfo standaloneInfo) {
|
||||
super(standaloneInfo);
|
||||
this.standaloneInfo = standaloneInfo;
|
||||
|
||||
getAndPrintServerInfo(getInetAddr());
|
||||
|
||||
if (standaloneInfo.isOnline()) {
|
||||
OnlineService onlineService = new OnlineService(standaloneInfo);
|
||||
this.authenticationService = onlineService.getOnlineAuthenticationService();
|
||||
} else {
|
||||
this.authenticationService = null;
|
||||
}
|
||||
|
||||
// Load bots
|
||||
this.botLoader = new BotLoader(this);
|
||||
this.botLoader.spin();
|
||||
if (standaloneInfo.hasGravity()) {
|
||||
this.gravityTimer.startTimer();
|
||||
}
|
||||
|
||||
// Load console reader
|
||||
ConsoleReader consoleReader = new ConsoleReader(this);
|
||||
consoleReader.start();
|
||||
}
|
||||
|
||||
private void getAndPrintServerInfo(InetSocketAddress inetAddr) {
|
||||
//print info
|
||||
Logger.info("Server Address:", inetAddr.getHostString());
|
||||
Logger.info("Server Port: " + inetAddr.getPort());
|
||||
Logger.info("Bot count: " + standaloneInfo.getBotCount());
|
||||
|
||||
//get and print server info
|
||||
ServerInfo serverInfo = new ServerInfo(inetAddr);
|
||||
serverInfo.requestInfo();
|
||||
ServerStatusInfo statusInfo = serverInfo.getStatusInfo();
|
||||
if (statusInfo != null) {
|
||||
Logger.info(
|
||||
"Server version: "
|
||||
+ statusInfo.getVersionInfo().getVersionName()
|
||||
+ " (" + statusInfo.getVersionInfo().getProtocolVersion()
|
||||
+ ")"
|
||||
);
|
||||
Logger.info("Player Count: " + statusInfo.getPlayerInfo().getOnlinePlayers()
|
||||
+ " / " + statusInfo.getPlayerInfo().getMaxPlayers());
|
||||
Logger.info();
|
||||
} else {
|
||||
Logger.warn("There was an error retrieving server status information. The server may be offline or running on a different version.");
|
||||
}
|
||||
}
|
||||
|
||||
public void renewMainListener() {
|
||||
this.bots.get(0).registerMainListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBot(Bot bot) {
|
||||
super.removeBot(bot);
|
||||
controlledBots.remove(bot);
|
||||
if (bot.hasMainListener()) {
|
||||
Logger.info("Bot with MainListener removed");
|
||||
this.isMainListenerMissing = true;
|
||||
}
|
||||
if (!this.bots.isEmpty()) {
|
||||
if (this.isMainListenerMissing && !this.standaloneInfo.isMinimal()) {
|
||||
Logger.info("Renewing MainListener");
|
||||
renewMainListener();
|
||||
this.isMainListenerMissing = false;
|
||||
}
|
||||
} else {
|
||||
if (this.botLoader.getTriedToConnect() == this.standaloneInfo.getBotCount()) {
|
||||
Logger.error("All bots disconnected, exiting");
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getPrompt() {
|
||||
int count = this.getControlledBots().size();
|
||||
if (count == 0) {
|
||||
return "ALL";
|
||||
} else if (count == 1) {
|
||||
//If controlling only one bot, return its nickname
|
||||
return getControlledBots().iterator().next().getNickname();
|
||||
} else {
|
||||
return count + " BOTS";
|
||||
}
|
||||
}
|
||||
|
||||
public StandaloneInfo getMainInfo() {
|
||||
return this.standaloneInfo;
|
||||
}
|
||||
|
||||
public HashSet<Bot> getControlledBots() {
|
||||
return this.controlledBots;
|
||||
}
|
||||
|
||||
public AuthenticationService getAuthenticationService() {
|
||||
return this.authenticationService;
|
||||
}
|
||||
|
||||
public boolean isMainListenerMissing() {
|
||||
return this.isMainListenerMissing;
|
||||
}
|
||||
|
||||
public void setMainListenerMissing(boolean missing) {
|
||||
this.isMainListenerMissing = missing;
|
||||
}
|
||||
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package com.shanebeestudios.mcbots.standalone;
|
||||
package com.shanebeestudios.mcbots.standalone.bot;
|
||||
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
+6
-5
@@ -1,4 +1,4 @@
|
||||
package com.shanebeestudios.mcbots.standalone;
|
||||
package com.shanebeestudios.mcbots.standalone.bot;
|
||||
|
||||
import com.github.steveice10.mc.auth.exception.request.AuthPendingException;
|
||||
import com.github.steveice10.mc.auth.exception.request.RequestException;
|
||||
@@ -6,6 +6,7 @@ import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.auth.service.MsaAuthenticationService;
|
||||
import com.github.steveice10.mc.auth.util.HTTP;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.standalone.bot.StandaloneBotManager;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Proxy;
|
||||
@@ -16,15 +17,15 @@ import java.util.Map;
|
||||
public class OnlineService {
|
||||
|
||||
private final String clientId = "8bef943e-5a63-429e-a93a-96391d2e32a9";
|
||||
private final StandaloneInfo standaloneInfo;
|
||||
private final StandaloneBotManager botManager;
|
||||
|
||||
public OnlineService(StandaloneInfo standaloneInfo) {
|
||||
this.standaloneInfo = standaloneInfo;
|
||||
public OnlineService(StandaloneBotManager botManager) {
|
||||
this.botManager = botManager;
|
||||
}
|
||||
|
||||
public AuthenticationService getOnlineAuthenticationService() {
|
||||
Logger.warn("Online mode enabled. The bot count will be set to 1.");
|
||||
this.standaloneInfo.setBotCount(1);
|
||||
this.botManager.setBotCount(1);
|
||||
|
||||
// Create request parameters map
|
||||
Map<String, String> params = new HashMap<>();
|
||||
+124
-11
@@ -1,8 +1,15 @@
|
||||
package com.shanebeestudios.mcbots.standalone;
|
||||
package com.shanebeestudios.mcbots.standalone.bot;
|
||||
|
||||
import com.shanebeestudios.mcbots.api.Info;
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
import com.shanebeestudios.mcbots.api.ServerInfo;
|
||||
import com.shanebeestudios.mcbots.api.generator.NickGenerator;
|
||||
import com.shanebeestudios.mcbots.api.generator.ProxyGenerator;
|
||||
import com.shanebeestudios.mcbots.api.util.Pair;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.api.bot.Bot;
|
||||
import com.shanebeestudios.mcbots.api.bot.BotLoader;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.CommandLineParser;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
@@ -16,18 +23,24 @@ import org.xbill.DNS.SRVRecord;
|
||||
import org.xbill.DNS.TextParseException;
|
||||
import org.xbill.DNS.Type;
|
||||
|
||||
public class StandaloneInfo extends Info {
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashSet;
|
||||
|
||||
private int botCount;
|
||||
public class StandaloneBotManager extends BotManager {
|
||||
|
||||
protected int botCount;
|
||||
private final int delayMin;
|
||||
private final int delayMax;
|
||||
private final boolean coloredChat;
|
||||
private final boolean online;
|
||||
|
||||
private ProxyGenerator proxyGenerator;
|
||||
private final HashSet<Bot> controlledBots = new HashSet<>();
|
||||
private final AuthenticationService authenticationService;
|
||||
private final BotLoader botLoader;
|
||||
|
||||
public StandaloneInfo(Options options, String[] args) {
|
||||
super();
|
||||
private boolean isMainListenerMissing = true;
|
||||
|
||||
public StandaloneBotManager(Options options, String[] args) {
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
CommandLine cmd = null;
|
||||
|
||||
@@ -35,7 +48,7 @@ public class StandaloneInfo extends Info {
|
||||
cmd = parser.parse(options, args);
|
||||
} catch (ParseException e) {
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
formatter.printHelp("mcbots", e.getMessage(), options, "\nhttps://github.com/crpmax/mc-bots", true);
|
||||
formatter.printHelp("mcbots", e.getMessage(), options, "\nhttps://github.com/ShaneBeee/mc-bots", true);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
@@ -66,8 +79,7 @@ public class StandaloneInfo extends Info {
|
||||
|
||||
String address = cmd.getOptionValue('s');
|
||||
Pair<String, Integer> addressPort = parseAddress(address);
|
||||
this.address = addressPort.getFirst();
|
||||
this.port = addressPort.getSecond();
|
||||
this.inetAddr = createInetAddress(addressPort.getFirst(), addressPort.getSecond());
|
||||
|
||||
this.coloredChat = !cmd.hasOption('n');
|
||||
|
||||
@@ -79,12 +91,34 @@ public class StandaloneInfo extends Info {
|
||||
}
|
||||
}
|
||||
|
||||
this.useRealNicknames = cmd.hasOption('r');
|
||||
boolean useRealNick = cmd.hasOption('r');
|
||||
this.nickPath = cmd.getOptionValue("nicks", null);
|
||||
this.nickPrefix = cmd.getOptionValue('p', "");
|
||||
|
||||
this.nickGenerator = new NickGenerator(this.nickPath, this.nickPrefix, useRealNick);
|
||||
|
||||
this.online = cmd.hasOption("o");
|
||||
this.hasGravity = cmd.hasOption("g");
|
||||
|
||||
getAndPrintServerInfo(getInetAddr());
|
||||
|
||||
if (isOnline()) {
|
||||
OnlineService onlineService = new OnlineService(this);
|
||||
this.authenticationService = onlineService.getOnlineAuthenticationService();
|
||||
} else {
|
||||
this.authenticationService = null;
|
||||
}
|
||||
|
||||
// Load bots
|
||||
this.botLoader = new BotLoader(this);
|
||||
this.botLoader.spin();
|
||||
if (hasGravity()) {
|
||||
this.gravityTimer.startTimer();
|
||||
}
|
||||
|
||||
// Load console reader
|
||||
ConsoleReader consoleReader = new ConsoleReader(this);
|
||||
consoleReader.start();
|
||||
}
|
||||
|
||||
private Pair<String, Integer> parseAddress(String address) {
|
||||
@@ -123,6 +157,69 @@ public class StandaloneInfo extends Info {
|
||||
return new Pair<>(delayMin, delayMax);
|
||||
}
|
||||
|
||||
private void getAndPrintServerInfo(InetSocketAddress inetAddr) {
|
||||
//print info
|
||||
Logger.info("Server Address:", inetAddr.getHostString());
|
||||
Logger.info("Server Port: " + inetAddr.getPort());
|
||||
Logger.info("Bot count: " + getBotCount());
|
||||
|
||||
//get and print server info
|
||||
ServerInfo serverInfo = new ServerInfo(inetAddr);
|
||||
serverInfo.requestInfo();
|
||||
ServerStatusInfo statusInfo = serverInfo.getStatusInfo();
|
||||
if (statusInfo != null) {
|
||||
Logger.info(
|
||||
"Server version: "
|
||||
+ statusInfo.getVersionInfo().getVersionName()
|
||||
+ " (" + statusInfo.getVersionInfo().getProtocolVersion()
|
||||
+ ")"
|
||||
);
|
||||
Logger.info("Player Count: " + statusInfo.getPlayerInfo().getOnlinePlayers()
|
||||
+ " / " + statusInfo.getPlayerInfo().getMaxPlayers());
|
||||
Logger.info();
|
||||
} else {
|
||||
Logger.warn("There was an error retrieving server status information. The server may be offline or running on a different version.");
|
||||
}
|
||||
}
|
||||
|
||||
public void renewMainListener() {
|
||||
this.bots.get(0).registerMainListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBot(Bot bot) {
|
||||
super.removeBot(bot);
|
||||
controlledBots.remove(bot);
|
||||
if (bot.hasMainListener()) {
|
||||
Logger.info("Bot with MainListener removed");
|
||||
this.isMainListenerMissing = true;
|
||||
}
|
||||
if (!this.bots.isEmpty()) {
|
||||
if (this.isMainListenerMissing && !isMinimal()) {
|
||||
Logger.info("Renewing MainListener");
|
||||
renewMainListener();
|
||||
this.isMainListenerMissing = false;
|
||||
}
|
||||
} else {
|
||||
if (this.botLoader.getTriedToConnect() == getBotCount()) {
|
||||
Logger.error("All bots disconnected, exiting");
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getPrompt() {
|
||||
int count = this.getControlledBots().size();
|
||||
if (count == 0) {
|
||||
return "ALL";
|
||||
} else if (count == 1) {
|
||||
//If controlling only one bot, return its nickname
|
||||
return getControlledBots().iterator().next().getNickname();
|
||||
} else {
|
||||
return count + " BOTS";
|
||||
}
|
||||
}
|
||||
|
||||
public int getBotCount() {
|
||||
return this.botCount;
|
||||
}
|
||||
@@ -152,4 +249,20 @@ public class StandaloneInfo extends Info {
|
||||
return this.proxyGenerator;
|
||||
}
|
||||
|
||||
public HashSet<Bot> getControlledBots() {
|
||||
return this.controlledBots;
|
||||
}
|
||||
|
||||
public AuthenticationService getAuthenticationService() {
|
||||
return this.authenticationService;
|
||||
}
|
||||
|
||||
public boolean isMainListenerMissing() {
|
||||
return this.isMainListenerMissing;
|
||||
}
|
||||
|
||||
public void setMainListenerMissing(boolean missing) {
|
||||
this.isMainListenerMissing = missing;
|
||||
}
|
||||
|
||||
}
|
||||
+8
-25
@@ -1,4 +1,4 @@
|
||||
package com.shanebeestudios.mcbots.listener;
|
||||
package com.shanebeestudios.mcbots.standalone.bot;
|
||||
|
||||
import com.github.steveice10.mc.protocol.data.UnexpectedEncryptionException;
|
||||
import com.github.steveice10.mc.protocol.data.game.ClientCommand;
|
||||
@@ -12,35 +12,18 @@ import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCl
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.shanebeestudios.mcbots.api.BotManager;
|
||||
import com.shanebeestudios.mcbots.api.listener.BaseSessionListener;
|
||||
import com.shanebeestudios.mcbots.api.util.Utils;
|
||||
import com.shanebeestudios.mcbots.api.util.logging.Logger;
|
||||
import com.shanebeestudios.mcbots.bot.Bot;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class ClientListener extends SessionAdapter {
|
||||
|
||||
private final Bot bot;
|
||||
private final Session client;
|
||||
private final BotManager botManager;
|
||||
private final ArrayList<String> joinMessages;
|
||||
private int autoRespawnDelay;
|
||||
|
||||
public ClientListener(Bot bot) {
|
||||
this.bot = bot;
|
||||
this.client = bot.getClient();
|
||||
this.botManager = bot.getBotManager();
|
||||
this.joinMessages = this.botManager.getInfoBase().getJoinMessages();
|
||||
this.autoRespawnDelay = this.botManager.getInfoBase().getAutoRespawnDelay();
|
||||
}
|
||||
@SuppressWarnings({"CallToPrintStackTrace", "DuplicatedCode"})
|
||||
public class StandaloneSessionListener extends BaseSessionListener {
|
||||
|
||||
@Override
|
||||
public void packetReceived(Session session, Packet packet) {
|
||||
@@ -50,7 +33,7 @@ public class ClientListener extends SessionAdapter {
|
||||
if (value == RespawnScreenValue.IMMEDIATE_RESPAWN) {
|
||||
this.autoRespawnDelay = 0;
|
||||
} else {
|
||||
this.autoRespawnDelay = this.botManager.getInfoBase().getAutoRespawnDelay();
|
||||
this.autoRespawnDelay = this.botManager.getAutoRespawnDelay();
|
||||
}
|
||||
}
|
||||
} else if (packet instanceof ClientboundLoginPacket loginPacket) {
|
||||
@@ -61,7 +44,7 @@ public class ClientListener extends SessionAdapter {
|
||||
Logger.info(this.bot.getNickname() + " connected");
|
||||
|
||||
if (!this.joinMessages.isEmpty()) {
|
||||
for (String msg : joinMessages) {
|
||||
for (String msg : this.joinMessages) {
|
||||
this.bot.sendChat(msg);
|
||||
|
||||
try {
|
||||
@@ -80,7 +63,7 @@ public class ClientListener extends SessionAdapter {
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
ClientListener.this.client.send(new ServerboundClientCommandPacket(ClientCommand.RESPAWN));
|
||||
StandaloneSessionListener.this.client.send(new ServerboundClientCommandPacket(ClientCommand.RESPAWN));
|
||||
}
|
||||
}, this.autoRespawnDelay);
|
||||
}
|
||||
@@ -90,7 +73,7 @@ public class ClientListener extends SessionAdapter {
|
||||
@Override
|
||||
public void disconnected(DisconnectedEvent event) {
|
||||
this.bot.setConnected(false);
|
||||
Logger.info(this.bot.getNickname() + " disconnected");
|
||||
this.botManager.logBotDisconnected(this.bot.getNickname());
|
||||
|
||||
// Do not write disconnect reason if disconnected by command
|
||||
if (!this.bot.isManualDisconnecting()) {
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user