Skip to content

Improvement of Explosion Send #64

@TheMeinerLP

Description

@TheMeinerLP

Since the view engine takes into account various distances and more, the explosion should be linked to the view engine.
This means that the sound of the explosion is only played locally instead of over the whole world.

Current code:

/**
     * Performs the explosion and send the corresponding packet
     *
     * @param instance instance to perform this explosion in
     */
    public void apply(@NotNull Instance instance) {
        List<Point> blocks = prepare(instance);
        byte[] records = new byte[3 * blocks.size()];
        for (int i = 0; i < blocks.size(); i++) {
            final var pos = blocks.get(i);
            instance.setBlock(pos, Block.AIR);
            final byte x = (byte) (pos.x() - Math.floor(getCenterX()));
            final byte y = (byte) (pos.y() - Math.floor(getCenterY()));
            final byte z = (byte) (pos.z() - Math.floor(getCenterZ()));
            records[i * 3 + 0] = x;
            records[i * 3 + 1] = y;
            records[i * 3 + 2] = z;
        }

        // TODO send only to close players
        ExplosionPacket packet = new ExplosionPacket(centerX, centerY, centerZ, strength,
                records, 0, 0, 0);
        postExplosion(instance, blocks, packet);
        PacketUtils.sendGroupedPacket(instance.getPlayers(), packet);

        postSend(instance, blocks);
    }

Example Code:

/**
     * Performs the explosion and send the corresponding packet
     *
     * @param instance instance to perform this explosion in
     */
    public void apply(@NotNull Instance instance) {
        List<Point> blocks = prepare(instance);
        byte[] records = new byte[3 * blocks.size()];
        for (int i = 0; i < blocks.size(); i++) {
            final var pos = blocks.get(i);
            instance.setBlock(pos, Block.AIR);
            final byte x = (byte) (pos.x() - Math.floor(getCenterX()));
            final byte y = (byte) (pos.y() - Math.floor(getCenterY()));
            final byte z = (byte) (pos.z() - Math.floor(getCenterZ()));
            records[i * 3 + 0] = x;
            records[i * 3 + 1] = y;
            records[i * 3 + 2] = z;
        }
        ExplosionPacket packet = new ExplosionPacket(centerX, centerY, centerZ, strength,
                records, 0, 0, 0);
        postExplosion(instance, blocks, packet);
        List<Player> players = instance.getNearbyEntities(new Pos(centerX, centerY, centerZ), ServerFlag.ENTITY_VIEW_DISTANCE)
                .stream()
                .filter(Player.class::isInstance)
                .map(Player.class::cast).toList();

        PacketUtils.sendGroupedPacket(players, packet);
        postSend(instance, blocks);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions