/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024  SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.fightsystem.utils;

import de.steamwar.Reflection;
import de.steamwar.fightsystem.Config;

import java.util.List;

public class HullHiderWrapper8 implements HullHiderWrapper {

    private static final Reflection.Constructor newMultiBlockChange = Reflection.getConstructor("net.minecraft.PacketPlayOutMultiBlockChange", int.class, short[].class, Reflection.getClass("net.minecraft.Chunk"));
    private static final Reflection.Method getHandle = Reflection.getMethod("org.bukkit.craftbukkit.CraftChunk", "getHandle");
    @Override
    public Object generateBlockChangePacket(List<Hull.IntVector> changes) {
        Hull.IntVector chunk = changes.get(0);
        chunk = new Hull.IntVector(chunk.getX() >> 4, chunk.getY() >> 4, chunk.getZ() >> 4);
        int xOffset = 16*chunk.getX();
        int zOffset = 16*chunk.getZ();
        short[] pos = new short[changes.size()];

        for(int i = 0; i < changes.size(); i++) {
            Hull.IntVector change = changes.get(i);
            pos[i] = (short) (((change.getX()-xOffset) << 12) + ((change.getZ()-zOffset) << 8) + change.getY());
        }

        return newMultiBlockChange.invoke(pos.length, pos, getHandle.invoke(Config.world.getChunkAt(chunk.getX(), chunk.getZ())));
    }
}
