Search in sources :

Example 1 with ChunkPacketBlockControllerAntiXray

use of com.vanillage.raytraceantixray.antixray.ChunkPacketBlockControllerAntiXray in project RayTraceAntiXray by stonar96.

the class WorldListener method onWorldInit.

@SuppressWarnings("deprecation")
@EventHandler
public void onWorldInit(WorldInitEvent event) {
    if (plugin.isEnabled(event.getWorld())) {
        List<String> toTrace = plugin.getConfig().getList("world-settings." + event.getWorld().getName() + ".anti-xray.ray-trace-blocks", plugin.getConfig().getList("world-settings.default.anti-xray.ray-trace-blocks")).stream().filter(o -> o != null).map(String::valueOf).collect(Collectors.toList());
        try {
            Field chunkPacketBlockController = Level.class.getDeclaredField("chunkPacketBlockController");
            chunkPacketBlockController.setAccessible(true);
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            Unsafe unsafe = (Unsafe) theUnsafe.get(null);
            unsafe.putObject(((CraftWorld) event.getWorld()).getHandle(), unsafe.objectFieldOffset(chunkPacketBlockController), new ChunkPacketBlockControllerAntiXray(plugin, plugin.getConfig().getInt("world-settings." + event.getWorld().getName() + ".anti-xray.max-ray-trace-block-count-per-chunk", plugin.getConfig().getInt("world-settings.default.anti-xray.max-ray-trace-block-count-per-chunk")), toTrace.isEmpty() ? null : toTrace, ((CraftWorld) event.getWorld()).getHandle(), MinecraftServer.getServer().executor));
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe) ChunkPacketBlockControllerAntiXray(com.vanillage.raytraceantixray.antixray.ChunkPacketBlockControllerAntiXray) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld) EventHandler(org.bukkit.event.EventHandler)

Example 2 with ChunkPacketBlockControllerAntiXray

use of com.vanillage.raytraceantixray.antixray.ChunkPacketBlockControllerAntiXray in project RayTraceAntiXray by stonar96.

the class RayTraceRunnable method run.

@Override
public void run() {
    List<? extends Location> locations = playerData.getLocations();
    Location playerLocation = locations.get(0);
    ChunkPacketBlockController chunkPacketBlockController = ((CraftWorld) playerLocation.getWorld()).getHandle().chunkPacketBlockController;
    if (!(chunkPacketBlockController instanceof ChunkPacketBlockControllerAntiXray)) {
        return;
    }
    boolean[] solidGlobal = ((ChunkPacketBlockControllerAntiXray) chunkPacketBlockController).solidGlobal;
    double rayTraceDistance = Math.max(plugin.getConfig().getDouble("world-settings." + playerLocation.getWorld().getName() + ".anti-xray.ray-trace-distance", plugin.getConfig().getDouble("world-settings.default.anti-xray.ray-trace-distance")), 0.);
    Location temp = playerLocation.clone();
    temp.setX(playerLocation.getX() - rayTraceDistance);
    temp.setZ(playerLocation.getZ() - rayTraceDistance);
    int chunkXMin = temp.getBlockX() >> 4;
    int chunkZMin = temp.getBlockZ() >> 4;
    temp.setX(playerLocation.getX() + rayTraceDistance);
    temp.setZ(playerLocation.getZ() + rayTraceDistance);
    int chunkXMax = temp.getBlockX() >> 4;
    int chunkZMax = temp.getBlockZ() >> 4;
    double rayTraceDistanceSquared = rayTraceDistance * rayTraceDistance;
    for (Location location : locations) {
        Vector vector = location.toVector();
        Vector direction = location.getDirection();
        for (Entry<ChunkPos, ChunkBlocks> chunkEntry : playerData.getChunks().entrySet()) {
            ChunkBlocks chunkBlocks = chunkEntry.getValue();
            LevelChunk chunk = chunkBlocks.getChunk();
            if (chunk == null) {
                playerData.getChunks().remove(chunkEntry.getKey(), chunkBlocks);
                continue;
            }
            if (chunk.locX < chunkXMin || chunk.locX > chunkXMax || chunk.locZ < chunkZMin || chunk.locZ > chunkZMax) {
                continue;
            }
            Iterator<? extends BlockPos> iterator = chunkBlocks.getBlocks().iterator();
            while (iterator.hasNext()) {
                BlockPos block = iterator.next();
                block = new BlockPos((chunk.locX << 4) + block.getX(), block.getY(), (chunk.locZ << 4) + block.getZ());
                Vector blockCenter = new Vector(block.getX() + 0.5, block.getY() + 0.5, block.getZ() + 0.5);
                Vector difference = vector.clone().subtract(blockCenter);
                if (difference.lengthSquared() > rayTraceDistanceSquared || difference.dot(direction) > 0.) {
                    continue;
                }
                Iterator<BlockPos> blockIterator = new BlockIterator(blockCenter, vector);
                boolean update = true;
                while (blockIterator.hasNext()) {
                    BlockPos rayBlock = blockIterator.next();
                    ChunkPos chunkPos = new ChunkPos(rayBlock);
                    ChunkBlocks rayChunkBlocks = playerData.getChunks().get(chunkPos);
                    if (rayChunkBlocks == null) {
                        update = false;
                        break;
                    }
                    LevelChunk rayChunk = rayChunkBlocks.getChunk();
                    if (rayChunk == null) {
                        playerData.getChunks().remove(chunkPos, rayChunkBlocks);
                        update = false;
                        break;
                    }
                    int sectionY = rayBlock.getY() >> 4;
                    if (sectionY < rayChunk.getMinSection() || sectionY > rayChunk.getMaxSection() - 1) {
                        continue;
                    }
                    LevelChunkSection section = rayChunk.getSections()[sectionY - rayChunk.getMinSection()];
                    if (section == null || section.hasOnlyAir()) {
                        // Sections aren't null anymore.
                        continue;
                    }
                    BlockState blockState;
                    // section.getStates().acquire();
                    try {
                        blockState = section.getBlockState(rayBlock.getX() & 15, rayBlock.getY() & 15, rayBlock.getZ() & 15);
                    } catch (MissingPaletteEntryException e) {
                        blockState = Blocks.AIR.defaultBlockState();
                    }
                    if (solidGlobal[ChunkPacketBlockControllerAntiXray.GLOBAL_BLOCKSTATE_PALETTE.idFor(blockState)] && checkSurroundingBlocks(block.getX(), block.getY(), block.getZ(), rayBlock.getX(), rayBlock.getY(), rayBlock.getZ(), difference, section, chunkPos.x, sectionY, chunkPos.z, playerData, solidGlobal)) {
                        update = false;
                        break;
                    }
                }
                if (update) {
                    playerData.getResult().add(block);
                    iterator.remove();
                }
            }
        }
    }
}
Also used : BlockIterator(com.vanillage.raytraceantixray.util.BlockIterator) ChunkPacketBlockController(com.destroystokyo.paper.antixray.ChunkPacketBlockController) ChunkBlocks(com.vanillage.raytraceantixray.data.ChunkBlocks) ChunkPacketBlockControllerAntiXray(com.vanillage.raytraceantixray.antixray.ChunkPacketBlockControllerAntiXray) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockState(net.minecraft.world.level.block.state.BlockState) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) MissingPaletteEntryException(net.minecraft.world.level.chunk.MissingPaletteEntryException) ChunkPos(net.minecraft.world.level.ChunkPos) BlockPos(net.minecraft.core.BlockPos) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 3 with ChunkPacketBlockControllerAntiXray

