use of net.minecraftforge.fml.common.network.simpleimpl.MessageContext in project BuildCraft by BuildCraft.
the class TileElectronicLibrary method readPayload.
@Override
public void readPayload(int id, PacketBufferBC buffer, Side side, MessageContext ctx) throws IOException {
super.readPayload(id, buffer, side, ctx);
if (side == Side.CLIENT) {
if (id == NET_RENDER_DATA) {
if (buffer.readBoolean()) {
selected = new Snapshot.Key(buffer);
} else {
selected = null;
}
}
if (id == NET_DOWN) {
if (buffer.readBoolean()) {
Snapshot snapshot = Snapshot.readFromNBT(NbtSquisher.expand(buffer));
snapshot.computeKey();
GlobalSavedDataSnapshots.get(world).addSnapshot(snapshot);
}
}
if (id == NET_UP) {
if (selected != null) {
Snapshot snapshot = GlobalSavedDataSnapshots.get(world).getSnapshot(selected);
if (snapshot != null) {
try (OutputStream outputStream = new OutputStream() {
private byte[] buf = new byte[4 * 1024];
private int pos = 0;
private boolean closed = false;
private void write(boolean last) throws IOException {
MessageManager.sendToServer(createMessage(NET_UP, localBuffer -> {
localBuffer.writeUniqueId(ctx.getClientHandler().getGameProfile().getId());
selected.writeToByteBuf(localBuffer);
localBuffer.writeBoolean(last);
localBuffer.writeByteArray(buf);
}));
}
@Override
public void write(int b) throws IOException {
buf[pos++] = (byte) b;
if (pos >= buf.length) {
write(false);
buf = new byte[buf.length];
pos = 0;
}
}
@Override
public void close() throws IOException {
if (closed) {
return;
}
closed = true;
buf = Arrays.copyOf(buf, pos);
pos = 0;
write(true);
}
}) {
NbtSquisher.squish(Snapshot.writeToNBT(snapshot), NbtSquishConstants.BUILDCRAFT_V1_COMPRESSED, outputStream);
}
}
}
}
}
if (side == Side.SERVER) {
if (id == NET_UP) {
UUID playerId = buffer.readUniqueId();
Snapshot.Key key = new Snapshot.Key(buffer);
Pair<UUID, Snapshot.Key> pair = Pair.of(playerId, key);
boolean last = buffer.readBoolean();
upSnapshotsParts.computeIfAbsent(pair, localPair -> new ArrayList<>()).add(buffer.readByteArray());
if (last && upSnapshotsParts.containsKey(pair)) {
try {
Snapshot snapshot = Snapshot.readFromNBT(NbtSquisher.expand(Bytes.concat(upSnapshotsParts.get(pair).toArray(new byte[0][]))));
Snapshot.Header header = snapshot.key.header;
snapshot = snapshot.copy();
snapshot.key = new Snapshot.Key(snapshot.key, (Snapshot.Header) null);
snapshot.computeKey();
GlobalSavedDataSnapshots.get(world).addSnapshot(snapshot);
invUpOut.setStackInSlot(0, BCBuildersItems.snapshot.getUsed(snapshot.getType(), header));
invUpIn.setStackInSlot(0, StackUtil.EMPTY);
} finally {
upSnapshotsParts.remove(pair);
}
}
}
}
}
use of net.minecraftforge.fml.common.network.simpleimpl.MessageContext in project Wizardry by TeamWizardry.
the class PacketDevilDustFizzle method handle.
@Override
public void handle(MessageContext messageContext) {
if (messageContext.side.isServer())
return;
World world = LibrarianLib.PROXY.getClientPlayer().world;
if (world == null)
return;
ClientRunnable.run(new ClientRunnable() {
@Override
@SideOnly(Side.CLIENT)
public void runIfClient() {
ParticleBuilder glitter = new ParticleBuilder(30);
glitter.setRender(new ResourceLocation(Wizardry.MODID, Constants.MISC.SPARKLE_BLURRED));
glitter.setMotionCalculationEnabled(true);
glitter.setCollision(true);
glitter.setCanBounce(true);
ParticleSpawner.spawn(glitter, world, new StaticInterp<>(pos), 15, RandUtil.nextInt(10), (i, builder) -> {
builder.setAcceleration(new Vec3d(0, RandUtil.nextDouble(-0.05, -0.01), 0));
builder.setColor(new Color(RandUtil.nextFloat(0.9f, 1), RandUtil.nextFloat(0, 0.25f), 0));
builder.setAlphaFunction(new InterpFadeInOut(0.0f, RandUtil.nextFloat(1f, 0.3f)));
builder.setScaleFunction(new InterpScale(RandUtil.nextFloat(0.3f, 1f), RandUtil.nextFloat(0, 0.2f)));
Vec3d offset = new Vec3d(RandUtil.nextDouble(-0.3, 0.3), RandUtil.nextDouble(-0.3, 0), RandUtil.nextDouble(-0.3, 0.3));
builder.setPositionOffset(offset);
builder.setLifetime(RandUtil.nextInt(20, 60));
builder.setMotion(new Vec3d(RandUtil.nextDouble(-0.1, 0.1), RandUtil.nextDouble(0.1, 0.5), RandUtil.nextDouble(-0.1, 0.1)));
});
}
});
}
Aggregations