/*
This file is a part of the SteamWar software.
Copyright (C) 2021 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 .
*/
package de.steamwar.fightsystem.utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.bukkit.Bukkit;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.logging.Level;
public class TechHider9 implements BiFunction {
private final Set hiddenBlockIds = BlockIdWrapper.impl.getHiddenBlockIds();
private final int obfuscateWith = BlockIdWrapper.impl.getObfuscateWith();
@Override
public byte[] apply(byte[] data, Integer primaryBitMask) {
ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.directBuffer(data.length + 100);
int i = 0;
while(i < data.length){
byte bitsPerBlock = data[i++];
buffer.writeByte(bitsPerBlock);
if(bitsPerBlock != 13){
int paletteLength = TechHider.readVarInt(data, i);
int paletteLengthLength = TechHider.readVarIntLength(data, i);
buffer.writeBytes(data, i, paletteLengthLength);
i += paletteLengthLength;
for(int actPaletteId = 0; actPaletteId < paletteLength; actPaletteId++){
int entry = TechHider.readVarInt(data, i);
i += TechHider.readVarIntLength(data, i);
if(hiddenBlockIds.contains(entry)){
entry = obfuscateWith;
}
buffer.writeBytes(TechHider.writeVarInt(entry));
}
}else{
buffer.writeByte(data[++i]); //Empty palette
Bukkit.getLogger().log(Level.SEVERE, "Full chunk occured");
}
int dataArrayLength = TechHider.readVarInt(data, i);
int dataArrayLengthLength = TechHider.readVarIntLength(data, i);
buffer.writeBytes(data, i, dataArrayLength*8 + dataArrayLengthLength);
i += dataArrayLengthLength;
i += dataArrayLength * 8;
buffer.writeBytes(data, i, 4096);
i += 4096; //Skylight (Not in Nether/End!!!) 2048 + Blocklight 2048
}
data = new byte[buffer.readableBytes()];
buffer.readBytes(data);
return data;
}
}