import com.irtimaled.bbor.common.events.*;
import com.irtimaled.bbor.common.messages.AddBoundingBox;
import com.irtimaled.bbor.common.messages.InitializeClient;
+import com.irtimaled.bbor.common.messages.PayloadBuilder;
import com.irtimaled.bbor.common.messages.RemoveBoundingBox;
import com.irtimaled.bbor.common.models.BoundingBox;
import com.irtimaled.bbor.common.models.BoundingBoxMobSpawner;
import com.irtimaled.bbor.common.models.WorldData;
import io.netty.channel.local.LocalAddress;
import net.minecraft.entity.player.EntityPlayerMP;
-import net.minecraft.network.play.server.SPacketCustomPayload;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
private void playerLoggedIn(EntityPlayerMP player) {
if (player.connection.netManager.getRemoteAddress() instanceof LocalAddress) return;
- player.connection.sendPacket(InitializeClient.getPayload(worldData));
+ player.connection.sendPacket(InitializeClient.getPayload(worldData).build());
}
private void playerLoggedOut(EntityPlayerMP player) {
}
private void sendRemoveBoundingBox(DimensionType dimensionType, BoundingBox boundingBox) {
- SPacketCustomPayload payload = RemoveBoundingBox.getPayload(dimensionType, boundingBox);
- if (payload == null) return;
+ PayloadBuilder payloadBuilder = RemoveBoundingBox.getPayload(dimensionType, boundingBox);
+ if (payloadBuilder == null) return;
for (EntityPlayerMP player : players) {
if (DimensionType.getById(player.dimension) == dimensionType) {
- player.connection.sendPacket(payload);
+ player.connection.sendPacket(payloadBuilder.build());
if (playerBoundingBoxesCache.containsKey(player)) {
playerBoundingBoxesCache.get(player).remove(boundingBox);
for (BoundingBox key : cacheSubset.keySet()) {
Set<BoundingBox> boundingBoxes = cacheSubset.get(key);
- SPacketCustomPayload payload = AddBoundingBox.getPayload(dimensionType, key, boundingBoxes);
- if (payload != null)
- player.connection.sendPacket(payload);
+ PayloadBuilder payloadBuilder = AddBoundingBox.getPayload(dimensionType, key, boundingBoxes);
+ if (payloadBuilder != null)
+ player.connection.sendPacket(payloadBuilder.build());
if (!playerBoundingBoxesCache.containsKey(player)) {
playerBoundingBoxesCache.put(player, new HashSet<>());
import com.irtimaled.bbor.client.events.AddBoundingBoxReceived;
import com.irtimaled.bbor.common.models.BoundingBox;
-import io.netty.buffer.Unpooled;
-import net.minecraft.network.PacketBuffer;
-import net.minecraft.network.play.server.SPacketCustomPayload;
-import net.minecraft.util.ResourceLocation;
import net.minecraft.world.dimension.DimensionType;
import java.util.HashSet;
import java.util.Set;
public class AddBoundingBox {
- public static final ResourceLocation NAME = new ResourceLocation("bbor:add_bounding_box");
+ public static final String NAME = "bbor:add_bounding_box";
- public static SPacketCustomPayload getPayload(DimensionType dimensionType, BoundingBox key, Set<BoundingBox> boundingBoxes) {
- if(!BoundingBoxSerializer.canSerialize(key)) return null;
+ public static PayloadBuilder getPayload(DimensionType dimensionType, BoundingBox key, Set<BoundingBox> boundingBoxes) {
+ if (!BoundingBoxSerializer.canSerialize(key)) return null;
- PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
- buf.writeVarInt(dimensionType.getId());
- BoundingBoxSerializer.serialize(key, buf);
+ PayloadBuilder builder = PayloadBuilder.clientBound(NAME)
+ .writeVarInt(dimensionType.getId());
+ BoundingBoxSerializer.serialize(key, builder);
if (boundingBoxes != null && boundingBoxes.size() > 1) {
for (BoundingBox boundingBox : boundingBoxes) {
- BoundingBoxSerializer.serialize(boundingBox, buf);
+ BoundingBoxSerializer.serialize(boundingBox, builder);
}
}
- return new SPacketCustomPayload(NAME, buf);
+ return builder;
}
- public static AddBoundingBoxReceived getEvent(PacketBuffer buf) {
- DimensionType dimensionType = DimensionType.getById(buf.readVarInt());
- BoundingBox key = BoundingBoxDeserializer.deserialize(buf);
+ public static AddBoundingBoxReceived getEvent(PayloadReader reader) {
+ int dimensionId = reader.readVarInt();
+ BoundingBox key = BoundingBoxDeserializer.deserialize(reader);
if (key == null) return null;
Set<BoundingBox> boundingBoxes = new HashSet<>();
- while (buf.isReadable()) {
- BoundingBox boundingBox = BoundingBoxDeserializer.deserialize(buf);
+ while (reader.isReadable()) {
+ BoundingBox boundingBox = BoundingBoxDeserializer.deserialize(reader);
boundingBoxes.add(boundingBox);
}
if (boundingBoxes.size() == 0)
boundingBoxes.add(key);
- return new AddBoundingBoxReceived(dimensionType, key, boundingBoxes);
+ return new AddBoundingBoxReceived(DimensionType.getById(dimensionId), key, boundingBoxes);
}
}
import com.irtimaled.bbor.common.BoundingBoxType;
import com.irtimaled.bbor.common.models.*;
-import net.minecraft.network.PacketBuffer;
import java.awt.*;
import java.util.HashSet;
import java.util.Set;
class BoundingBoxDeserializer {
- static BoundingBox deserialize(PacketBuffer buf) {
- if (!buf.isReadable(2)) return null;
+ static BoundingBox deserialize(PayloadReader reader) {
+ if (!reader.isReadable(2)) return null;
- char type = buf.readChar();
+ char type = reader.readChar();
switch (type) {
case 'V':
- return deserializeVillage(buf);
+ return deserializeVillage(reader);
case 'S':
- return deserializeStructure(buf);
+ return deserializeStructure(reader);
case 'M':
- return deserializeMobSpawner(buf);
+ return deserializeMobSpawner(reader);
}
return null;
}
- private static BoundingBox deserializeStructure(PacketBuffer buf) {
- BoundingBoxType type = BoundingBoxType.getByNameHash(buf.readInt());
+ private static BoundingBox deserializeStructure(PayloadReader reader) {
+ BoundingBoxType type = BoundingBoxType.getByNameHash(reader.readInt());
if (type == null) return null;
- Coords minCoords = deserializeCoords(buf);
- Coords maxCoords = deserializeCoords(buf);
+ Coords minCoords = reader.readCoords();
+ Coords maxCoords = reader.readCoords();
return BoundingBoxStructure.from(minCoords, maxCoords, type);
}
- private static BoundingBox deserializeVillage(PacketBuffer buf) {
- Coords center = deserializeCoords(buf);
- int radius = buf.readVarInt();
- boolean spawnsIronGolems = buf.readBoolean();
- Color color = new Color(buf.readVarInt());
+ private static BoundingBox deserializeVillage(PayloadReader reader) {
+ Coords center = reader.readCoords();
+ int radius = reader.readVarInt();
+ boolean spawnsIronGolems = reader.readBoolean();
+ Color color = new Color(reader.readVarInt());
Set<Coords> doors = new HashSet<>();
- while (buf.isReadable()) {
- Coords door = deserializeCoords(buf);
- doors.add(door);
+ while (reader.isReadable()) {
+ doors.add(reader.readCoords());
}
return BoundingBoxVillage.from(center, radius, color, spawnsIronGolems, doors);
}
- private static BoundingBox deserializeMobSpawner(PacketBuffer buf) {
- Coords center = deserializeCoords(buf);
- return BoundingBoxMobSpawner.from(center);
- }
-
- private static Coords deserializeCoords(PacketBuffer buf) {
- int x = buf.readVarInt();
- int y = buf.readVarInt();
- int z = buf.readVarInt();
- return new Coords(x, y, z);
+ private static BoundingBox deserializeMobSpawner(PayloadReader reader) {
+ return BoundingBoxMobSpawner.from(reader.readCoords());
}
}
package com.irtimaled.bbor.common.messages;
import com.irtimaled.bbor.common.models.*;
-import net.minecraft.network.PacketBuffer;
-import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
class BoundingBoxSerializer {
- private static final Map<Class, BiConsumer<BoundingBox, PacketBuffer>> serializers = new HashMap<>();
+ private static final Map<Class, BiConsumer<BoundingBox, PayloadBuilder>> serializers = new HashMap<>();
static {
- serializers.put(BoundingBoxVillage.class, (bb, buf) -> serializeVillage((BoundingBoxVillage) bb, buf));
- serializers.put(BoundingBoxStructure.class, (bb, buf) -> serializeStructure((BoundingBoxStructure) bb, buf));
- serializers.put(BoundingBoxMobSpawner.class, (bb, buf) -> serializeMobSpawner((BoundingBoxMobSpawner) bb, buf));
+ serializers.put(BoundingBoxVillage.class, (bb, pb) -> serializeVillage((BoundingBoxVillage) bb, pb));
+ serializers.put(BoundingBoxStructure.class, (bb, pb) -> serializeStructure((BoundingBoxStructure) bb, pb));
+ serializers.put(BoundingBoxMobSpawner.class, (bb, pb) -> serializeMobSpawner((BoundingBoxMobSpawner) bb, pb));
}
static boolean canSerialize(BoundingBox key) {
return serializers.containsKey(key.getClass());
}
- static void serialize(BoundingBox boundingBox, PacketBuffer buf) {
- BiConsumer<BoundingBox, PacketBuffer> serializer = serializers.get(boundingBox.getClass());
+ static void serialize(BoundingBox boundingBox, PayloadBuilder builder) {
+ BiConsumer<BoundingBox, PayloadBuilder> serializer = serializers.get(boundingBox.getClass());
if (serializer == null) return;
- serializer.accept(boundingBox, buf);
+ serializer.accept(boundingBox, builder);
}
- private static void serializeVillage(BoundingBoxVillage boundingBox, PacketBuffer buf) {
- buf.writeChar('V');
- serializeCoords(boundingBox.getCenter(), buf);
- buf.writeVarInt(boundingBox.getRadius());
- buf.writeBoolean(boundingBox.getSpawnsIronGolems());
- serializeColor(boundingBox.getColor(), buf);
+ private static void serializeVillage(BoundingBoxVillage boundingBox, PayloadBuilder builder) {
+ builder.writeChar('V')
+ .writeCoords(boundingBox.getCenter())
+ .writeVarInt(boundingBox.getRadius())
+ .writeBoolean(boundingBox.getSpawnsIronGolems())
+ .writeVarInt(boundingBox.getColor().getRGB());
for (Coords door : boundingBox.getDoors()) {
- serializeCoords(door, buf);
+ builder.writeCoords(door);
}
}
- private static void serializeStructure(BoundingBoxStructure boundingBox, PacketBuffer buf) {
- buf.writeChar('S');
- buf.writeInt(boundingBox.getTypeName().hashCode());
- serializeCuboid(boundingBox, buf);
+ private static void serializeStructure(BoundingBoxStructure boundingBox, PayloadBuilder builder) {
+ builder.writeChar('S')
+ .writeInt(boundingBox.getTypeName().hashCode())
+ .writeCoords(boundingBox.getMinCoords())
+ .writeCoords(boundingBox.getMaxCoords());
}
- private static void serializeMobSpawner(BoundingBoxMobSpawner boundingBox, PacketBuffer buf) {
- buf.writeChar('M');
- serializeCoords(boundingBox.getCoords(), buf);
- }
-
- private static void serializeColor(Color color, PacketBuffer buf) {
- buf.writeVarInt(color.getRGB());
- }
-
- private static void serializeCuboid(BoundingBox boundingBox, PacketBuffer buf) {
- serializeCoords(boundingBox.getMinCoords(), buf);
- serializeCoords(boundingBox.getMaxCoords(), buf);
- }
-
- private static void serializeCoords(Coords coords, PacketBuffer buf) {
- buf.writeVarInt(coords.getX());
- buf.writeVarInt(coords.getY());
- buf.writeVarInt(coords.getZ());
+ private static void serializeMobSpawner(BoundingBoxMobSpawner boundingBox, PayloadBuilder builder) {
+ builder.writeChar('M')
+ .writeCoords(boundingBox.getCoords());
}
}
import com.irtimaled.bbor.client.events.InitializeClientReceived;
import com.irtimaled.bbor.common.models.WorldData;
-import io.netty.buffer.Unpooled;
-import net.minecraft.network.PacketBuffer;
-import net.minecraft.network.play.server.SPacketCustomPayload;
-import net.minecraft.util.ResourceLocation;
public class InitializeClient {
- public static final ResourceLocation NAME = new ResourceLocation("bbor:initialize");
+ public static final String NAME = "bbor:initialize";
- public static SPacketCustomPayload getPayload(WorldData worldData) {
- PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
- buf.writeLong(worldData.getSeed());
- buf.writeInt(worldData.getSpawnX());
- buf.writeInt(worldData.getSpawnZ());
-
- return new SPacketCustomPayload(NAME, buf);
+ public static PayloadBuilder getPayload(WorldData worldData) {
+ return PayloadBuilder.clientBound(NAME)
+ .writeLong(worldData.getSeed())
+ .writeInt(worldData.getSpawnX())
+ .writeInt(worldData.getSpawnZ());
}
- public static InitializeClientReceived getEvent(PacketBuffer buf) {
- long seed = buf.readLong();
- int spawnX = buf.readInt();
- int spawnZ = buf.readInt();
+ public static InitializeClientReceived getEvent(PayloadReader reader) {
+ long seed = reader.readLong();
+ int spawnX = reader.readInt();
+ int spawnZ = reader.readInt();
return new InitializeClientReceived(seed, spawnX, spawnZ);
}
}
--- /dev/null
+package com.irtimaled.bbor.common.messages;
+
+import com.irtimaled.bbor.common.models.Coords;
+import io.netty.buffer.Unpooled;
+import net.minecraft.network.Packet;
+import net.minecraft.network.PacketBuffer;
+import net.minecraft.network.play.client.CPacketCustomPayload;
+import net.minecraft.network.play.server.SPacketCustomPayload;
+import net.minecraft.util.ResourceLocation;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BiFunction;
+
+public class PayloadBuilder {
+ private static Map<String, ResourceLocation> packetNames = new HashMap<>();
+
+ static PayloadBuilder clientBound(String name) {
+ return new PayloadBuilder(packetNames.computeIfAbsent(name, ResourceLocation::new), SPacketCustomPayload::new);
+ }
+
+ static PayloadBuilder serverBound(String name) {
+ return new PayloadBuilder(packetNames.computeIfAbsent(name, ResourceLocation::new), CPacketCustomPayload::new);
+ }
+
+ private final ResourceLocation name;
+ private final BiFunction<ResourceLocation, PacketBuffer, Packet<?>> packetBuilder;
+ private final PacketBuffer buffer;
+
+ private PayloadBuilder(ResourceLocation name, BiFunction<ResourceLocation, PacketBuffer, Packet<?>> packetBuilder) {
+ this.name = name;
+ this.buffer = new PacketBuffer(Unpooled.buffer());
+ this.packetBuilder = packetBuilder;
+ }
+
+ private Packet<?> packet;
+
+ public Packet<?> build() {
+ if (packet == null)
+ packet = packetBuilder.apply(name, buffer);
+ return packet;
+ }
+
+ PayloadBuilder writeLong(long value) {
+ buffer.writeLong(value);
+ packet = null;
+ return this;
+ }
+
+ PayloadBuilder writeInt(int value) {
+ buffer.writeInt(value);
+ packet = null;
+ return this;
+ }
+
+ PayloadBuilder writeVarInt(int value) {
+ buffer.writeVarInt(value);
+ packet = null;
+ return this;
+ }
+
+ PayloadBuilder writeChar(char value) {
+ buffer.writeChar(value);
+ packet = null;
+ return this;
+ }
+
+ PayloadBuilder writeBoolean(boolean value) {
+ buffer.writeBoolean(value);
+ packet = null;
+ return this;
+ }
+
+ PayloadBuilder writeCoords(Coords coords) {
+ return writeVarInt(coords.getX())
+ .writeVarInt(coords.getY())
+ .writeVarInt(coords.getZ());
+ }
+}
--- /dev/null
+package com.irtimaled.bbor.common.messages;
+
+import com.irtimaled.bbor.common.models.Coords;
+import net.minecraft.network.PacketBuffer;
+
+public class PayloadReader {
+ private PacketBuffer buffer;
+
+ public PayloadReader(PacketBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+ long readLong() {
+ return buffer.readLong();
+ }
+
+ int readInt() {
+ return buffer.readInt();
+ }
+
+ int readVarInt() {
+ return buffer.readVarInt();
+ }
+
+ boolean isReadable() {
+ return buffer.isReadable();
+ }
+
+ boolean isReadable(int count) {
+ return buffer.isReadable(count);
+ }
+
+ char readChar() {
+ return buffer.readChar();
+ }
+
+ boolean readBoolean() {
+ return buffer.readBoolean();
+ }
+
+ Coords readCoords() {
+ int x = readVarInt();
+ int y = readVarInt();
+ int z = readVarInt();
+ return new Coords(x, y, z);
+ }
+}
import com.irtimaled.bbor.client.events.RemoveBoundingBoxReceived;
import com.irtimaled.bbor.common.models.BoundingBox;
-import io.netty.buffer.Unpooled;
-import net.minecraft.network.PacketBuffer;
-import net.minecraft.network.play.server.SPacketCustomPayload;
-import net.minecraft.util.ResourceLocation;
import net.minecraft.world.dimension.DimensionType;
public class RemoveBoundingBox {
- public static final ResourceLocation NAME = new ResourceLocation("bbor:remove_bounding_box");
+ public static final String NAME = "bbor:remove_bounding_box";
- public static SPacketCustomPayload getPayload(DimensionType dimensionType, BoundingBox key) {
- if(!BoundingBoxSerializer.canSerialize(key)) return null;
+ public static PayloadBuilder getPayload(DimensionType dimensionType, BoundingBox key) {
+ if (!BoundingBoxSerializer.canSerialize(key)) return null;
- PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
- buf.writeVarInt(dimensionType.getId());
- BoundingBoxSerializer.serialize(key, buf);
-
- return new SPacketCustomPayload(NAME, buf);
+ PayloadBuilder builder = PayloadBuilder.clientBound(NAME)
+ .writeVarInt(dimensionType.getId());
+ BoundingBoxSerializer.serialize(key, builder);
+ return builder;
}
- public static RemoveBoundingBoxReceived getEvent(PacketBuffer buf) {
- DimensionType dimensionType = DimensionType.getById(buf.readVarInt());
- BoundingBox key = BoundingBoxDeserializer.deserialize(buf);
- if(key == null) return null;
+ public static RemoveBoundingBoxReceived getEvent(PayloadReader reader) {
+ int dimensionId = reader.readVarInt();
+ BoundingBox key = BoundingBoxDeserializer.deserialize(reader);
+ if (key == null) return null;
- return new RemoveBoundingBoxReceived(dimensionType, key);
+ return new RemoveBoundingBoxReceived(DimensionType.getById(dimensionId), key);
}
}
package com.irtimaled.bbor.common.messages;
-import io.netty.buffer.Unpooled;
-import net.minecraft.network.PacketBuffer;
-import net.minecraft.network.play.client.CPacketCustomPayload;
-import net.minecraft.util.ResourceLocation;
-
public class SubscribeToServer {
- public static final ResourceLocation NAME = new ResourceLocation("bbor:subscribe");
+ public static final String NAME = "bbor:subscribe";
- public static CPacketCustomPayload getPayload() {
- return new CPacketCustomPayload(NAME, new PacketBuffer(Unpooled.buffer()));
+ public static PayloadBuilder getPayload() {
+ return PayloadBuilder.serverBound(NAME);
}
}
@Redirect(method = "processPacket", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/play/INetHandlerPlayServer;processCustomPayload(Lnet/minecraft/network/play/client/CPacketCustomPayload;)V"))
private void processPacket(INetHandlerPlayServer netHandlerPlayServer, CPacketCustomPayload packet) {
- if (this.channel.equals(SubscribeToServer.NAME)) {
+ if (this.channel.toString().equals(SubscribeToServer.NAME)) {
EntityPlayerMP player = ((NetHandlerPlayServer) netHandlerPlayServer).player;
EventBus.publish(new PlayerSubscribed(player));
} else {
package com.irtimaled.bbor.mixin.network.play.server;
import com.irtimaled.bbor.common.EventBus;
-import com.irtimaled.bbor.common.messages.AddBoundingBox;
-import com.irtimaled.bbor.common.messages.InitializeClient;
-import com.irtimaled.bbor.common.messages.RemoveBoundingBox;
-import com.irtimaled.bbor.common.messages.SubscribeToServer;
+import com.irtimaled.bbor.common.messages.*;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.INetHandlerPlayClient;
PacketBuffer data = null;
try {
data = packet.getBufferData();
- if (InitializeClient.NAME.equals(channel)) {
- EventBus.publish(InitializeClient.getEvent(data));
- ((NetHandlerPlayClient) netHandlerPlayClient).sendPacket(SubscribeToServer.getPayload());
- } else if (AddBoundingBox.NAME.equals(channel)) {
- EventBus.publish(AddBoundingBox.getEvent(data));
- } else if (RemoveBoundingBox.NAME.equals(channel)) {
- EventBus.publish(RemoveBoundingBox.getEvent(data));
- } else {
- netHandlerPlayClient.handleCustomPayload(packet);
+ PayloadReader reader = new PayloadReader(data);
+ switch (channel.toString()) {
+ case InitializeClient.NAME: {
+ EventBus.publish(InitializeClient.getEvent(reader));
+ ((NetHandlerPlayClient) netHandlerPlayClient).sendPacket(SubscribeToServer.getPayload().build());
+ break;
+ }
+ case AddBoundingBox.NAME: {
+ EventBus.publish(AddBoundingBox.getEvent(reader));
+ break;
+ }
+ case RemoveBoundingBox.NAME: {
+ EventBus.publish(RemoveBoundingBox.getEvent(reader));
+ break;
+ }
+ default: {
+ netHandlerPlayClient.handleCustomPayload(packet);
+ break;
+ }
}
} finally {
if (data != null)