SteamWar/FightSystem
Archiviert
13
1
Dieses Repository wurde am 2024-08-05 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
FightSystem/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java

325 Zeilen
13 KiB
Java

2019-09-05 18:26:13 +02:00
package de.steamwar.fightsystem.utils;
2019-04-25 20:33:36 +02:00
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
2019-05-28 06:16:16 +02:00
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.ChunkCoordIntPair;
2019-04-25 20:33:36 +02:00
import com.comphenix.protocol.wrappers.MultiBlockChangeInfo;
import com.comphenix.protocol.wrappers.WrappedBlockData;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
2019-11-10 22:34:22 +01:00
import de.steamwar.core.Core;
2019-11-16 08:37:33 +01:00
import de.steamwar.fightsystem.Config;
2019-09-05 18:26:13 +02:00
import de.steamwar.fightsystem.FightSystem;
2019-09-25 18:21:20 +02:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import javafx.util.Pair;
import org.bukkit.Bukkit;
2019-05-28 06:16:16 +02:00
import org.bukkit.GameMode;
2019-04-25 20:33:36 +02:00
import org.bukkit.Material;
import org.bukkit.entity.Player;
import java.util.*;
2019-04-25 20:33:36 +02:00
2019-11-16 08:37:33 +01:00
import static de.steamwar.fightsystem.utils.ITechHider.bypass;
2019-04-25 20:33:36 +02:00
public class TechHider {
2019-06-22 17:11:10 +02:00
private TechHider(){}
private static Map<PacketContainer, PacketContainer> packetCache = new HashMap<>();
2019-05-28 06:16:16 +02:00
private static int arenaMinX;
private static int arenaMaxX;
private static int arenaMinZ;
private static int arenaMaxZ;
private static short obfuscateShift4;
2019-05-28 06:16:16 +02:00
private static Material obfuscateMaterial;
private static boolean running = false;
private static int threadMultiplier = 1;
2019-05-28 06:16:16 +02:00
2019-04-25 20:33:36 +02:00
public static void init(){
2019-09-12 20:46:21 +02:00
if(disabled())
return;
2019-11-16 08:37:33 +01:00
arenaMinX = ITechHider.posToChunk(Config.ArenaMinX);
arenaMinZ = ITechHider.posToChunk(Config.ArenaMinZ);
arenaMaxX = ITechHider.posToChunk(Config.ArenaMaxX) + 1;
arenaMaxZ = ITechHider.posToChunk(Config.ArenaMaxZ) + 1;
obfuscateShift4 = (short)(Config.ObfuscateWith << 4);
2019-11-10 17:29:59 +01:00
//noinspection deprecation
2019-05-28 06:16:16 +02:00
obfuscateMaterial = Material.getMaterial(Config.ObfuscateWith);
2019-09-05 18:26:13 +02:00
if(Config.event())
threadMultiplier = 4;
2019-11-10 22:34:22 +01:00
if(Core.getVersion() > 8){
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Client.USE_ITEM) {
@Override
public void onPacketReceiving(PacketEvent e) {
Player p = e.getPlayer();
2019-11-10 22:34:22 +01:00
if(p.getGameMode() == GameMode.SPECTATOR){
e.setCancelled(true);
}
}
2019-11-10 22:34:22 +01:00
});
}
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Client.USE_ENTITY) {
@Override
public void onPacketReceiving(PacketEvent e) {
Player p = e.getPlayer();
if(p.getGameMode() == GameMode.SPECTATOR){
e.setCancelled(true);
}
}
});
}
public static void start(){
if(running)
return;
running = true;
2019-11-15 08:22:16 +01:00
if(disabled())
return;
Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), packetCache::clear, 1, 1);
2019-11-15 08:22:16 +01:00
switch(Core.getVersion()){
case 8:
break;
default:
chunkHider();
multiBlockHider();
blockHider();
blockActionHider();
}
2019-11-10 17:29:59 +01:00
}
private static void chunkHider(){
ProtocolLibrary.getProtocolManager().getAsynchronousManager().registerAsyncHandler(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Server.MAP_CHUNK) {
2019-04-25 20:33:36 +02:00
@Override
public void onPacketSending(PacketEvent e) {
2019-07-01 21:02:32 +02:00
PacketContainer packet = e.getPacket();
2019-04-25 20:33:36 +02:00
StructureModifier<Integer> ints = packet.getIntegers();
int chunkX = ints.read(0);
int chunkZ = ints.read(1);
2019-05-28 06:16:16 +02:00
Player p = e.getPlayer();
if(bypass(p, chunkX, chunkZ))
return;
PacketContainer cached = packetCache.get(packet);
if(cached != null){
e.setPacket(cached);
return;
}
cached = packet.deepClone();
packetCache.put(packet, cached);
e.setPacket(cached);
StructureModifier<List> list = cached.getSpecificModifier(List.class);
2019-07-01 21:02:32 +02:00
List nmsTags = list.read(0);
2019-04-25 20:33:36 +02:00
boolean changed = false;
for(int i = nmsTags.size() - 1; i >= 0; i--){
NbtCompound nbt = NbtFactory.fromNMSCompound(nmsTags.get(i));
2019-05-28 06:16:16 +02:00
if(Config.HiddenBlockEntities.contains(nbt.getString("id"))){
2019-04-25 20:33:36 +02:00
nmsTags.remove(i);
changed = true;
}
}
if(changed){
list.write(0, nmsTags);
}
changed = false;
StructureModifier<byte[]> byteArray = cached.getByteArrays();
2019-04-25 20:33:36 +02:00
byte [] data = byteArray.read(0);
2019-09-29 19:35:32 +02:00
ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.directBuffer(data.length + 100);
2019-04-25 20:33:36 +02:00
int i = 0;
2019-09-25 18:21:20 +02:00
while(i < data.length){
byte bitsPerBlock = data[i++];
buffer.writeByte(bitsPerBlock);
2019-04-25 20:33:36 +02:00
if(bitsPerBlock < 4)
bitsPerBlock = 4;
else if(bitsPerBlock > 8){
bitsPerBlock = 13;
2019-09-29 19:35:32 +02:00
buffer.writeByte(data[++i]);
2019-04-25 20:33:36 +02:00
}
if(bitsPerBlock != 13){
2019-11-16 08:37:33 +01:00
int paletteLength = ITechHider.readVarInt(data, i);
int paletteLengthLength = ITechHider.readVarIntLength(data, i);
2019-09-25 18:21:20 +02:00
buffer.writeBytes(data, i, paletteLengthLength);
i += paletteLengthLength;
2019-04-25 20:33:36 +02:00
for(int actPaletteId = 0; actPaletteId < paletteLength; actPaletteId++){
2019-11-16 08:37:33 +01:00
int actPalette = ITechHider.readVarInt(data, i);
int actPaletteLength = ITechHider.readVarIntLength(data, i);
2019-04-25 20:33:36 +02:00
int blockId = actPalette >> 4;
if(Config.HiddenBlocks.contains(blockId)){
2019-11-16 08:37:33 +01:00
byte[] a = ITechHider.writeVarInt(obfuscateShift4);
2019-09-25 18:21:20 +02:00
buffer.writeBytes(a);
2019-04-25 20:33:36 +02:00
changed = true;
}else{
2019-09-25 18:21:20 +02:00
buffer.writeBytes(data, i, actPaletteLength);
2019-04-25 20:33:36 +02:00
}
2019-09-25 18:21:20 +02:00
i += actPaletteLength;
2019-04-25 20:33:36 +02:00
}
2019-11-16 08:37:33 +01:00
int dataArrayLength = ITechHider.readVarInt(data, i);
int dataArrayLengthLength = ITechHider.readVarIntLength(data, i);
2019-09-25 18:21:20 +02:00
buffer.writeBytes(data, i, dataArrayLength * 8 + dataArrayLengthLength);
i += dataArrayLengthLength;
2019-04-25 20:33:36 +02:00
i += dataArrayLength * 8;
}else{
2019-11-16 08:37:33 +01:00
int dataArrayLength = ITechHider.readVarInt(data, i);
int dataArrayLengthLength = ITechHider.readVarIntLength(data, i);
2019-09-25 18:21:20 +02:00
buffer.writeBytes(data, i, dataArrayLength*8 + dataArrayLengthLength);
i += dataArrayLengthLength;
i += dataArrayLength * 8;
2019-04-25 20:33:36 +02:00
}
2019-09-25 18:21:20 +02:00
buffer.writeBytes(data, i, 4096);
2019-04-25 20:33:36 +02:00
i += 4096; //Skylight (Not in Nether/End!!!) 2048 + Blocklight 2048
}
if(changed){
2019-09-29 19:35:32 +02:00
data = new byte[buffer.readableBytes()];
2019-09-25 18:21:20 +02:00
buffer.readBytes(data);
byteArray.write(0, data);
2019-04-25 20:33:36 +02:00
}
}
2019-11-10 17:29:59 +01:00
}).start(threadMultiplier * 4);
}
2019-11-10 17:29:59 +01:00
private static void multiBlockHider(){
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Server.MULTI_BLOCK_CHANGE) {
2019-04-25 20:33:36 +02:00
@Override
public void onPacketSending(PacketEvent e) {
2019-07-01 21:02:32 +02:00
PacketContainer packet = e.getPacket();
2019-04-25 20:33:36 +02:00
2019-05-28 06:16:16 +02:00
Player p = e.getPlayer();
2019-06-14 19:11:09 +02:00
ChunkCoordIntPair pos = packet.getChunkCoordIntPairs().read(0);
2019-05-28 06:16:16 +02:00
if(bypass(p, pos.getChunkX(), pos.getChunkZ()))
return;
PacketContainer cached = packetCache.get(packet);
if(cached != null){
e.setPacket(cached);
return;
}
cached = packet.shallowClone();
packetCache.put(packet, cached);
e.setPacket(cached);
StructureModifier<MultiBlockChangeInfo[]> blockStructure = cached.getMultiBlockChangeInfoArrays();
2019-07-01 21:02:32 +02:00
MultiBlockChangeInfo[] changes = blockStructure.read(0).clone();
2019-05-28 06:16:16 +02:00
boolean changed = false;
2019-04-25 20:33:36 +02:00
for(MultiBlockChangeInfo mbci : changes){
WrappedBlockData block = mbci.getData();
2019-11-10 17:29:59 +01:00
//noinspection deprecation
2019-04-25 20:33:36 +02:00
if(Config.HiddenBlocks.contains(block.getType().getId())){
changed = true;
2019-05-28 06:16:16 +02:00
block.setType(obfuscateMaterial);
2019-04-25 20:33:36 +02:00
mbci.setData(block);
}
}
if(changed){
blockStructure.write(0, changes);
}
}
});
2019-11-10 17:29:59 +01:00
}
private static void blockHider(){
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Server.BLOCK_CHANGE) {
2019-04-25 20:33:36 +02:00
@Override
public void onPacketSending(PacketEvent e) {
PacketContainer packet = e.getPacket();
2019-05-28 06:16:16 +02:00
BlockPosition pos = packet.getBlockPositionModifier().read(0);
2019-05-28 06:16:16 +02:00
Player p = e.getPlayer();
2019-11-16 08:37:33 +01:00
if(bypass(p, ITechHider.posToChunk(pos.getX()), ITechHider.posToChunk(pos.getZ())))
2019-05-28 06:16:16 +02:00
return;
2019-04-25 20:33:36 +02:00
2019-11-10 17:29:59 +01:00
PacketContainer cached = packetCache.get(packet);
if(cached != null){
e.setPacket(cached);
return;
}
cached = packet.deepClone();
packetCache.put(packet, cached);
e.setPacket(cached);
StructureModifier<WrappedBlockData> blockStructure = cached.getBlockData();
WrappedBlockData block = blockStructure.read(0);
//noinspection deprecation
if(Config.HiddenBlocks.contains(block.getType().getId())){
block.setType(obfuscateMaterial);
blockStructure.write(0, block);
}
2019-04-25 20:33:36 +02:00
}
});
2019-04-25 20:33:36 +02:00
}
2019-11-10 17:29:59 +01:00
private static void blockActionHider(){
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Server.BLOCK_ACTION) {
@Override
public void onPacketSending(PacketEvent e) {
PacketContainer packet = e.getPacket();
BlockPosition pos = packet.getBlockPositionModifier().read(0);
2019-09-05 18:26:13 +02:00
2019-11-10 17:29:59 +01:00
Player p = e.getPlayer();
2019-11-16 08:37:33 +01:00
if(bypass(p, ITechHider.posToChunk(pos.getX()), ITechHider.posToChunk(pos.getZ())))
2019-11-10 17:29:59 +01:00
return;
e.setCancelled(true);
}
});
2019-05-28 06:16:16 +02:00
}
public static List<Pair<Integer, Integer>> prepareChunkReload(Player p){
2019-09-12 20:46:21 +02:00
if(disabled())
return Collections.emptyList();
List<Pair<Integer, Integer>> chunksToReload = new ArrayList<>();
for(int x = arenaMinX; x <= arenaMaxX; x++)
for(int z = arenaMinZ; z <= arenaMaxZ; z++)
if(!bypass(p, x, z))
chunksToReload.add(new Pair<>(x, z));
return chunksToReload;
}
public static void reloadChunks(Player p, List<Pair<Integer, Integer>> chunksToReload){
2019-09-12 20:46:21 +02:00
if(disabled())
return;
Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> {
for(Pair<Integer, Integer> chunk : chunksToReload){
if(bypass(p, chunk.getKey(), chunk.getValue()))
2019-11-10 22:34:22 +01:00
reloadChunk(p, chunk);
}
2019-06-22 17:11:10 +02:00
}, 40);
}
2019-11-10 22:34:22 +01:00
private static void reloadChunk(Player p, Pair<Integer, Integer> chunk){
switch(Core.getVersion()){
case 8:
2019-11-15 08:22:16 +01:00
TechHider_8.reloadChunk(p, chunk);
2019-11-10 22:34:22 +01:00
break;
default:
2019-11-15 08:22:16 +01:00
TechHider_12.reloadChunk(p, chunk);
2019-11-10 22:34:22 +01:00
}
}
2019-09-12 20:46:21 +02:00
private static boolean disabled(){
2019-11-16 08:37:33 +01:00
return Config.OnlyPublicSchematics;
2019-04-25 20:33:36 +02:00
}
}