Skip to content

Instantly share code, notes, and snippets.

@PaulBGD
Created June 5, 2021 15:55
Show Gist options
  • Save PaulBGD/fa435e930b2673bf48759b1641560e29 to your computer and use it in GitHub Desktop.
Save PaulBGD/fa435e930b2673bf48759b1641560e29 to your computer and use it in GitHub Desktop.
Botting stuff
const mineflayer = require("mineflayer");
const count = +process.argv[2] || 50;
const niceness = +process.argv[3] || 1.25;
const type = process.argv[4] || "sequential";
console.log(process.argv, {count, niceness, type});
async function addBot(i) {
const name = `Bot-${i}`;
const hoststring = process.env.HOST || "localhost:25565";
const host = hoststring.split(":")[0];
const port = +hoststring.split(":")[1];
const bot = mineflayer.createBot({
host, port,
username: name,
version: false,
});
return new Promise((resolve, reject) => {
const start = Date.now();
bot.connected = true;
bot.on("error", err => {
if (err.errno === "ECONNREFUSED") {
console.error("Server shut down, stopping bot.");
process.exit(0);
}
console.error("Error", name, err);
reject(err);
});
bot.on("login", () => {
console.log("Logged in", name);
});
bot.once("spawn", () => {
resolve({timeTaken: Date.now() - start, bot});
})
bot.on("end", () => {
bot.connected = false;
console.log("Disconnected", name)
});
});
}
const startTime = Date.now();
async function startSeq() {
let i = 0;
let bots = [];
for (; i < count; i++) {
const {timeTaken, bot} = await addBot(i);
bots.push(bot);
console.log("Took", timeTaken);
await new Promise(resolve => setTimeout(resolve, timeTaken * niceness));
}
console.log("Total took", (Date.now() - startTime) / 1000 + "s");
while (true) {
bots = bots.filter(bot => bot.connected);
if (bots.length !== count) {
const {timeTaken, bot} = await addBot(++i);
bots.push(bot);
console.log("Took", timeTaken);
}
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
async function startSwap() {
const pool = [];
for (let i = 0; i < count; i++) {
pool.push(i);
}
while (true) {
while (!pool.length) {
await new Promise(resolve => setTimeout(resolve, 1000 * niceness));
}
const id = pool.shift();
console.log("got", id);
const {timeTaken, bot} = await addBot(id);
console.log("Took", timeTaken);
await new Promise(resolve => setTimeout(resolve, 3 * timeTaken * niceness));
console.log("next")
setTimeout(() => {
console.log("Readding " + id + " to pool")
if (bot.connected) {
bot.quit();
}
pool.push(id);
}, 60 * 1000 * niceness);
}
}
function start() {
if (type === "sequential") {
return startSeq();
} else if (type === "swap") {
return startSwap();
}
console.error("Invalid type " + type);
process.exit(1);
}
start().catch(err => {
console.error(err);
process.exit(1);
});
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class PlayerSpreader extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
private int index = 1;
private final Set<UUID> uniquePlayers = new HashSet<>();
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
uniquePlayers.add(event.getPlayer().getUniqueId());
event.getPlayer().setGameMode(GameMode.CREATIVE);
event.getPlayer().setFlying(true);
event.getPlayer().setOp(true);
event.getPlayer().teleport(new Location(event.getPlayer().getWorld(), index * 500, 125, index * 500));
if (index++ > uniquePlayers.size()) {
index = 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment