Skip to content

Commit

Permalink
[#202] deobfuscate block on block fall event (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingrim4 authored Mar 8, 2022
1 parent e18dc9d commit 6278060
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public abstract class AbstractNmsManager implements NmsManager {

private static final byte FLAG_AIR = 1;
private static final byte FLAG_OCCLUDING = 2;
private static final byte FLAG_TILE_ENTITY = 4;
private static final byte FLAG_FALLABLE = 4;
private static final byte FLAG_TILE_ENTITY = 8;

private final AbstractRegionFileCache<?> regionFileCache;
private final Map<Material, Set<Integer>> materialToIds = new HashMap<>();
Expand All @@ -31,10 +32,11 @@ protected final void registerMaterialId(Material material, int id) {
this.materialToIds.computeIfAbsent(material, key -> new HashSet<>()).add(id);
}

protected final void setBlockFlags(int blockId, boolean isAir, boolean canOcclude, boolean isTileEntity) {
protected final void setBlockFlags(int blockId, boolean isAir, boolean canOcclude, boolean canFall, boolean isTileEntity) {
byte flags = this.blockFlags[blockId];
flags |= isAir ? FLAG_AIR : 0;
flags |= canOcclude ? FLAG_OCCLUDING : 0;
flags |= canFall ? FLAG_FALLABLE : 0;
flags |= isTileEntity ? FLAG_TILE_ENTITY : 0;
this.blockFlags[blockId] = flags;
}
Expand All @@ -46,7 +48,7 @@ public final AbstractRegionFileCache<?> getRegionFileCache() {

@Override
public final Set<Integer> getBlockIds(Material material) {
return Collections.unmodifiableSet(this.materialToIds.get(material));
return Collections.unmodifiableSet(this.materialToIds.getOrDefault(material, Collections.emptySet()));
}

@Override
Expand All @@ -59,6 +61,11 @@ public final boolean isOccluding(int blockId) {
return (this.blockFlags[blockId] & FLAG_OCCLUDING) != 0;
}

@Override
public boolean isFallable(int blockId) {
return (this.blockFlags[blockId] & FLAG_FALLABLE) != 0;
}

@Override
public final boolean isBlockEntity(int blockId) {
return (this.blockFlags[blockId] & FLAG_TILE_ENTITY) != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface NmsManager {

boolean isOccluding(int blockId);

boolean isFallable(int blockId);

boolean isBlockEntity(int blockId);

ReadOnlyChunk getReadOnlyChunk(World world, int chunkX, int chunkZ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_10_R1.Block;
import net.minecraft.server.v1_10_R1.BlockAir;
import net.minecraft.server.v1_10_R1.BlockFalling;
import net.minecraft.server.v1_10_R1.BlockPosition;
import net.minecraft.server.v1_10_R1.Chunk;
import net.minecraft.server.v1_10_R1.ChunkProviderServer;
Expand Down Expand Up @@ -73,7 +74,7 @@ public NmsManager(Config config) {
int blockId = getBlockId(blockData);
this.registerMaterialId(material, blockId);
Block block = blockData.getBlock();
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block.isTileEntity());
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block instanceof BlockFalling, block.isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_11_R1.Block;
import net.minecraft.server.v1_11_R1.BlockAir;
import net.minecraft.server.v1_11_R1.BlockFalling;
import net.minecraft.server.v1_11_R1.BlockPosition;
import net.minecraft.server.v1_11_R1.Chunk;
import net.minecraft.server.v1_11_R1.ChunkProviderServer;
Expand Down Expand Up @@ -73,7 +74,7 @@ public NmsManager(Config config) {
int blockId = getBlockId(blockData);
this.registerMaterialId(material, blockId);
Block block = blockData.getBlock();
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block.isTileEntity());
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block instanceof BlockFalling, block.isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_12_R1.Block;
import net.minecraft.server.v1_12_R1.BlockAir;
import net.minecraft.server.v1_12_R1.BlockFalling;
import net.minecraft.server.v1_12_R1.BlockPosition;
import net.minecraft.server.v1_12_R1.Chunk;
import net.minecraft.server.v1_12_R1.ChunkProviderServer;
Expand Down Expand Up @@ -73,7 +74,7 @@ public NmsManager(Config config) {
int blockId = getBlockId(blockData);
this.registerMaterialId(material, blockId);
Block block = blockData.getBlock();
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block.isTileEntity());
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block instanceof BlockFalling, block.isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_13_R1.Block;
import net.minecraft.server.v1_13_R1.BlockFalling;
import net.minecraft.server.v1_13_R1.BlockPosition;
import net.minecraft.server.v1_13_R1.Chunk;
import net.minecraft.server.v1_13_R1.ChunkProviderServer;
Expand Down Expand Up @@ -71,8 +72,9 @@ public NmsManager(Config config) {
IBlockData blockData = iterator.next();
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.getBlock().isTileEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof BlockFalling, blockData.getBlock().isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_13_R2.Block;
import net.minecraft.server.v1_13_R2.BlockFalling;
import net.minecraft.server.v1_13_R2.BlockPosition;
import net.minecraft.server.v1_13_R2.Chunk;
import net.minecraft.server.v1_13_R2.ChunkProviderServer;
Expand Down Expand Up @@ -72,8 +73,9 @@ public NmsManager(Config config) {
IBlockData blockData = iterator.next();
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.getBlock().isTileEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof BlockFalling, blockData.getBlock().isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_14_R1.Block;
import net.minecraft.server.v1_14_R1.BlockFalling;
import net.minecraft.server.v1_14_R1.BlockPosition;
import net.minecraft.server.v1_14_R1.Chunk;
import net.minecraft.server.v1_14_R1.ChunkProviderServer;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (IBlockData blockData : Block.REGISTRY_ID) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.getBlock().isTileEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof BlockFalling, blockData.getBlock().isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_15_R1.Block;
import net.minecraft.server.v1_15_R1.BlockFalling;
import net.minecraft.server.v1_15_R1.BlockPosition;
import net.minecraft.server.v1_15_R1.Chunk;
import net.minecraft.server.v1_15_R1.ChunkProviderServer;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (IBlockData blockData : Block.REGISTRY_ID) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.getBlock().isTileEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof BlockFalling, blockData.getBlock().isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_16_R1.Block;
import net.minecraft.server.v1_16_R1.BlockFalling;
import net.minecraft.server.v1_16_R1.BlockPosition;
import net.minecraft.server.v1_16_R1.Chunk;
import net.minecraft.server.v1_16_R1.ChunkProviderServer;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (IBlockData blockData : Block.REGISTRY_ID) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.getBlock().isTileEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof BlockFalling, blockData.getBlock().isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_16_R2.Block;
import net.minecraft.server.v1_16_R2.BlockFalling;
import net.minecraft.server.v1_16_R2.BlockPosition;
import net.minecraft.server.v1_16_R2.Chunk;
import net.minecraft.server.v1_16_R2.ChunkProviderServer;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (IBlockData blockData : Block.REGISTRY_ID) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.getBlock().isTileEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof BlockFalling, blockData.getBlock().isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_16_R3.Block;
import net.minecraft.server.v1_16_R3.BlockFalling;
import net.minecraft.server.v1_16_R3.BlockPosition;
import net.minecraft.server.v1_16_R3.Chunk;
import net.minecraft.server.v1_16_R3.ChunkProviderServer;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (IBlockData blockData : Block.REGISTRY_ID) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.getBlock().isTileEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof BlockFalling, blockData.getBlock().isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Fallable;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (BlockState blockData : Block.BLOCK_STATE_REGISTRY) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.hasBlockEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof Fallable, blockData.hasBlockEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Fallable;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (BlockState blockData : Block.BLOCK_STATE_REGISTRY) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.hasBlockEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof Fallable, blockData.hasBlockEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Fallable;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
Expand Down Expand Up @@ -70,8 +71,9 @@ public NmsManager(Config config) {
for (BlockState blockData : Block.BLOCK_STATE_REGISTRY) {
Material material = CraftBlockData.fromData(blockData).getMaterial();
int blockId = getBlockId(blockData);
Block block = blockData.getBlock();
this.registerMaterialId(material, blockId);
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), blockData.hasBlockEntity());
this.setBlockFlags(blockId, blockData.isAir(), material.isOccluding(), block instanceof Fallable, blockData.hasBlockEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.server.v1_9_R2.Block;
import net.minecraft.server.v1_9_R2.BlockAir;
import net.minecraft.server.v1_9_R2.BlockFalling;
import net.minecraft.server.v1_9_R2.BlockPosition;
import net.minecraft.server.v1_9_R2.Chunk;
import net.minecraft.server.v1_9_R2.ChunkProviderServer;
Expand Down Expand Up @@ -73,7 +74,7 @@ public NmsManager(Config config) {
int blockId = getBlockId(blockData);
this.registerMaterialId(material, blockId);
Block block = blockData.getBlock();
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block.isTileEntity());
this.setBlockFlags(blockId, block instanceof BlockAir, material.isOccluding(), block instanceof BlockFalling, block.isTileEntity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public static boolean isOccluding(int blockId) {
return instance.isOccluding(blockId);
}

public static boolean isFallable(int blockId) {
return instance.isFallable(blockId);
}

public static boolean isBlockEntity(int blockId) {
return instance.isBlockEntity(blockId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package net.imprex.orebfuscator.obfuscation;

import java.util.BitSet;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.EventHandler;
Expand All @@ -10,6 +13,7 @@
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
Expand All @@ -35,10 +39,20 @@ public static void createAndRegister(Orebfuscator orebfuscator, DeobfuscationWor
private final OrebfuscatorConfig config;
private final DeobfuscationWorker deobfuscationWorker;

private final BitSet occludingFallable = new BitSet();

private DeobfuscationListener(Orebfuscator orebfuscator, DeobfuscationWorker deobfuscationWorker) {
this.updateSystem = orebfuscator.getUpdateSystem();
this.config = orebfuscator.getOrebfuscatorConfig();
this.deobfuscationWorker = deobfuscationWorker;

for (Material material : Material.values()) {
for (int blockId : NmsInstance.getBlockIds(material)) {
if (NmsInstance.isFallable(blockId) && NmsInstance.isOccluding(blockId)) {
this.occludingFallable.set(material.ordinal());
}
}
}
}

@EventHandler
Expand Down Expand Up @@ -73,6 +87,13 @@ public void onBlockPistonRetract(BlockPistonRetractEvent event) {
this.deobfuscationWorker.deobfuscate(event.getBlocks(), true);
}

@EventHandler
public void onBlockPhysics(BlockPhysicsEvent event) {
if (this.occludingFallable.get(event.getBlock().getType().ordinal())) {
this.deobfuscationWorker.deobfuscate(event.getBlock());
}
}

@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
this.deobfuscationWorker.deobfuscate(event.blockList(), true);
Expand Down

0 comments on commit 6278060

Please sign in to comment.