Skip to content

Commit

Permalink
Add ability to connect to random server
Browse files Browse the repository at this point in the history
  • Loading branch information
BoomEaro committed Dec 16, 2023
1 parent e8c75e7 commit 67bc76f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/main/java/ua/nanit/limbo/configuration/LimboConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public final class LimboConfig {

Expand Down Expand Up @@ -76,7 +77,7 @@ public final class LimboConfig {
private int maxPacketsPerSec;
private int maxBytesPerSec;

private String onMoveServer;
private List<String> onMoveServers;
private long cooldownTime;

public LimboConfig(Path root) {
Expand Down Expand Up @@ -141,7 +142,7 @@ public void load() throws Exception {
maxPacketsPerSec = conf.node("traffic", "packets").getInt(-1);
maxBytesPerSec = conf.node("traffic", "bytes").getInt(-1);

onMoveServer = conf.node("onMoveServer").getString();
onMoveServers = conf.node("onMoveServers").getList(String.class);
cooldownTime = conf.node("cooldownTime").getLong();
}

Expand Down Expand Up @@ -283,8 +284,8 @@ public int getMaxBytesPerSec() {
return maxBytesPerSec;
}

public String getOnMoveServer() {
return onMoveServer;
public List<String> getOnMoveServers() {
return onMoveServers;
}

public long getCooldownTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import java.net.SocketAddress;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -363,7 +365,11 @@ public void sendToServer(double x, double y, double z) {
if (!channel.isActive()) {
return;
}
sendPacket(PacketSnapshots.PACKET_SERVER_TELEPORT);
Random random = ThreadLocalRandom.current();

List<PacketSnapshot> packets = PacketSnapshots.PACKET_SERVER_TELEPORTS;
PacketSnapshot randomPacket = packets.get(random.nextInt(packets.size()));
sendPacket(randomPacket);
}, this.server.getConfig().getCooldownTime(), TimeUnit.MILLISECONDS);
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/ua/nanit/limbo/connection/PacketSnapshots.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public final class PacketSnapshots {
public static PacketSnapshot PACKET_REGISTRY_DATA;
public static PacketSnapshot PACKET_FINISH_CONFIGURATION;

public static PacketSnapshot PACKET_SERVER_TELEPORT;
public static List<PacketSnapshot> PACKET_SERVER_TELEPORTS;
public static List<PacketSnapshot> PACKETS_CHUNKS;

private PacketSnapshots() { }
Expand Down Expand Up @@ -201,14 +201,18 @@ public static void initPackets(LimboServer server) {
PACKET_REGISTRY_DATA = PacketSnapshot.of(packetRegistryData);
PACKET_FINISH_CONFIGURATION = PacketSnapshot.of(new PacketFinishConfiguration());

PacketPluginMessage pluginMessage = new PacketPluginMessage();
pluginMessage.setChannel("BungeeCord", "bungeecord:main");
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(server.getConfig().getOnMoveServer());
pluginMessage.setMessage(out.toByteArray());
List<PacketSnapshot> pluginMessages = new ArrayList<>();
for (String serverName : server.getConfig().getOnMoveServers()) {
PacketPluginMessage pluginMessage = new PacketPluginMessage();
pluginMessage.setChannel("BungeeCord", "bungeecord:main");
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(serverName);
pluginMessage.setMessage(out.toByteArray());

PACKET_SERVER_TELEPORT = PacketSnapshot.of(pluginMessage);
pluginMessages.add( PacketSnapshot.of(pluginMessage));
}
PACKET_SERVER_TELEPORTS = pluginMessages;

int chunkXOffset = (int) 0 >> 4; // Default x position is 0
int chunkZOffset = (int) 0 >> 4; // Default z position is 0
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,7 @@ traffic:
# How many bytes per second allowed for single connection
# Ignored if -1
bytes: 2048
onMoveServer: lobby
onMoveServers:
- "lobby1"
- "lobby2"
cooldownTime: 5000

0 comments on commit 67bc76f

Please sign in to comment.