Search in sources :

Example 1 with MovingObjectPosition

use of net.minecraft.server.v1_8_R3.MovingObjectPosition 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 2 with MovingObjectPosition

use of net.minecraft.server.v1_8_R3.MovingObjectPosition in project Warlords by ebicep.

the class AbstractPiercingProjectileBase method checkCollisionAndMove.

@Nullable
protected MovingObjectPosition checkCollisionAndMove(InternalProjectile projectile, Location currentLocation, Vector speed, WarlordsPlayer shooter) {
    Vec3D before = new Vec3D(currentLocation.getX(), currentLocation.getY(), currentLocation.getZ());
    currentLocation.add(speed);
    Vec3D after = new Vec3D(currentLocation.getX(), currentLocation.getY(), currentLocation.getZ());
    int radius = 3;
    /* TODO */
    PlayerFilter.entitiesInRectangle(currentLocation.getWorld(), Math.min(before.a - radius, after.a - radius), Math.min(before.b - radius, after.b - radius), Math.min(before.c - radius, after.c - radius), Math.max(before.a + radius, after.a + radius), Math.max(before.b + radius, after.b + radius), Math.max(before.c + radius, after.c + radius)).enemiesOf(shooter).filter(e -> true);
    @Nullable MovingObjectPosition hit = null;
    double hitDistance = 0;
    for (Entity entity : currentLocation.getWorld().getEntities()) {
        WarlordsPlayer wp = getFromEntity(entity);
        if (wp != null && (hitTeammates || shooter.isEnemyAlive(wp)) && wp.isAlive() && wp != shooter) {
            // This logic does not properly deal with an EnderDragon entity, as it has a complex hitbox
            assert entity instanceof CraftEntity;
            net.minecraft.server.v1_8_R3.Entity nmsEntity = ((CraftEntity) entity).getHandle();
            AxisAlignedBB aabb = nmsEntity.getBoundingBox();
            // Increase the size of the boundingbox so entities are easier
            // to hit. This is needed because people see their projectiles
            // as something big, instead of a tiny point
            aabb = new AxisAlignedBB(aabb.a - playerHitbox, aabb.b - playerHitbox, aabb.c - playerHitbox, aabb.d + playerHitbox, aabb.e + playerHitbox, aabb.f + playerHitbox);
            MovingObjectPosition mop = aabb.a(after, before);
            if (mop != null) {
                mop.entity = nmsEntity;
                double distance = before.distanceSquared(mop.pos);
                if (shouldEndProjectileOnHit(projectile, wp)) {
                    if (hit == null || distance < hitDistance) {
                        hitDistance = distance;
                        hit = mop;
                    }
                } else {
                    PENDING_HITS.add(new PendingHit(new Location(currentLocation.getWorld(), mop.pos.a, mop.pos.b, mop.pos.c), distance, wp));
                }
            }
        }
    }
    BlockIterator itr = new BlockIterator(currentLocation.getWorld(), new Vector(before.a, before.b, before.c), speed, 0, (int) (projectileSpeed + 1));
    while (itr.hasNext()) {
        Block block = itr.next();
        if (block.getType().isSolid() && block.getType() != Material.BARRIER && block.getType() != Material.STANDING_BANNER) {
            BlockPosition pos = new BlockPosition(block.getX(), block.getY(), block.getZ());
            WorldServer world = ((CraftWorld) block.getWorld()).getHandle();
            IBlockData type = world.getType(pos);
            AxisAlignedBB box = type.getBlock().a(world, pos, type);
            MovingObjectPosition mop = box.a(after, before);
            // Flags have no hitbox while they are considered solid??
            if (mop != null) {
                double distance = before.distanceSquared(mop.pos);
                if (shouldEndProjectileOnHit(projectile, block)) {
                    if ((hit == null || distance < hitDistance)) {
                        hitDistance = distance;
                        hit = mop;
                    }
                    // checked in order so we can bail out early
                    break;
                }
            }
        }
    }
    if (hit != null) {
        currentLocation.setX(hit.pos.a);
        currentLocation.setY(hit.pos.b);
        currentLocation.setZ(hit.pos.c);
    }
    if (!PENDING_HITS.isEmpty()) {
        Collections.sort(PENDING_HITS);
        for (PendingHit p : PENDING_HITS) {
            if (hit == null || hitDistance < p.distance) {
                this.onNonCancellingHit(projectile, p.hit, p.loc);
            } else {
                break;
            }
        }
        PENDING_HITS.clear();
    }
    return hit;
}
Also used : Entity(org.bukkit.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity) BlockIterator(org.bukkit.util.BlockIterator) WarlordsPlayer(com.ebicep.warlords.player.WarlordsPlayer) CraftEntity(org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity) Block(org.bukkit.block.Block) Vector(org.bukkit.util.Vector) CraftWorld(org.bukkit.craftbukkit.v1_8_R3.CraftWorld) Nullable(javax.annotation.Nullable) net.minecraft.server.v1_8_R3(net.minecraft.server.v1_8_R3) Location(org.bukkit.Location) Nullable(javax.annotation.Nullable)

