diff --git a/src/main/java/xyz/nucleoid/extras/game_portal/ServerChangePortalBackend.java b/src/main/java/xyz/nucleoid/extras/game_portal/ServerChangePortalBackend.java index 4013a69..5cb0634 100644 --- a/src/main/java/xyz/nucleoid/extras/game_portal/ServerChangePortalBackend.java +++ b/src/main/java/xyz/nucleoid/extras/game_portal/ServerChangePortalBackend.java @@ -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; @@ -27,6 +29,7 @@ public final class ServerChangePortalBackend implements GamePortalBackend { public static final Map> ID_TO_PORTAL = new HashMap<>(); + private static boolean lastFailed = false; private final ItemStack icon; private final Text name; @@ -53,39 +56,34 @@ public ServerChangePortalBackend(Text name, List 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(); @@ -105,7 +103,7 @@ public int read() throws IOException { }); } } catch (Throwable e) { - e.printStackTrace(); + NucleoidExtras.LOGGER.warn("Failed to receive bungee packet!", e); } } @@ -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())); } } diff --git a/src/main/java/xyz/nucleoid/extras/network/BungeeCordPayload.java b/src/main/java/xyz/nucleoid/extras/network/BungeeCordPayload.java index 58fb8a5..49e094b 100644 --- a/src/main/java/xyz/nucleoid/extras/network/BungeeCordPayload.java +++ b/src/main/java/xyz/nucleoid/extras/network/BungeeCordPayload.java @@ -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 ID = new CustomPayload.Id<>(Identifier.of("bungeecord", "main")); public static final PacketCodec PACKET_CODEC = PacketCodec.of(BungeeCordPayload::write, BungeeCordPayload::read); private void write(ByteBuf buf) { - return; + buf.writeBytes(this.data); } @Override @@ -21,6 +22,6 @@ public CustomPayload.Id getId() { } private static BungeeCordPayload read(ByteBuf buf) { - return new BungeeCordPayload(PacketByteBufs.create()); + return new BungeeCordPayload(PacketByteBuf.readByteArray(buf)); } }