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;
|
2019-11-25 17:53:23 +01:00
|
|
|
import com.comphenix.protocol.events.PacketContainer;
|
2019-04-25 20:33:36 +02:00
|
|
|
import com.comphenix.protocol.events.PacketEvent;
|
2019-11-25 17:53:23 +01:00
|
|
|
import com.comphenix.protocol.reflect.StructureModifier;
|
|
|
|
import com.comphenix.protocol.wrappers.BlockPosition;
|
|
|
|
import com.comphenix.protocol.wrappers.ChunkCoordIntPair;
|
|
|
|
import com.comphenix.protocol.wrappers.MultiBlockChangeInfo;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedBlockData;
|
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-11-25 17:53:23 +01:00
|
|
|
import de.steamwar.fightsystem.IFightSystem;
|
2019-06-05 22:14:27 +02:00
|
|
|
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.entity.Player;
|
|
|
|
|
2019-11-23 18:15:45 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
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(){}
|
|
|
|
|
2019-10-07 08:23:24 +02:00
|
|
|
private static boolean running = false;
|
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-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-10-07 08:23:24 +02:00
|
|
|
|
2019-11-23 18:15:45 +01:00
|
|
|
if(p.getGameMode() == GameMode.SPECTATOR)
|
2019-11-10 22:34:22 +01:00
|
|
|
e.setCancelled(true);
|
2019-10-07 08:23:24 +02:00
|
|
|
}
|
2019-11-10 22:34:22 +01:00
|
|
|
});
|
|
|
|
}
|
2019-11-08 09:32:43 +01:00
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Client.USE_ENTITY) {
|
2019-10-07 08:23:24 +02:00
|
|
|
@Override
|
|
|
|
public void onPacketReceiving(PacketEvent e) {
|
|
|
|
Player p = e.getPlayer();
|
|
|
|
|
2019-11-23 18:15:45 +01:00
|
|
|
if(p.getGameMode() == GameMode.SPECTATOR)
|
2019-10-07 08:23:24 +02:00
|
|
|
e.setCancelled(true);
|
|
|
|
}
|
2019-11-08 09:32:43 +01:00
|
|
|
});
|
2019-10-07 08:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void start(){
|
|
|
|
if(running)
|
|
|
|
return;
|
|
|
|
running = true;
|
|
|
|
|
2019-11-15 08:22:16 +01:00
|
|
|
if(disabled())
|
2019-10-07 08:23:24 +02:00
|
|
|
return;
|
|
|
|
|
2019-11-25 17:53:23 +01:00
|
|
|
blockActionHider();
|
|
|
|
blockHider();
|
|
|
|
multiBlockHider();
|
2020-01-19 07:12:22 +01:00
|
|
|
updateBlockEntity();
|
2019-11-15 08:22:16 +01:00
|
|
|
switch(Core.getVersion()){
|
2019-12-27 12:02:27 +01:00
|
|
|
case 15:
|
|
|
|
TechHider_15.start();
|
|
|
|
break;
|
2019-11-25 17:53:23 +01:00
|
|
|
case 14:
|
|
|
|
TechHider_14.start();
|
|
|
|
break;
|
2019-12-27 11:05:12 +01:00
|
|
|
case 10:
|
|
|
|
case 9:
|
|
|
|
case 8:
|
|
|
|
break;
|
2019-11-15 08:22:16 +01:00
|
|
|
default:
|
2019-11-23 18:15:45 +01:00
|
|
|
TechHider_12.start();
|
2019-11-15 08:22:16 +01:00
|
|
|
}
|
2019-11-10 17:29:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:53:23 +01:00
|
|
|
private static void multiBlockHider(){
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(IFightSystem.getPlugin(), PacketType.Play.Server.MULTI_BLOCK_CHANGE) {
|
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent e) {
|
|
|
|
PacketContainer packet = e.getPacket();
|
|
|
|
|
|
|
|
Player p = e.getPlayer();
|
|
|
|
ChunkCoordIntPair pos = packet.getChunkCoordIntPairs().read(0);
|
|
|
|
if(bypass(p, pos.getChunkX(), pos.getChunkZ()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
PacketContainer cached = ITechHider.packetCache.get(packet);
|
|
|
|
if(cached != null){
|
|
|
|
e.setPacket(cached);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = packet.shallowClone();
|
|
|
|
ITechHider.packetCache.put(packet, cached);
|
|
|
|
e.setPacket(cached);
|
|
|
|
StructureModifier<MultiBlockChangeInfo[]> blockStructure = cached.getMultiBlockChangeInfoArrays();
|
|
|
|
MultiBlockChangeInfo[] changes = blockStructure.read(0).clone();
|
|
|
|
boolean changed = false;
|
|
|
|
for(MultiBlockChangeInfo mbci : changes){
|
|
|
|
WrappedBlockData block = mbci.getData();
|
2019-12-31 17:04:00 +01:00
|
|
|
if(Config.HiddenBlockTags.contains(block.getType().name())){
|
2019-11-25 17:53:23 +01:00
|
|
|
changed = true;
|
|
|
|
block.setType(ITechHider.obfuscateMaterial);
|
|
|
|
mbci.setData(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(changed){
|
|
|
|
blockStructure.write(0, changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void blockHider(){
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(IFightSystem.getPlugin(), PacketType.Play.Server.BLOCK_CHANGE) {
|
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent e) {
|
|
|
|
PacketContainer packet = e.getPacket();
|
|
|
|
BlockPosition pos = packet.getBlockPositionModifier().read(0);
|
|
|
|
|
|
|
|
Player p = e.getPlayer();
|
|
|
|
if(bypass(p, ITechHider.posToChunk(pos.getX()), ITechHider.posToChunk(pos.getZ())))
|
|
|
|
return;
|
|
|
|
|
|
|
|
PacketContainer cached = ITechHider.packetCache.get(packet);
|
|
|
|
if(cached != null){
|
|
|
|
e.setPacket(cached);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = packet.deepClone();
|
|
|
|
ITechHider.packetCache.put(packet, cached);
|
|
|
|
e.setPacket(cached);
|
|
|
|
StructureModifier<WrappedBlockData> blockStructure = cached.getBlockData();
|
|
|
|
WrappedBlockData block = blockStructure.read(0);
|
2019-12-31 17:04:00 +01:00
|
|
|
if(Config.HiddenBlockTags.contains(block.getType().name())){
|
2019-11-25 17:53:23 +01:00
|
|
|
block.setType(ITechHider.obfuscateMaterial);
|
|
|
|
blockStructure.write(0, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void blockActionHider(){
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(IFightSystem.getPlugin(), PacketType.Play.Server.BLOCK_ACTION) {
|
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent e) {
|
|
|
|
PacketContainer packet = e.getPacket();
|
|
|
|
BlockPosition pos = packet.getBlockPositionModifier().read(0);
|
|
|
|
|
|
|
|
Player p = e.getPlayer();
|
|
|
|
if(bypass(p, ITechHider.posToChunk(pos.getX()), ITechHider.posToChunk(pos.getZ())))
|
|
|
|
return;
|
|
|
|
|
|
|
|
e.setCancelled(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-19 07:12:22 +01:00
|
|
|
private static void updateBlockEntity(){
|
|
|
|
if(Core.getVersion() < 9)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(FightSystem.getPlugin(), PacketType.Play.Server.TILE_ENTITY_DATA) {
|
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
|
|
|
PacketContainer packet = event.getPacket();
|
|
|
|
BlockPosition pos = packet.getBlockPositionModifier().read(0);
|
|
|
|
|
|
|
|
Player p = event.getPlayer();
|
|
|
|
if(bypass(p, ITechHider.posToChunk(pos.getX()), ITechHider.posToChunk(pos.getZ())))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 9 == Set sign text
|
2020-03-01 13:35:10 +01:00
|
|
|
if(packet.getIntegers().read(0) != 9)
|
2020-01-19 07:12:22 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-19 07:38:54 +01:00
|
|
|
public static List<ITechHider.ChunkPos> prepareChunkReload(Player p){
|
2019-09-12 20:46:21 +02:00
|
|
|
if(disabled())
|
|
|
|
return Collections.emptyList();
|
2020-01-19 07:38:54 +01:00
|
|
|
List<ITechHider.ChunkPos> chunksToReload = new ArrayList<>();
|
2019-11-23 18:15:45 +01:00
|
|
|
for(int x = ITechHider.arenaMinX; x <= ITechHider.arenaMaxX; x++)
|
|
|
|
for(int z = ITechHider.arenaMinZ; z <= ITechHider.arenaMaxZ; z++)
|
2019-06-08 12:57:58 +02:00
|
|
|
if(!bypass(p, x, z))
|
2020-01-19 07:38:54 +01:00
|
|
|
chunksToReload.add(new ITechHider.ChunkPos(x, z));
|
2019-06-08 12:57:58 +02:00
|
|
|
return chunksToReload;
|
|
|
|
}
|
2019-06-05 22:14:27 +02:00
|
|
|
|
2020-01-19 07:38:54 +01:00
|
|
|
public static void reloadChunks(Player p, List<ITechHider.ChunkPos> chunksToReload){
|
2019-09-12 20:46:21 +02:00
|
|
|
if(disabled())
|
|
|
|
return;
|
2019-06-08 12:57:58 +02:00
|
|
|
Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> {
|
2020-01-19 07:38:54 +01:00
|
|
|
for(ITechHider.ChunkPos chunk : chunksToReload){
|
|
|
|
if(bypass(p, chunk.x(), chunk.z()))
|
2019-11-10 22:34:22 +01:00
|
|
|
reloadChunk(p, chunk);
|
2019-07-01 18:04:46 +02:00
|
|
|
}
|
2019-06-22 17:11:10 +02:00
|
|
|
}, 40);
|
2019-06-05 22:14:27 +02:00
|
|
|
}
|
|
|
|
|
2020-01-19 07:38:54 +01:00
|
|
|
private static void reloadChunk(Player p, ITechHider.ChunkPos chunk){
|
2019-11-10 22:34:22 +01:00
|
|
|
switch(Core.getVersion()){
|
2019-12-27 12:02:27 +01:00
|
|
|
case 15:
|
|
|
|
TechHider_15.reloadChunk(p, chunk);
|
|
|
|
break;
|
2019-11-25 17:53:23 +01:00
|
|
|
case 14:
|
|
|
|
TechHider_14.reloadChunk(p, chunk);
|
|
|
|
break;
|
2019-12-27 11:05:12 +01:00
|
|
|
case 10:
|
|
|
|
TechHider_10.reloadChunk(p, chunk);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
TechHider_9.reloadChunk(p, chunk);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
TechHider_8.reloadChunk(p, chunk);
|
|
|
|
break;
|
2019-11-10 22:34:22 +01:00
|
|
|
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(){
|
2020-05-19 09:42:49 +02:00
|
|
|
return Config.OnlyPublicSchematics || Config.test() || !Config.TechhiderActive;
|
2019-04-25 20:33:36 +02:00
|
|
|
}
|
|
|
|
}
|