Search in sources :

Example 11 with INMSHandler

use of thpmc.vanilla_source.api.nms.INMSHandler in project VanillaSource by TheHollowPlanetMC.

the class ImplParallelChunk method sendUpdate.

@Override
public void sendUpdate(Player player) {
    INMSHandler nmsHandler = NMSManager.getNMSHandler();
    nmsHandler.sendChunkMultiBlockChangeUpdatePacket(player, this);
    TaskHandler.runSync(() -> {
        World world = Bukkit.getWorld(getWorld().getName());
        if (world == null)
            return;
        TaskHandler.runWorldSync(world, () -> {
            Object lightUpdatePacket = nmsHandler.createLightUpdatePacketAtPrimaryThread(this);
            if (lightUpdatePacket != null)
                nmsHandler.sendPacket(player, lightUpdatePacket);
        });
    });
}
Also used : ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) World(org.bukkit.World) INMSHandler(thpmc.vanilla_source.api.nms.INMSHandler)

Example 12 with INMSHandler

use of thpmc.vanilla_source.api.nms.INMSHandler in project VanillaSource by TheHollowPlanetMC.

the class EngineWorld method rayTraceBlocksForShortRange.

@Nullable
default EngineRayTraceResult rayTraceBlocksForShortRange(@NotNull Vector startPosition, @NotNull Vector direction, double distance, @NotNull CollideOption collideOption) {
    Vector movement = direction.clone().normalize().multiply(distance);
    Vector endPosition = startPosition.clone().add(movement);
    // collect block collisions
    EngineBoundingBox region = new EngineBoundingBox(startPosition.getX(), startPosition.getY(), startPosition.getZ(), endPosition.getX(), endPosition.getY(), endPosition.getZ()).expandForMovement(movement);
    region.expand(collideOption.getBoundingBoxGrow());
    region.expand(16);
    Set<EngineBoundingBox> boxList = new HashSet<>();
    // get block collisions
    int startX = NumberConversions.floor(region.getMinX());
    int startY = NumberConversions.floor(region.getMinY());
    int startZ = NumberConversions.floor(region.getMinZ());
    int endX = NumberConversions.floor(region.getMaxX());
    int endY = NumberConversions.floor(region.getMaxY());
    int endZ = NumberConversions.floor(region.getMaxZ());
    INMSHandler nmsHandler = VanillaSourceAPI.getInstance().getNMSHandler();
    EngineChunk chunk = null;
    for (int x = startX; x < endX; x++) {
        for (int y = startY; y < endY; y++) {
            for (int z = startZ; z < endZ; z++) {
                int chunkX = x >> 4;
                int chunkZ = z >> 4;
                // get chunk cache
                if (chunk == null) {
                    chunk = this.getChunkAt(chunkX, chunkZ);
                } else if (chunk.getChunkX() != chunkX || chunk.getChunkZ() != chunkZ) {
                    chunk = this.getChunkAt(chunkX, chunkZ);
                }
                if (chunk == null) {
                    boxList.add(EngineBoundingBox.getBoundingBoxForUnloadChunk(chunkX, chunkZ));
                    continue;
                }
                // get nms block from cache
                Object iBlockData = chunk.getNMSBlockData(x, y, z);
                if (iBlockData == null) {
                    continue;
                }
                EngineBlock engineBlock = new EngineBlock(this, chunk, x, y, z, iBlockData);
                // collect block collisions
                nmsHandler.collectBlockCollisions(engineBlock, boxList, collideOption);
            }
        }
    }
    // perform raytrace
    return rayTraceForCollisionList(startPosition, direction, distance, boxList, collideOption);
}
Also used : EngineBlock(thpmc.vanilla_source.api.world.block.EngineBlock) Vector(org.bukkit.util.Vector) EngineBoundingBox(thpmc.vanilla_source.api.util.collision.EngineBoundingBox) INMSHandler(thpmc.vanilla_source.api.nms.INMSHandler) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with INMSHandler

use of thpmc.vanilla_source.api.nms.INMSHandler in project VanillaSource by TheHollowPlanetMC.

the class TestListener method onPlayerClick.

