Search in sources :

Example 56 with WorldServer

use of net.minecraft.server.v1_15_R1.WorldServer in project custom-items-gradle by knokko.

the class Raytracer method raytrace.

/**
 * <p>Performs a raytrace from {@code startLocation} towards {@code startLocation + vector}.
 * The {@code vector} determines both the direction and the maximum distance of the raytrace!</p>
 *
 * <p>If an intersection with any block or entity was found, a RaytraceResult representing the intersection
 * that is closest to {@code startLocation} will be returned. If no such intersection was found, this
 * method will return null.</p>
 *
 * <p>Entities included in {@code entitiesToExclude} and dropped item entities will be ignored by
 * the raytrace.</p>
 *
 * @param startLocation The location from which the raytrace will start
 * @param vector The direction and maximum distance of the raytrace
 * @param entitiesToExclude An array of entities that will be ignored by this raytrace, may contain null
 * @return A RaytraceResult for the nearest intersection, or null if no intersection was found
 */
public static RaytraceResult raytrace(Location startLocation, Vector vector, Entity... entitiesToExclude) {
    // Important variables
    World world = startLocation.getWorld();
    Vec3D rayStart = new Vec3D(startLocation.getX(), startLocation.getY(), startLocation.getZ());
    Vec3D velocityVec = new Vec3D(vector.getX(), vector.getY(), vector.getZ());
    Vec3D rayEnd = new Vec3D(rayStart.x + velocityVec.x, rayStart.y + velocityVec.y, rayStart.z + velocityVec.z);
    CraftWorld craftWorld = (CraftWorld) world;
    WorldServer nmsWorld = craftWorld.getHandle();
    // Start with infinity to make sure that any other distance will be shorter
    double nearestDistanceSq = Double.POSITIVE_INFINITY;
    Vec3D intersectionPos = null;
    // The block raytrace
    MovingObjectPosition rayResult = nmsWorld.rayTrace(rayStart, rayEnd, true, true, false);
    if (rayResult != null && rayResult.type == EnumMovingObjectType.BLOCK) {
        double blockDistanceSq = rayResult.pos.distanceSquared(rayStart);
        if (blockDistanceSq < vector.lengthSquared()) {
            intersectionPos = rayResult.pos;
            nearestDistanceSq = blockDistanceSq;
        }
    }
    // The entity raytrace
    AxisAlignedBB movementBB = new AxisAlignedBB(rayStart.x, rayStart.y, rayStart.z, rayEnd.x, rayEnd.y, rayEnd.z);
    List<net.minecraft.server.v1_12_R1.Entity> nmsEntityList = nmsWorld.getEntities(null, movementBB);
    net.minecraft.server.v1_12_R1.Entity intersectedEntity = null;
    entityListLoop: for (net.minecraft.server.v1_12_R1.Entity nmsEntity : nmsEntityList) {
        // It's currently convenient to ignore dropped items
        if (nmsEntity instanceof EntityItem)
            continue entityListLoop;
        // Since the entities in entitiesToExclude could be null, it's important to call equals() on craftEntity
        CraftEntity craftEntity = nmsEntity.getBukkitEntity();
        for (Entity exclude : entitiesToExclude) if (craftEntity.equals(exclude))
            continue entityListLoop;
        // Check if we intersect this entity and check if the distance to it is smaller than the nearest distance so far
        MovingObjectPosition entityIntersection = nmsEntity.getBoundingBox().b(rayStart, rayEnd);
        if (entityIntersection != null) {
            double distanceSq = rayStart.distanceSquared(entityIntersection.pos);
            if (distanceSq < nearestDistanceSq) {
                nearestDistanceSq = distanceSq;
                intersectedEntity = nmsEntity;
                intersectionPos = entityIntersection.pos;
            }
        }
    }
    // Determining the final result
    if (nearestDistanceSq < Double.POSITIVE_INFINITY) {
        Location hitLocation = new Location(world, intersectionPos.x, intersectionPos.y, intersectionPos.z);
        if (intersectedEntity != null) {
            return RaytraceResult.hitEntity(intersectedEntity.getBukkitEntity(), hitLocation);
        } else {
            return RaytraceResult.hitBlock(hitLocation);
        }
    } else {
        return null;
    }
}
Also used : AxisAlignedBB(net.minecraft.server.v1_12_R1.AxisAlignedBB) Entity(org.bukkit.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity) CraftEntity(org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity) WorldServer(net.minecraft.server.v1_12_R1.WorldServer) CraftWorld(org.bukkit.craftbukkit.v1_12_R1.CraftWorld) World(org.bukkit.World) Vec3D(net.minecraft.server.v1_12_R1.Vec3D) MovingObjectPosition(net.minecraft.server.v1_12_R1.MovingObjectPosition) CraftWorld(org.bukkit.craftbukkit.v1_12_R1.CraftWorld) EntityItem(net.minecraft.server.v1_12_R1.EntityItem) Location(org.bukkit.Location)