use of com.vanillage.raytraceantixray.antixray.ChunkPacketBlockControllerAntiXray in project RayTraceAntiXray by stonar96.

the class UpdateBukkitRunnable method run.

@Override
public void run() {
    for (Entry<UUID, PlayerData> entry : plugin.getPlayerData().entrySet()) {
        PlayerData playerData = entry.getValue();
        Level level = ((CraftWorld) playerData.getLocations().get(0).getWorld()).getHandle();
        ChunkPacketBlockController chunkPacketBlockController = level.chunkPacketBlockController;
        if (chunkPacketBlockController instanceof ChunkPacketBlockControllerAntiXray) {
            Queue<BlockPos> result = playerData.getResult();
            for (BlockPos block = result.poll(); block != null; block = result.poll()) {
                ((ChunkPacketBlockControllerAntiXray) chunkPacketBlockController).updateBlock(level, block);
            }
        }
    }
}
Also used : ChunkPacketBlockController(com.destroystokyo.paper.antixray.ChunkPacketBlockController) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) UUID(java.util.UUID) ChunkPacketBlockControllerAntiXray(com.vanillage.raytraceantixray.antixray.ChunkPacketBlockControllerAntiXray) PlayerData(com.vanillage.raytraceantixray.data.PlayerData) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld)

Aggregations

ChunkPacketBlockControllerAntiXray (com.vanillage.raytraceantixray.antixray.ChunkPacketBlockControllerAntiXray)3 ChunkPacketBlockController (com.destroystokyo.paper.antixray.ChunkPacketBlockController)2 BlockPos (net.minecraft.core.BlockPos)2 CraftWorld (org.bukkit.craftbukkit.v1_18_R2.CraftWorld)2 ChunkBlocks (com.vanillage.raytraceantixray.data.ChunkBlocks)1 PlayerData (com.vanillage.raytraceantixray.data.PlayerData)1 BlockIterator (com.vanillage.raytraceantixray.util.BlockIterator)1 Field (java.lang.reflect.Field)1 UUID (java.util.UUID)1 ChunkPos (net.minecraft.world.level.ChunkPos)1 Level (net.minecraft.world.level.Level)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)1 LevelChunkSection (net.minecraft.world.level.chunk.LevelChunkSection)1 MissingPaletteEntryException (net.minecraft.world.level.chunk.MissingPaletteEntryException)1 Location (org.bukkit.Location)1 EventHandler (org.bukkit.event.EventHandler)1 Vector (org.bukkit.util.Vector)1 Unsafe (sun.misc.Unsafe)1