@EventHandler
public void onPlayerClick(PlayerAnimationEvent event) {
    Player player = event.getPlayer();
    if (!player.isSneaking())
        return;
    /*
        Location loc = player.getLocation();
        NativeBridge.test2(player.getWorld().getName().toCharArray(), loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ());
        player.sendMessage("click!");*/
    /*
        List<EngineBoundingBox> list = new ArrayList<>();
        EngineWorld world = AsyncWorldCache.getAsyncWorld(player.getWorld().getName());
        
        Location loc = player.getLocation();
        
        INMSHandler nmsHandler = THPEngineAPI.getInstance().getNMSHandler();
        
        for(int x = loc.getBlockX() - 10; x < loc.getBlockX() + 10; x++){
            for(int y = loc.getBlockY() - 10; y < loc.getBlockY() + 10; y++){
                for(int z = loc.getBlockZ() - 10; z < loc.getBlockZ() + 10; z++){
                    EngineChunk chunk = world.getChunkAt(x >> 4, z >> 4);
                    Object iBlockData = chunk.getNMSBlockData(x, y, z);
                    if(iBlockData == null) continue;
                    
                    EngineBlock block = new EngineBlock(world, chunk, x, y, z, iBlockData);
                    nmsHandler.collectBlockCollisions(block, list, new CollideOption(FluidCollisionMode.ALWAYS, true));
                }
            }
        }
        
        for(EngineBoundingBox boundingBox : list){
            player.spawnParticle(Particle.FLAME, boundingBox.getMinX(), boundingBox.getMinY(), boundingBox.getMinZ(), 0);
            player.spawnParticle(Particle.FLAME, boundingBox.getMinX(), boundingBox.getMaxY(), boundingBox.getMinZ(), 0);
            player.spawnParticle(Particle.FLAME, boundingBox.getMinX(), boundingBox.getMinY(), boundingBox.getMaxZ(), 0);
            player.spawnParticle(Particle.FLAME, boundingBox.getMinX(), boundingBox.getMaxY(), boundingBox.getMaxZ(), 0);
            player.spawnParticle(Particle.FLAME, boundingBox.getMaxX(), boundingBox.getMinY(), boundingBox.getMinZ(), 0);
            player.spawnParticle(Particle.FLAME, boundingBox.getMaxX(), boundingBox.getMaxY(), boundingBox.getMinZ(), 0);
            player.spawnParticle(Particle.FLAME, boundingBox.getMaxX(), boundingBox.getMinY(), boundingBox.getMaxZ(), 0);
            player.spawnParticle(Particle.FLAME, boundingBox.getMaxX(), boundingBox.getMaxY(), boundingBox.getMaxZ(), 0);
        }*/
    /*
        TaskHandler.runAsync(() -> {
            EngineWorld world = AsyncWorldCache.getAsyncWorld(player.getWorld().getName());
            CollideOption collideOption = new CollideOption(FluidCollisionMode.ALWAYS, false);
            collideOption.setCollideBlockFunction(engineBlock -> {
                return engineBlock.getMaterial() != Material.OAK_LEAVES;
            });
            collideOption.setBoundingBoxGrow(0.2);
            EngineRayTraceResult rayTraceResult = world.rayTraceEntities(player.getEyeLocation().toVector(), player.getEyeLocation().getDirection(), 20, collideOption);
            if(rayTraceResult == null){
                player.sendMessage("NOT HIT!");
            }else{
                Vector hitPosition = rayTraceResult.getHitPosition();
                BlockFace hitFace = rayTraceResult.getHitFace();
        
                player.spawnParticle(Particle.FLAME, hitPosition.getX(), hitPosition.getY(), hitPosition.getZ(), 0);
                player.sendMessage("Hit face : " + hitFace);
            }
        });*/
    Location location = player.getLocation();
    INMSHandler nmsHandler = VanillaSourceAPI.getInstance().getNMSHandler();
    for (int index = 0; index < 100; index++) {
        GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "NPC");
        NMSEntityController entityPlayer = nmsHandler.createNMSEntityController(location.getWorld(), location.getX(), location.getY(), location.getZ(), EntityType.PLAYER, gameProfile);
        entityPlayer.setPositionRaw(location.getX(), location.getY(), location.getZ());
        VanillaSourceAPI.getInstance().getTickRunnerPool().spawn(tickRunner -> {
            EngineEntity npc = new EngineEntity(tickRunner.getThreadLocalCache().getWorld(location.getWorld().getName()), entityPlayer, tickRunner);
            npc.getAIController().goalSelector.registerGoal(0, new EntityFollowGoal(player));
            npc.setAutoClimbHeight(1.0F);
            tickRunner.addEntity(npc);
        });
        count++;
        player.sendMessage("PlayerNPC -> " + count);
    }