Example 57 with WorldServer

use of net.minecraft.server.v1_15_R1.WorldServer in project TheAPI by TheDevTec.

the class v1_10_R1 method packetRespawn.

@Override
public Object packetRespawn(Player player) {
    EntityPlayer entityPlayer = (EntityPlayer) getPlayer(player);
    WorldServer worldserver = entityPlayer.x();
    byte actualDimension = (byte) worldserver.getWorld().getEnvironment().getId();
    return new PacketPlayOutRespawn((byte) ((actualDimension >= 0) ? -1 : 0), worldserver.getDifficulty(), worldserver.getWorldData().getType(), entityPlayer.playerInteractManager.getGameMode());
}
Also used : EntityPlayer(net.minecraft.server.v1_10_R1.EntityPlayer) WorldServer(net.minecraft.server.v1_10_R1.WorldServer) PacketPlayOutRespawn(net.minecraft.server.v1_10_R1.PacketPlayOutRespawn)

Example 58 with WorldServer

use of net.minecraft.server.v1_15_R1.WorldServer in project TheAPI by TheDevTec.

the class v1_10_R1 method getChunk.

@Override
public Object getChunk(World world, int x, int z) {
    WorldServer sworld = ((CraftWorld) world).getHandle();
    net.minecraft.server.v1_10_R1.Chunk loaded = ((ChunkProviderServer) sworld.getChunkProvider()).getChunkIfLoaded(x, z);
    if (loaded == null) {
        // load
        try {
            net.minecraft.server.v1_10_R1.Chunk chunk;
            chunk = ((IChunkLoader) Ref.get(((ChunkProviderServer) sworld.getChunkProvider()), chunkLoader)).a(sworld, x, z);
            if (chunk != null) {
                chunk.setLastSaved(sworld.getTime());
                if (((ChunkProviderServer) sworld.getChunkProvider()).chunkGenerator != null) {
                    ((ChunkProviderServer) sworld.getChunkProvider()).chunkGenerator.recreateStructures(chunk, x, z);
                }
            }
            if (chunk != null) {
                ((ChunkProviderServer) sworld.getChunkProvider()).chunks.put(ChunkCoordIntPair.a(x, z), chunk);
                postToMainThread(() -> {
                    chunk.addEntities();
                });
                loaded = chunk;
            }
        } catch (Exception e) {
        }
    }
    if (loaded == null) {
        // generate new chunk
        ChunkRegionLoader loader = null;
        if ((IChunkLoader) Ref.get(((ChunkProviderServer) sworld.getChunkProvider()), chunkLoader) instanceof ChunkRegionLoader) {
            loader = (ChunkRegionLoader) Ref.get(((ChunkProviderServer) sworld.getChunkProvider()), chunkLoader);
        }
        if (loader != null && loader.chunkExists(sworld, x, z)) {
            loaded = ChunkIOExecutor.syncChunkLoad(sworld, loader, (ChunkProviderServer) sworld.getChunkProvider(), x, z);
        } else {
            loaded = ((ChunkProviderServer) sworld.getChunkProvider()).originalGetChunkAt(x, z);
        }
        loaded = ((ChunkProviderServer) sworld.getChunkProvider()).chunkGenerator.getOrCreateChunk(x, z);
        ((ChunkProviderServer) sworld.getChunkProvider()).chunks.put(ChunkCoordIntPair.a(x, z), loaded);
    }
    return loaded;
}
Also used : ChunkProviderServer(net.minecraft.server.v1_10_R1.ChunkProviderServer) WorldServer(net.minecraft.server.v1_10_R1.WorldServer) ChunkRegionLoader(net.minecraft.server.v1_10_R1.ChunkRegionLoader) CraftWorld(org.bukkit.craftbukkit.v1_10_R1.CraftWorld)

Example 59 with WorldServer

use of net.minecraft.server.v1_15_R1.WorldServer in project TheAPI by TheDevTec.

the class v1_13_R1 method packetRespawn.