Example 3 with MovingObjectPosition

use of net.minecraft.server.v1_8_R3.MovingObjectPosition in project Warlords by ebicep.

the class FlagSpawnPointOption method register.

@Override
public void register(Game game) {
    this.game = game;
    // We register a gamemarker to prevent any captures for our own team if we lost our flag
    game.registerGameMarker(FlagCaptureInhibitMarker.class, pFlag -> {
        return !(info.getFlag() instanceof SpawnFlagLocation) && info.getTeam() == pFlag.getPlayer().getTeam();
    });
    game.registerGameMarker(DebugLocationMarker.class, DebugLocationMarker.create(Material.BANNER, 0, this.getClass(), "Flag spawn: " + info.getTeam(), this.info.getSpawnLocation()));
    game.registerGameMarker(DebugLocationMarker.class, DebugLocationMarker.create(Material.BANNER, 15, this.getClass(), "Flag: " + info.getTeam(), () -> info.getFlag().getLocation(), () -> info.getFlag().getDebugInformation()));
    FlagHolder holder = FlagHolder.create(() -> info);
    game.registerGameMarker(FlagHolder.class, holder);
    if (this.registerCompassMarker) {
        game.registerGameMarker(CompassTargetMarker.class, holder);
    }
    game.registerGameMarker(ScoreboardHandler.class, scoreboard = new SimpleScoreboardHandler(info.getTeam() == Team.RED ? 20 : 21, "flag") {

        @Override
        public List<String> computeLines(@Nullable WarlordsPlayer player) {
            String flagName = info.getTeam().coloredPrefix();
            FlagLocation flag = info.getFlag();
            if (flag instanceof SpawnFlagLocation) {
                return singletonList(flagName + " Flag: " + ChatColor.GREEN + "Safe");
            } else if (flag instanceof PlayerFlagLocation) {
                PlayerFlagLocation pFlag = (PlayerFlagLocation) flag;
                String extra = pFlag.getPickUpTicks() == 0 ? "" : ChatColor.YELLOW + " +" + pFlag.getComputedHumanMultiplier() + "§e%";
                return singletonList(flagName + " Flag: " + ChatColor.RED + "Stolen!" + extra);
            } else if (flag instanceof GroundFlagLocation) {
                GroundFlagLocation gFlag = (GroundFlagLocation) flag;
                return singletonList(flagName + " Flag: " + ChatColor.YELLOW + "Dropped! " + ChatColor.GRAY + gFlag.getDespawnTimerSeconds());
            } else {
                return singletonList(flagName + " Flag: " + ChatColor.GRAY + "Respawning...");
            }
        }
    });
    game.registerEvents(new Listener() {

        @EventHandler(priority = EventPriority.LOW)
        public void onArmorStandBreak(EntityDamageByEntityEvent event) {
            boolean isOurArmorStand = renderer.getRenderedArmorStands().contains(event.getEntity());
            WarlordsPlayer wp = Warlords.getPlayer(event.getDamager());
            if (wp != null && wp.getGame() == game && isOurArmorStand) {
                onFlagInteract(wp);
                event.setCancelled(true);
            }
        }

        @EventHandler(priority = EventPriority.LOW)
        public void onPotentialFlagInteract(PlayerInteractEntityEvent event) {
            onPotentialFlagInteract((PlayerEvent) event);
        }

        @EventHandler(priority = EventPriority.LOW)
        public void onPotentialFlagInteract(PlayerInteractEvent event) {
            onPotentialFlagInteract((PlayerEvent) event);
        }

        private void onPotentialFlagInteract(PlayerEvent event) {
            WarlordsPlayer wp = Warlords.getPlayer(event.getPlayer());
            if (wp != null && wp.getGame() == game) {
                Location playerLocation = wp.getEntity().getEyeLocation();
                Vector direction = wp.getEntity().getLocation().getDirection().multiply(3);
                Vec3D from = new Vec3D(playerLocation.getX(), playerLocation.getY(), playerLocation.getZ());
                Vec3D to = new Vec3D(playerLocation.getX() + direction.getX(), playerLocation.getY() + direction.getY(), playerLocation.getZ() + direction.getZ());
                checkFlagInteract(playerLocation, wp, from, to, renderer);
            }
        }

        private void checkFlagInteract(Location playerLocation, WarlordsPlayer wp, Vec3D from, Vec3D to, FlagRenderer render) {
            Location entityLoc = new Location(playerLocation.getWorld(), 0, 0, 0);
            for (Entity stand : render.getRenderedArmorStands()) {
                stand.getLocation(entityLoc);
                if (entityLoc.getWorld() == playerLocation.getWorld() && entityLoc.distanceSquared(playerLocation) < 5 * 5) {
                    AxisAlignedBB aabb = new AxisAlignedBB(entityLoc.getX() - 0.5, entityLoc.getY(), entityLoc.getZ() - 0.5, entityLoc.getX() + 0.5, entityLoc.getY() + 2, entityLoc.getZ() + 0.5);
                    MovingObjectPosition mop = aabb.a(from, to);
                    if (mop != null) {
                        onFlagInteract(wp);
                        break;
                    }
                }
            }
        }

        private void onFlagInteract(WarlordsPlayer wp) {
            Team team = wp.getTeam();
            if (wp.isDeath()) {
                return;
            }
            if (renderer.getLastFlagState() != info.getFlag()) {
                // Prevent the player from interacting when the render state is outdated
                return;
            }
            wp.setFlagCooldown(2);
            if (info.getFlag() instanceof GroundFlagLocation) {
                GroundFlagLocation groundFlagLocation = (GroundFlagLocation) info.getFlag();
                if (team == info.getTeam()) {
                    // Return flag
                    info.setFlag(new SpawnFlagLocation(info.getSpawnLocation(), wp));
                } else {
                    // Steal flag
                    info.setFlag(new PlayerFlagLocation(wp, groundFlagLocation.getDamageTimer()));
                    if (wp.getEntity().getVehicle() != null) {
                        wp.getEntity().getVehicle().remove();
                    }
                }
            } else if (info.getFlag() instanceof SpawnFlagLocation) {
                if (team == info.getTeam()) {
                    // Nothing
                    wp.sendMessage("§cYou can't steal your own team's flag!");
                } else {
                    // Steal flag
                    info.setFlag(new PlayerFlagLocation(wp, 0));
                    wp.getCooldownManager().addCooldown(new RegularCooldown<FlagSpawnPointOption>("Flag Damage Resistance", "RES", FlagSpawnPointOption.class, null, wp, CooldownTypes.BUFF, cooldownManager -> {
                    }, 15 * 20) {

                        @Override
                        public float modifyDamageAfterInterveneFromSelf(WarlordsDamageHealingEvent event, float currentDamageValue) {
                            return currentDamageValue * .9f;
                        }
                    });
                }
            }
        }
    });
}
Also used : AxisAlignedBB(net.minecraft.server.v1_8_R3.AxisAlignedBB) MovingObjectPosition(net.minecraft.server.v1_8_R3.MovingObjectPosition) SimpleScoreboardHandler(com.ebicep.warlords.game.option.marker.scoreboard.SimpleScoreboardHandler) Collections.singletonList(java.util.Collections.singletonList) EventHandler(org.bukkit.event.EventHandler) Location(org.bukkit.Location) ScoreboardHandler(com.ebicep.warlords.game.option.marker.scoreboard.ScoreboardHandler) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Nonnull(javax.annotation.Nonnull) Vec3D(net.minecraft.server.v1_8_R3.Vec3D) Material(org.bukkit.Material) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Nullable(javax.annotation.Nullable) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) GameRunnable(com.ebicep.warlords.util.warlords.GameRunnable) Entity(org.bukkit.entity.Entity) Team(com.ebicep.warlords.game.Team) com.ebicep.warlords.game.flags(com.ebicep.warlords.game.flags) Warlords(com.ebicep.warlords.Warlords) CooldownTypes(com.ebicep.warlords.player.cooldowns.CooldownTypes) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent) Vector(org.bukkit.util.Vector) List(java.util.List) Game(com.ebicep.warlords.game.Game) RegularCooldown(com.ebicep.warlords.player.cooldowns.cooldowns.RegularCooldown) EventPriority(org.bukkit.event.EventPriority) WarlordsPlayer(com.ebicep.warlords.player.WarlordsPlayer) PlayerEvent(org.bukkit.event.player.PlayerEvent) WarlordsFlagUpdatedEvent(com.ebicep.warlords.events.WarlordsFlagUpdatedEvent) ChatColor(org.bukkit.ChatColor) AxisAlignedBB(net.minecraft.server.v1_8_R3.AxisAlignedBB) WarlordsDamageHealingEvent(com.ebicep.warlords.events.WarlordsDamageHealingEvent) com.ebicep.warlords.game.option.marker(com.ebicep.warlords.game.option.marker) Entity(org.bukkit.entity.Entity) Listener(org.bukkit.event.Listener) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) EventHandler(org.bukkit.event.EventHandler) Vec3D(net.minecraft.server.v1_8_R3.Vec3D) Team(com.ebicep.warlords.game.Team) WarlordsDamageHealingEvent(com.ebicep.warlords.events.WarlordsDamageHealingEvent) Vector(org.bukkit.util.Vector) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent) WarlordsPlayer(com.ebicep.warlords.player.WarlordsPlayer) PlayerEvent(org.bukkit.event.player.PlayerEvent) MovingObjectPosition(net.minecraft.server.v1_8_R3.MovingObjectPosition) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) SimpleScoreboardHandler(com.ebicep.warlords.game.option.marker.scoreboard.SimpleScoreboardHandler) Nullable(javax.annotation.Nullable) Location(org.bukkit.Location)

