Search in sources :

Example 6 with MessageContext

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);
                }
            }
        }
    }
}
Also used : EnumAccess(buildcraft.lib.tile.item.ItemHandlerManager.EnumAccess) Arrays(java.util.Arrays) ItemSnapshot(buildcraft.builders.item.ItemSnapshot) Snapshot(buildcraft.builders.snapshot.Snapshot) ItemHandlerSimple(buildcraft.lib.tile.item.ItemHandlerSimple) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) Pair(org.apache.commons.lang3.tuple.Pair) Side(net.minecraftforge.fml.relauncher.Side) NbtSquishConstants(buildcraft.api.data.NbtSquishConstants) Map(java.util.Map) ITickable(net.minecraft.util.ITickable) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) MessageManager(buildcraft.lib.net.MessageManager) DeltaInt(buildcraft.lib.delta.DeltaInt) Nonnull(javax.annotation.Nonnull) OutputStream(java.io.OutputStream) DeltaManager(buildcraft.lib.delta.DeltaManager) PacketBufferBC(buildcraft.lib.net.PacketBufferBC) IdAllocator(buildcraft.lib.misc.data.IdAllocator) BCBuildersItems(buildcraft.builders.BCBuildersItems) NbtSquisher(buildcraft.lib.nbt.NbtSquisher) TileBC_Neptune(buildcraft.lib.tile.TileBC_Neptune) IOException(java.io.IOException) UUID(java.util.UUID) Bytes(com.google.common.primitives.Bytes) List(java.util.List) MessageContext(net.minecraftforge.fml.common.network.simpleimpl.MessageContext) GlobalSavedDataSnapshots(buildcraft.builders.snapshot.GlobalSavedDataSnapshots) StackUtil(buildcraft.lib.misc.StackUtil) EnumPipePart(buildcraft.api.core.EnumPipePart) StackInsertionFunction(buildcraft.lib.tile.item.StackInsertionFunction) ItemSnapshot(buildcraft.builders.item.ItemSnapshot) Snapshot(buildcraft.builders.snapshot.Snapshot) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 7 with MessageContext

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)));
            });
        }
    });
}
Also used : InterpScale(com.teamwizardry.wizardry.api.util.interp.InterpScale) InterpFadeInOut(com.teamwizardry.librarianlib.features.particle.functions.InterpFadeInOut) StaticInterp(com.teamwizardry.librarianlib.features.math.interpolate.StaticInterp) ClientRunnable(com.teamwizardry.librarianlib.features.utilities.client.ClientRunnable) World(net.minecraft.world.World) Wizardry(com.teamwizardry.wizardry.Wizardry) ParticleSpawner(com.teamwizardry.librarianlib.features.particle.ParticleSpawner) LibrarianLib(com.teamwizardry.librarianlib.core.LibrarianLib) PacketBase(com.teamwizardry.librarianlib.features.network.PacketBase) ParticleBuilder(com.teamwizardry.librarianlib.features.particle.ParticleBuilder) java.awt(java.awt) Vec3d(net.minecraft.util.math.Vec3d) MessageContext(net.minecraftforge.fml.common.network.simpleimpl.MessageContext) Side(net.minecraftforge.fml.relauncher.Side) ResourceLocation(net.minecraft.util.ResourceLocation) RandUtil(com.teamwizardry.wizardry.api.util.RandUtil) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) Save(com.teamwizardry.librarianlib.features.saving.Save) Constants(com.teamwizardry.wizardry.api.Constants) InterpScale(com.teamwizardry.wizardry.api.util.interp.InterpScale) ResourceLocation(net.minecraft.util.ResourceLocation) StaticInterp(com.teamwizardry.librarianlib.features.math.interpolate.StaticInterp) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) World(net.minecraft.world.World) ClientRunnable(com.teamwizardry.librarianlib.features.utilities.client.ClientRunnable) InterpFadeInOut(com.teamwizardry.librarianlib.features.particle.functions.InterpFadeInOut) ParticleBuilder(com.teamwizardry.librarianlib.features.particle.ParticleBuilder) Vec3d(net.minecraft.util.math.Vec3d)

Aggregations

MessageContext (net.minecraftforge.fml.common.network.simpleimpl.MessageContext)7 Side (net.minecraftforge.fml.relauncher.Side)6 ItemStack (net.minecraft.item.ItemStack)5 EnumPipePart (buildcraft.api.core.EnumPipePart)2 IdAllocator (buildcraft.lib.misc.data.IdAllocator)2 MessageManager (buildcraft.lib.net.MessageManager)2 PacketBufferBC (buildcraft.lib.net.PacketBufferBC)2 IOException (java.io.IOException)2 List (java.util.List)2 BlockPos (net.minecraft.util.math.BlockPos)2 IMessage (net.minecraftforge.fml.common.network.simpleimpl.IMessage)2 IAreaProvider (buildcraft.api.core.IAreaProvider)1 IBox (buildcraft.api.core.IBox)1 NbtSquishConstants (buildcraft.api.data.NbtSquishConstants)1 IFillerPattern (buildcraft.api.filler.IFillerPattern)1 IItemTransactor (buildcraft.api.inventory.IItemTransactor)1 MjAPI (buildcraft.api.mj.MjAPI)1 MjBattery (buildcraft.api.mj.MjBattery)1 MjCapabilityHelper (buildcraft.api.mj.MjCapabilityHelper)1 IStatementParameter (buildcraft.api.statements.IStatementParameter)1