@Override
public Object packetRespawn(Player player) {
    EntityPlayer entityPlayer = (EntityPlayer) getPlayer(player);
    WorldServer worldserver = entityPlayer.getWorldServer();
    byte actualDimension = (byte) worldserver.getWorld().getEnvironment().getId();
    return new PacketPlayOutRespawn((byte) ((actualDimension >= 0) ? -1 : 0), worldserver.getDifficulty(), worldserver.getWorldData().getType(), entityPlayer.playerInteractManager.getGameMode());
}
Also used : EntityPlayer(net.minecraft.server.v1_13_R1.EntityPlayer) WorldServer(net.minecraft.server.v1_13_R1.WorldServer) PacketPlayOutRespawn(net.minecraft.server.v1_13_R1.PacketPlayOutRespawn)

Example 60 with WorldServer

use of net.minecraft.server.v1_15_R1.WorldServer in project TheAPI by TheDevTec.

the class v1_11_R1 method getChunk.

@Override
public Object getChunk(World world, int x, int z) {
    WorldServer sworld = ((CraftWorld) world).getHandle();
    net.minecraft.server.v1_11_R1.Chunk loaded = ((ChunkProviderServer) sworld.getChunkProvider()).getChunkIfLoaded(x, z);
    if (loaded == null) {
        // load
        try {
            net.minecraft.server.v1_11_R1.Chunk chunk;
            chunk = ((IChunkLoader) Ref.get(((ChunkProviderServer) sworld.getChunkProvider()), chunkLoader)).a(sworld, x, z);
            if (chunk != null) {
                chunk.setLastSaved(sworld.getTime());
                if (((ChunkProviderServer) sworld.getChunkProvider()).chunkGenerator != null) {
                    ((ChunkProviderServer) sworld.getChunkProvider()).chunkGenerator.recreateStructures(chunk, x, z);
                }
            }
            if (chunk != null) {
                ((ChunkProviderServer) sworld.getChunkProvider()).chunks.put(ChunkCoordIntPair.a(x, z), chunk);
                postToMainThread(() -> {
                    chunk.addEntities();
                });
                loaded = chunk;
            }
        } catch (Exception e) {
        }
    }
    if (loaded == null) {
        // generate new chunk
        ChunkRegionLoader loader = null;
        if ((IChunkLoader) Ref.get(((ChunkProviderServer) sworld.getChunkProvider()), chunkLoader) instanceof ChunkRegionLoader) {
            loader = (ChunkRegionLoader) Ref.get(((ChunkProviderServer) sworld.getChunkProvider()), chunkLoader);
        }
        if (loader != null && loader.chunkExists(sworld, x, z)) {
            loaded = ChunkIOExecutor.syncChunkLoad(sworld, loader, (ChunkProviderServer) sworld.getChunkProvider(), x, z);
        } else {
            loaded = ((ChunkProviderServer) sworld.getChunkProvider()).originalGetChunkAt(x, z);
        }
        loaded = ((ChunkProviderServer) sworld.getChunkProvider()).chunkGenerator.getOrCreateChunk(x, z);
        ((ChunkProviderServer) sworld.getChunkProvider()).chunks.put(ChunkCoordIntPair.a(x, z), loaded);
    }
    return loaded;
}
Also used : ChunkProviderServer(net.minecraft.server.v1_11_R1.ChunkProviderServer) WorldServer(net.minecraft.server.v1_11_R1.WorldServer) ChunkRegionLoader(net.minecraft.server.v1_11_R1.ChunkRegionLoader) CraftWorld(org.bukkit.craftbukkit.v1_11_R1.CraftWorld)

Aggregations

WorldServer (net.minecraft.server.v1_16_R3.WorldServer)24 WorldServer (net.minecraft.server.v1_12_R1.WorldServer)20 CraftWorld (org.bukkit.craftbukkit.v1_16_R3.CraftWorld)19 ArrayList (java.util.ArrayList)18 List (java.util.List)16 UUID (java.util.UUID)16 CraftWorld (org.bukkit.craftbukkit.v1_12_R1.CraftWorld)16 GameProfile (com.mojang.authlib.GameProfile)15 WorldServer (net.minecraft.server.v1_11_R1.WorldServer)15 Material (org.bukkit.Material)15 WorldServer (net.minecraft.server.v1_14_R1.WorldServer)14 WorldServer (net.minecraft.server.v1_15_R1.WorldServer)14 WorldServer (net.minecraft.server.v1_10_R1.WorldServer)13 Block (org.bukkit.block.Block)13 CraftWorld (org.bukkit.craftbukkit.v1_11_R1.CraftWorld)13 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)12 WorldServer (net.minecraft.server.v1_8_R3.WorldServer)12 LivingEntity (org.bukkit.entity.LivingEntity)12 CraftWorld (org.bukkit.craftbukkit.v1_10_R1.CraftWorld)11 CraftWorld (org.bukkit.craftbukkit.v1_15_R1.CraftWorld)11