Example 4 with MovingObjectPosition

use of net.minecraft.server.v1_8_R3.MovingObjectPosition in project custom-items-gradle by knokko.

the class EntityLineIntersection method distanceToStart.

public static double distanceToStart(Entity entity, Location lineStartLocation, Vector direction, double safeUpperBound) {
    net.minecraft.server.v1_12_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
    Vec3D lineStart = new Vec3D(lineStartLocation.getX(), lineStartLocation.getY(), lineStartLocation.getZ());
    Vec3D lineEnd = new Vec3D(lineStartLocation.getX() + safeUpperBound * direction.getX(), lineStartLocation.getY() + safeUpperBound * direction.getY(), lineStartLocation.getZ() + safeUpperBound * direction.getZ());
    MovingObjectPosition intersection = nmsEntity.getBoundingBox().b(lineStart, lineEnd);
    if (intersection != null) {
        return Math.sqrt(intersection.pos.distanceSquared(lineStart));
    } else {
        return Double.POSITIVE_INFINITY;
    }
}
Also used : MovingObjectPosition(net.minecraft.server.v1_12_R1.MovingObjectPosition) CraftEntity(org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity) Vec3D(net.minecraft.server.v1_12_R1.Vec3D)

Aggregations

Location (org.bukkit.Location)3 Entity (org.bukkit.entity.Entity)3 WarlordsPlayer (com.ebicep.warlords.player.WarlordsPlayer)2 Nullable (javax.annotation.Nullable)2 MovingObjectPosition (net.minecraft.server.v1_12_R1.MovingObjectPosition)2 Vec3D (net.minecraft.server.v1_12_R1.Vec3D)2 CraftEntity (org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity)2 Vector (org.bukkit.util.Vector)2 Warlords (com.ebicep.warlords.Warlords)1 WarlordsDamageHealingEvent (com.ebicep.warlords.events.WarlordsDamageHealingEvent)1 WarlordsFlagUpdatedEvent (com.ebicep.warlords.events.WarlordsFlagUpdatedEvent)1 Game (com.ebicep.warlords.game.Game)1 Team (com.ebicep.warlords.game.Team)1 com.ebicep.warlords.game.flags (com.ebicep.warlords.game.flags)1 com.ebicep.warlords.game.option.marker (com.ebicep.warlords.game.option.marker)1 ScoreboardHandler (com.ebicep.warlords.game.option.marker.scoreboard.ScoreboardHandler)1 SimpleScoreboardHandler (com.ebicep.warlords.game.option.marker.scoreboard.SimpleScoreboardHandler)1 CooldownTypes (com.ebicep.warlords.player.cooldowns.CooldownTypes)1 RegularCooldown (com.ebicep.warlords.player.cooldowns.cooldowns.RegularCooldown)1 GameRunnable (com.ebicep.warlords.util.warlords.GameRunnable)1