Skip to content

Commit

Permalink
Re-enable bungee packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 9, 2024
1 parent 3fe8614 commit 41f09c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.random.Random;
import xyz.nucleoid.extras.NucleoidExtras;
import xyz.nucleoid.extras.network.BungeeCordPayload;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.impl.portal.GamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.GamePortalDisplay;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -27,6 +29,7 @@

public final class ServerChangePortalBackend implements GamePortalBackend {
public static final Map<String, List<ServerChangePortalBackend>> ID_TO_PORTAL = new HashMap<>();
private static boolean lastFailed = false;

private final ItemStack icon;
private final Text name;
Expand All @@ -53,39 +56,34 @@ public ServerChangePortalBackend(Text name, List<Text> description, ItemStack ic
}

public static void tick(MinecraftServer server) {
if (true) {
return;
}

var players = server.getPlayerManager().getPlayerList();
try {
var players = server.getPlayerManager().getPlayerList();

if (players.isEmpty() || server.getTicks() % 100 != 0) {
return;
}
if (players.isEmpty() || server.getTicks() % 200 != 0) {
return;
}

var random = players.get(Random.create().nextInt(players.size()));
var random = players.get(Random.create().nextInt(players.size()));

for (var key : ID_TO_PORTAL.keySet()) {
var buf = ByteStreams.newDataOutput();
buf.writeUTF("PlayerCount");
buf.writeUTF(key);
for (var key : ID_TO_PORTAL.keySet()) {
var buf = ByteStreams.newDataOutput();
buf.writeUTF("PlayerCount");
buf.writeUTF(key);

var out = PacketByteBufs.create();
out.writeBytes(buf.toByteArray());
ServerPlayNetworking.send(random, new BungeeCordPayload(out));
ServerPlayNetworking.send(random, new BungeeCordPayload(buf.toByteArray()));
}
lastFailed = false;
} catch (Throwable e) {
if (!lastFailed) {
NucleoidExtras.LOGGER.warn("Failed to sent bungee packet!", e);
}
lastFailed = true;
}
}

public static void handlePacket(BungeeCordPayload payload, ServerPlayNetworking.Context context) {
ByteBuf buf = payload.data();

try {
var out = new DataInputStream(new InputStream() {
@Override
public int read() throws IOException {
return buf.readByte();
}
});
var out = new DataInputStream(new ByteArrayInputStream(payload.data()));

var type = out.readUTF();

Expand All @@ -105,7 +103,7 @@ public int read() throws IOException {
});
}
} catch (Throwable e) {
e.printStackTrace();
NucleoidExtras.LOGGER.warn("Failed to receive bungee packet!", e);
}
}

Expand Down Expand Up @@ -160,10 +158,6 @@ public void applyTo(ServerPlayerEntity player, boolean alt) {
var buf = ByteStreams.newDataOutput();
buf.writeUTF("Connect");
buf.writeUTF(this.serverId);


var out = PacketByteBufs.create();
out.writeBytes(buf.toByteArray());
ServerPlayNetworking.send(player, new BungeeCordPayload(out));
ServerPlayNetworking.send(player, new BungeeCordPayload(buf.toByteArray()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import io.netty.buffer.ByteBuf;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;

public record BungeeCordPayload(ByteBuf data) implements CustomPayload {
public record BungeeCordPayload(byte[] data) implements CustomPayload {
public static final CustomPayload.Id<BungeeCordPayload> ID = new CustomPayload.Id<>(Identifier.of("bungeecord", "main"));

public static final PacketCodec<ByteBuf, BungeeCordPayload> PACKET_CODEC = PacketCodec.of(BungeeCordPayload::write, BungeeCordPayload::read);

private void write(ByteBuf buf) {
return;
buf.writeBytes(this.data);
}

@Override
Expand All @@ -21,6 +22,6 @@ public CustomPayload.Id<BungeeCordPayload> getId() {
}

private static BungeeCordPayload read(ByteBuf buf) {
return new BungeeCordPayload(PacketByteBufs.create());
return new BungeeCordPayload(PacketByteBuf.readByteArray(buf));
}
}

0 comments on commit 41f09c6

Please sign in to comment.