use of net.minecraft.network.PacketBuffer in project RFToolsDimensions by McJty.
the class PacketSyncDimensionInfo method toBytes.
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(dimensions.size());
for (Map.Entry<Integer, DimensionDescriptor> me : dimensions.entrySet()) {
buf.writeInt(me.getKey());
NBTTagCompound tagCompound = new NBTTagCompound();
me.getValue().writeToNBT(tagCompound);
PacketBuffer buffer = new PacketBuffer(buf);
buffer.writeNBTTagCompoundToBuffer(tagCompound);
}
buf.writeInt(dimensionInformation.size());
for (Map.Entry<Integer, DimensionInformation> me : dimensionInformation.entrySet()) {
buf.writeInt(me.getKey());
DimensionInformation dimInfo = me.getValue();
NetworkTools.writeString(buf, dimInfo.getName());
dimInfo.toBytes(buf);
}
}
use of net.minecraft.network.PacketBuffer in project GregTech by GregTechCE.
the class NetworkHandler method packet2proxy.
@SuppressWarnings("unchecked")
public static FMLProxyPacket packet2proxy(Packet packet) {
PacketCodec<Packet> codec = (PacketCodec<Packet>) codecMap.get(packet.getClass());
PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
buf.writeVarInt(packetMap.getId(packet.getClass()));
codec.encoder.encode(packet, buf);
return new FMLProxyPacket(buf, GTValues.MODID);
}
use of net.minecraft.network.PacketBuffer in project GregTech by GregTechCE.
the class NetworkHandler method init.
public static void init() {
channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(GTValues.MODID);
channel.register(new NetworkHandler());
PacketEncoder<PacketUIWidgetUpdate> widgetUpdateEncoder = (packet, buf) -> {
buf.writeVarInt(packet.updateData.readableBytes());
buf.writeBytes(packet.updateData);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.widgetId);
};
PacketDecoder<PacketUIWidgetUpdate> widgetUpdateDecoder = (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
return new PacketUIWidgetUpdate(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
};
registerPacket(1, PacketUIOpen.class, new PacketCodec<>((packet, buf) -> {
buf.writeVarInt(packet.serializedHolder.readableBytes());
buf.writeBytes(packet.serializedHolder);
buf.writeVarInt(packet.uiFactoryId);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.initialWidgetUpdates.size());
for (PacketUIWidgetUpdate widgetUpdate : packet.initialWidgetUpdates) {
widgetUpdateEncoder.encode(widgetUpdate, buf);
}
}, (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
int uiFactoryId = buf.readVarInt();
int windowId = buf.readVarInt();
ArrayList<PacketUIWidgetUpdate> initialWidgetUpdates = new ArrayList<>();
int initialWidgetUpdatesCount = buf.readVarInt();
for (int i = 0; i < initialWidgetUpdatesCount; i++) {
initialWidgetUpdates.add(widgetUpdateDecoder.decode(buf));
}
return new PacketUIOpen(uiFactoryId, new PacketBuffer(copiedDataBuffer), windowId, initialWidgetUpdates);
}));
registerPacket(2, PacketUIWidgetUpdate.class, new PacketCodec<>((packet, buf) -> {
buf.writeVarInt(packet.updateData.readableBytes());
buf.writeBytes(packet.updateData);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.widgetId);
}, (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
return new PacketUIWidgetUpdate(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
}));
registerPacket(3, PacketUIClientAction.class, new PacketCodec<>((packet, buf) -> {
buf.writeVarInt(packet.updateData.readableBytes());
buf.writeBytes(packet.updateData);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.widgetId);
}, (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
return new PacketUIClientAction(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
}));
registerPacket(4, PacketBlockParticle.class, new PacketCodec<>((packet, buf) -> {
buf.writeBlockPos(packet.blockPos);
buf.writeFloat((float) packet.entityPos.x);
buf.writeFloat((float) packet.entityPos.y);
buf.writeFloat((float) packet.entityPos.z);
buf.writeVarInt(packet.particlesAmount);
}, (buf) -> new PacketBlockParticle(buf.readBlockPos(), new Vector3(buf.readFloat(), buf.readFloat(), buf.readFloat()), buf.readVarInt())));
registerPacket(5, PacketClipboard.class, new PacketCodec<>((packet, buf) -> {
buf.writeString(packet.text);
}, (buf) -> new PacketClipboard(buf.readString(32767))));
registerServerExecutor(PacketUIClientAction.class, (packet, handler) -> {
Container openContainer = handler.player.openContainer;
if (openContainer instanceof ModularUIContainer && openContainer.windowId == packet.windowId) {
ModularUI modularUI = ((ModularUIContainer) openContainer).getModularUI();
PacketBuffer buffer = packet.updateData;
modularUI.guiWidgets.get(packet.widgetId).handleClientAction(buffer.readVarInt(), buffer);
}
});
if (FMLCommonHandler.instance().getSide().isClient()) {
initClient();
}
}
use of net.minecraft.network.PacketBuffer in project GregTech by GregTechCE.
the class SyncedTileEntityBase method onDataPacket.
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound updateTag = pkt.getNbtCompound();
NBTTagList tagList = updateTag.getTagList("d", NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++) {
NBTTagCompound entryTag = tagList.getCompoundTagAt(i);
int discriminator = entryTag.getInteger("i");
byte[] updateData = entryTag.getByteArray("d");
ByteBuf backedBuffer = Unpooled.copiedBuffer(updateData);
receiveCustomData(discriminator, new PacketBuffer(backedBuffer));
}
}
use of net.minecraft.network.PacketBuffer in project GregTech by GregTechCE.
the class SyncedTileEntityBase method writeCustomData.
public void writeCustomData(int discriminator, Consumer<PacketBuffer> dataWriter) {
ByteBuf backedBuffer = Unpooled.buffer();
dataWriter.accept(new PacketBuffer(backedBuffer));
byte[] updateData = Arrays.copyOfRange(backedBuffer.array(), 0, backedBuffer.writerIndex());
updateEntries.add(new UpdateEntry(discriminator, updateData));
@SuppressWarnings("deprecation") IBlockState blockState = getBlockType().getStateFromMeta(getBlockMetadata());
world.notifyBlockUpdate(getPos(), blockState, blockState, 0);
}
Aggregations