/*
        World world = player.getWorld();
        BlockPosition start = new BlockPosition(173, 80, -23);
        BlockPosition goal = new BlockPosition(172, 80, 4);
    
        ThreadLocalCache cache = new ThreadLocalCache();
        ThreadLocalEngineWorld engineWorld = cache.getWorld(world.getName());
        
        CollideOption collideOption = new CollideOption(FluidCollisionMode.NEVER, true);
        
        AsyncAStarMachine astar;
        long s;
        long e;
        astar = new AsyncAStarMachine(engineWorld, start, goal, 1, 1, 50, false, collideOption);
        s = System.nanoTime();
        astar.runPathFinding();
        e = System.nanoTime();
        System.out.println("[Java] iteration = 50,  Time = " + (e - s) + "ns");
        s = System.nanoTime();
        astar.runPathfindingNative();
        e = System.nanoTime();
        System.out.println("[Rust] iteration = 50,  Time = " + (e - s) + "ns");
    
        astar = new AsyncAStarMachine(engineWorld, start, goal, 1, 1, 100, false, collideOption);
        s = System.nanoTime();
        astar.runPathFinding();
        e = System.nanoTime();
        System.out.println("[Java] iteration = 100,  Time = " + (e - s) + "ns");
        s = System.nanoTime();
        astar.runPathfindingNative();
        e = System.nanoTime();
        System.out.println("[Rust] iteration = 100,  Time = " + (e - s) + "ns");
    
        astar = new AsyncAStarMachine(engineWorld, start, goal, 1, 1, 500, false, collideOption);
        s = System.nanoTime();
        astar.runPathFinding();
        e = System.nanoTime();
        System.out.println("[Java] iteration = 500,  Time = " + (e - s) + "ns");
        s = System.nanoTime();
        astar.runPathfindingNative();
        e = System.nanoTime();
        System.out.println("[Rust] iteration = 500,  Time = " + (e - s) + "ns");
    
        astar = new AsyncAStarMachine(engineWorld, start, goal, 1, 1, 5000, false, collideOption);
        s = System.nanoTime();
        astar.runPathFinding();
        e = System.nanoTime();
        System.out.println("[Java] iteration = 5000,  Time = " + (e - s) + "ns");
        s = System.nanoTime();
        astar.runPathfindingNative();
        e = System.nanoTime();
        System.out.println("[Rust] iteration = 5000,  Time = " + (e - s) + "ns");*/
}
Also used : EntityFollowGoal(thpmc.vanilla_source.api.entity.ai.navigation.goal.EntityFollowGoal) Player(org.bukkit.entity.Player) GameProfile(com.mojang.authlib.GameProfile) EngineEntity(thpmc.vanilla_source.api.entity.EngineEntity) NMSEntityController(thpmc.vanilla_source.api.nms.entity.NMSEntityController) INMSHandler(thpmc.vanilla_source.api.nms.INMSHandler) EventHandler(org.bukkit.event.EventHandler)

Aggregations

INMSHandler (thpmc.vanilla_source.api.nms.INMSHandler)13 EngineBlock (thpmc.vanilla_source.api.world.block.EngineBlock)5 EngineChunk (thpmc.vanilla_source.api.world.cache.EngineChunk)4 Vector (org.bukkit.util.Vector)3 GameProfile (com.mojang.authlib.GameProfile)2 HashSet (java.util.HashSet)2 World (org.bukkit.World)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 NMSEntityController (thpmc.vanilla_source.api.nms.entity.NMSEntityController)2 ParallelWorld (thpmc.vanilla_source.api.world.parallel.ParallelWorld)2 ArrayList (java.util.ArrayList)1 Location (org.bukkit.Location)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 EngineEntity (thpmc.vanilla_source.api.entity.EngineEntity)1 EntityFollowGoal (thpmc.vanilla_source.api.entity.ai.navigation.goal.EntityFollowGoal)1 BlockPosition (thpmc.vanilla_source.api.entity.ai.pathfinding.BlockPosition)1 CollideOption (thpmc.vanilla_source.api.util.collision.CollideOption)1 EngineBoundingBox (thpmc.vanilla_source.api.util.collision.EngineBoundingBox)1