Skip to content

Commit

Permalink
Issue #146: Obfuscate signs based on whether the sign has text or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey-Terzi committed Apr 29, 2017
1 parent 2b738f4 commit 14b705c
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public interface INmsManager {
BlockState getBlockState(World world, int x, int y, int z);

int getBlockId(World world, int x, int y, int z);

String getTextFromChatComponent(String json);
}
2 changes: 1 addition & 1 deletion Plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.lishid</groupId>
<artifactId>orebfuscator</artifactId>
<version>4.2.1-SNAPSHOT</version>
<version>4.2.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Orebfuscator4</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

package com.lishid.orebfuscator.chunkmap;

import java.util.List;

import com.comphenix.protocol.wrappers.nbt.NbtCompound;

public class ChunkData {
public int chunkX;
public int chunkZ;
Expand All @@ -13,4 +17,5 @@ public class ChunkData {
public byte[] data;
public boolean isOverworld;
public boolean useCache;
public List<NbtCompound> blockEntities;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public WorldConfig getWorld(World world) {
break;
default:
baseCfg = this.orebfuscatorConfig.getNormalWorld();
break;
}

WorldConfig cfg = this.orebfuscatorConfig.getWorlds().get(world.getName().toLowerCase());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class WorldConfig {
private Boolean enabled;
private Boolean darknessHideBlocks;
private Boolean antiTexturePackAndFreecam;
private Integer airGeneratorMaxChance;
private Boolean bypassObfuscationForSignsWithText;
private Integer airGeneratorMaxChance;
private boolean[] obfuscateBlocks;
private boolean[] obfuscateAndProximityBlocks;
private boolean[] darknessBlocks;
Expand All @@ -34,6 +35,7 @@ public void setDefaults() {
this.enabled = true;
this.darknessHideBlocks = false;
this.antiTexturePackAndFreecam = true;
this.bypassObfuscationForSignsWithText = false;
this.airGeneratorMaxChance = 43;
this.obfuscateBlocks = new boolean[256];

Expand Down Expand Up @@ -68,6 +70,10 @@ public void init(WorldConfig baseWorld) {
this.antiTexturePackAndFreecam = baseWorld.antiTexturePackAndFreecam;
}

if(this.bypassObfuscationForSignsWithText == null) {
this.bypassObfuscationForSignsWithText = baseWorld.bypassObfuscationForSignsWithText;
}

if(this.airGeneratorMaxChance == null) {
this.airGeneratorMaxChance = baseWorld.airGeneratorMaxChance;
}
Expand Down Expand Up @@ -126,6 +132,14 @@ public void setAntiTexturePackAndFreecam(Boolean value) {
this.antiTexturePackAndFreecam = value;
}

public Boolean isBypassObfuscationForSignsWithText() {
return this.bypassObfuscationForSignsWithText;
}

public void setBypassObfuscationForSignsWithText(Boolean value) {
this.bypassObfuscationForSignsWithText = value;
}

public Integer getAirGeneratorMaxChance() {
return this.airGeneratorMaxChance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ private WorldConfig readWorld(String worldPath, WorldConfig cfg, WorldType world
Boolean antiTexturePackAndFreecam = getBoolean(worldPath + ".AntiTexturePackAndFreecam", cfg.isAntiTexturePackAndFreecam(), withSave);
Integer airGeneratorMaxChance = getInt(worldPath + ".AirGeneratorMaxChance", cfg.getAirGeneratorMaxChance(), 40, 100, withSave);
Boolean darknessHideBlocks = getBoolean(worldPath + ".DarknessHideBlocks", cfg.isDarknessHideBlocks(), withSave);
Boolean bypassObfuscationForSignsWithText = getBoolean(worldPath + ".BypassObfuscationForSignsWithText", cfg.isBypassObfuscationForSignsWithText(), withSave);
boolean[] darknessBlocks = readBlockMatrix(cfg.getDarknessBlocks(), cfg.getDarknessBlockIds(), worldPath + ".DarknessBlocks", withSave);
Integer mode1Block = this.materialReader.getMaterialIdByPath(worldPath + ".Mode1Block", cfg.getMode1BlockId(), withSave);
Integer[] randomBlocks = this.materialReader.getMaterialIdsByPath(worldPath + ".RandomBlocks", cfg.getRandomBlocks(), withSave);
Expand All @@ -204,6 +205,7 @@ private WorldConfig readWorld(String worldPath, WorldConfig cfg, WorldType world

cfg.setEnabled(enabled);
cfg.setAntiTexturePackAndFreecam(antiTexturePackAndFreecam);
cfg.setBypassObfuscationForSignsWithText(bypassObfuscationForSignsWithText);
cfg.setAirGeneratorMaxChance(airGeneratorMaxChance);
cfg.setDarknessHideBlocks(darknessHideBlocks);
cfg.setDarknessBlocks(darknessBlocks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.lishid.orebfuscator.hook;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import com.comphenix.protocol.PacketType;
Expand All @@ -27,7 +31,11 @@
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
import com.lishid.orebfuscator.Orebfuscator;
import com.lishid.orebfuscator.chunkmap.ChunkData;
import com.lishid.orebfuscator.config.WorldConfig;
import com.lishid.orebfuscator.hithack.BlockHitManager;
import com.lishid.orebfuscator.obfuscation.Calculations;

Expand All @@ -53,6 +61,7 @@ public void onPacketSending(PacketEvent event) {
chunkData.primaryBitMask = ints.read(2);
chunkData.data = byteArray.read(0);
chunkData.isOverworld = event.getPlayer().getWorld().getEnvironment() == World.Environment.NORMAL;
chunkData.blockEntities = getBlockEntities(packet, event.getPlayer());

try {
byte[] newData = Calculations.obfuscateOrUseCache(chunkData, event.getPlayer());
Expand All @@ -79,6 +88,24 @@ public void onPacketReceiving(PacketEvent event) {
});
}

@SuppressWarnings("rawtypes")
private static List<NbtCompound> getBlockEntities(PacketContainer packet, Player player) {
WorldConfig worldConfig = Orebfuscator.configManager.getWorld(player.getWorld());

if(!worldConfig.isBypassObfuscationForSignsWithText()) {
return null;
}

List list = packet.getSpecificModifier(List.class).read(0);
List<NbtCompound> result = new ArrayList<NbtCompound>();

for(Object tag : list) {
result.add(NbtFactory.fromNMSCompound(tag));
}

return result;
}

/*
private static boolean _isSaved;
private void saveTestData(ChunkData chunkData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
import java.util.ArrayList;
import java.util.Random;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;

import com.comphenix.protocol.wrappers.nbt.NbtBase;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtType;
import com.lishid.orebfuscator.DeprecatedMethods;
import com.lishid.orebfuscator.Orebfuscator;
import com.lishid.orebfuscator.cache.ObfuscatedCachedChunk;
import com.lishid.orebfuscator.cache.ObfuscatedDataCache;
Expand Down Expand Up @@ -173,7 +178,7 @@ private static byte[] obfuscate(ChunkData chunkData, Player player, ArrayList<Bl
}

// Check if the block is obfuscated
if (obfuscate) {
if (obfuscate && canObfuscate(chunkData, x, y, z, blockState)) {
if (specialObfuscate) {
// Proximity hider
blockState.id = proximityHider.getSpecialBlockID();
Expand Down Expand Up @@ -247,6 +252,60 @@ private static byte[] obfuscate(ChunkData chunkData, Player player, ArrayList<Bl
return output;
}

private static boolean canObfuscate(ChunkData chunkData, int x, int y, int z, BlockState blockState) {
if(chunkData.blockEntities == null
|| (
blockState.id != DeprecatedMethods.getMaterialId(Material.WALL_SIGN)
&& blockState.id != DeprecatedMethods.getMaterialId(Material.SIGN_POST)
)
)
{
return true;
}

NbtCompound tag = getNbtTag(chunkData, x, y, z);

return tag == null ||
isSignTextEmpty(tag, "Text1")
&& isSignTextEmpty(tag, "Text2")
&& isSignTextEmpty(tag, "Text3")
&& isSignTextEmpty(tag, "Text4");
}

private static boolean isSignTextEmpty(NbtCompound compound, String key) {
NbtBase<?> tag = compound.getValue(key);

if(tag == null || tag.getType() != NbtType.TAG_STRING) {
return true;
}

String json = (String)tag.getValue();

if(json == null || json.isEmpty()) {
return true;
}

String text = Orebfuscator.nms.getTextFromChatComponent(json);

return text == null || text.isEmpty();
}

private static NbtCompound getNbtTag(ChunkData chunkData, int x, int y, int z) {
for(NbtCompound tag : chunkData.blockEntities) {
if(tag != null) {
if(x == tag.getInteger("x")
&& y == tag.getInteger("y")
&& z == tag.getInteger("z")
)
{
return tag;
}
}
}

return null;
}

private static void addBlocksToPalette(ChunkMapManager manager, WorldConfig worldConfig) {
if(!manager.inputHasNonAirBlock()) {
return;
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
The definitive Anti X-Ray plugin for CraftBukkit

###Changelog:
- Updated to support 1.11 - 1.11.2
- Updated to support 1.10 (Thanks Asgorioth)
- Updated to support 1.9.4
- Updated to support 1.9-1.9.2 (https://www.devotedmc.com)
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>techcable-repo</id>
<url>https://repo.techcable.net/content/groups/public/</url>
</repository>
</repositories>

<modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.server.v1_10_R1.Chunk;
import net.minecraft.server.v1_10_R1.ChunkProviderServer;
import net.minecraft.server.v1_10_R1.IBlockData;
import net.minecraft.server.v1_10_R1.IChatBaseComponent;
import net.minecraft.server.v1_10_R1.Packet;
import net.minecraft.server.v1_10_R1.PlayerChunkMap;
import net.minecraft.server.v1_10_R1.TileEntity;
Expand All @@ -19,6 +20,7 @@
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_10_R1.util.CraftChatMessage;
import org.bukkit.entity.Player;

import com.lishid.orebfuscator.nms.IBlockInfo;
Expand All @@ -40,17 +42,20 @@ public INBT createNBT() {
return new NBT();
}

@Override
public IChunkCache createChunkCache() {
return new ChunkCache(this.maxLoadedCacheFiles);
}

@Override
public IChunkManager getChunkManager(World world) {
WorldServer worldServer = ((CraftWorld)world).getHandle();
PlayerChunkMap chunkMap = worldServer.getPlayerChunkMap();

return new ChunkManager(chunkMap);
}

@Override
public void updateBlockTileEntity(BlockCoord blockCoord, Player player) {
CraftWorld world = (CraftWorld)player.getWorld();
TileEntity tileEntity = world.getTileEntityAt(blockCoord.x, blockCoord.y, blockCoord.z);
Expand All @@ -67,17 +72,20 @@ public void updateBlockTileEntity(BlockCoord blockCoord, Player player) {
}
}

@Override
public void notifyBlockChange(World world, IBlockInfo blockInfo) {
BlockPosition blockPosition = new BlockPosition(blockInfo.getX(), blockInfo.getY(), blockInfo.getZ());
IBlockData blockData = ((BlockInfo)blockInfo).getBlockData();

((CraftWorld)world).getHandle().notify(blockPosition, blockData, blockData, 0);
}

@Override
public int getBlockLightLevel(World world, int x, int y, int z) {
return ((CraftWorld)world).getHandle().getLightLevel(new BlockPosition(x, y, z));
}

@Override
public IBlockInfo getBlockInfo(World world, int x, int y, int z) {
IBlockData blockData = getBlockData(world, x, y, z);

Expand All @@ -86,6 +94,7 @@ public IBlockInfo getBlockInfo(World world, int x, int y, int z) {
: null;
}

@Override
public BlockState getBlockState(World world, int x, int y, int z) {
IBlockData blockData = getBlockData(world, x, y, z);

Expand All @@ -100,12 +109,19 @@ public BlockState getBlockState(World world, int x, int y, int z) {
return blockState;
}

@Override
public int getBlockId(World world, int x, int y, int z) {
IBlockData blockData = getBlockData(world, x, y, z);

return blockData != null ? Block.getId(blockData.getBlock()): -1;
}

@Override
public String getTextFromChatComponent(String json) {
IChatBaseComponent component = IChatBaseComponent.ChatSerializer.a(json);
return CraftChatMessage.fromComponent(component);
}

private static IBlockData getBlockData(World world, int x, int y, int z) {
int chunkX = x >> 4;
int chunkZ = z >> 4;
Expand Down
Loading

0 comments on commit 14b705c

Please sign in to comment.