Java Code Examples for org.bukkit.entity.Player#isOnGround()

The following examples show how to use org.bukkit.entity.Player#isOnGround() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CombatLogTracker.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Get the cause of the player's imminent death, or null if they are not about to die NOTE: not
 * idempotent, has the side effect of clearing the recentDamage cache
 */
public @Nullable ImminentDeath getImminentDeath(Player player) {
  // If the player is already dead or in creative mode, we don't care
  if (player.isDead() || player.getGameMode() == GameMode.CREATIVE) return null;

  // If the player was on the ground, or is flying, or is able to fly, they are fine
  if (!(player.isOnGround() || player.isFlying() || player.getAllowFlight())) {
    // If the player is falling, detect an imminent falling death
    double fallDistance = player.getFallDistance();
    Block landingBlock = null;
    int waterDepth = 0;
    Location location = player.getLocation();

    if (location.getY() > 256) {
      // If player is above Y 256, assume they fell at least to there
      fallDistance += location.getY() - 256;
      location.setY(256);
    }

    // Search the blocks directly beneath the player until we find what they would have landed on
    Block block = null;
    for (; location.getY() >= 0; location.add(0, -1, 0)) {
      block = location.getBlock();
      if (block != null) {
        landingBlock = block;

        if (Materials.isWater(landingBlock.getType())) {
          // If the player falls through water, reset fall distance and inc the water depth
          fallDistance = -1;
          waterDepth += 1;

          // Break if they have fallen through enough water to stop falling
          if (waterDepth >= BREAK_FALL_WATER_DEPTH) break;
        } else {
          // If the block is not water, reset the water depth
          waterDepth = 0;

          if (Materials.isSolid(landingBlock.getType())
              || Materials.isLava(landingBlock.getType())) {
            // Break if the player hits a solid block or lava
            break;
          } else if (landingBlock.getType() == Material.WEB) {
            // If they hit web, reset their fall distance, but assume they keep falling
            fallDistance = -1;
          }
        }
      }

      fallDistance += 1;
    }

    double resistanceFactor = getResistanceFactor(player);
    boolean fireResistance = hasFireResistance(player);

    // Now decide if the landing would have killed them
    if (location.getBlockY() < 0) {
      // The player would have fallen into the void
      return new ImminentDeath(EntityDamageEvent.DamageCause.VOID, location, null, false);
    } else if (landingBlock != null) {
      if (Materials.isSolid(landingBlock.getType())
          && player.getHealth() <= resistanceFactor * (fallDistance - SAFE_FALL_DISTANCE)) {
        // The player would have landed on a solid block and taken enough fall damage to kill them
        return new ImminentDeath(
            EntityDamageEvent.DamageCause.FALL,
            landingBlock.getLocation().add(0, 0.5, 0),
            null,
            false);
      } else if (Materials.isLava(landingBlock.getType())
          && resistanceFactor > 0
          && !fireResistance) {
        // The player would have landed in lava, and we give the lava the benefit of the doubt
        return new ImminentDeath(
            EntityDamageEvent.DamageCause.LAVA, landingBlock.getLocation(), landingBlock, false);
      }
    }
  }

  // If we didn't predict a falling death, detect combat log due to recent damage
  Damage damage = this.recentDamage.remove(player);
  if (damage != null && damage.time.plus(RECENT_DAMAGE_THRESHOLD).isAfter(Instant.now())) {
    // Player logged out too soon after taking damage
    return new ImminentDeath(damage.event.getCause(), player.getLocation(), null, true);
  }

  return null;
}
 
Example 2
Source File: CombatLogTracker.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Get the cause of the player's imminent death, or null if they are not about to die
 * NOTE: not idempotent, has the side effect of clearing the recentDamage cache
 */
public @Nullable ImminentDeath getImminentDeath(Player player) {
    // If the player is already dead or in creative mode, we don't care
    if(player.isDead() || player.getGameMode() == GameMode.CREATIVE) return null;

    // If the player was on the ground, or is flying, or is able to fly, they are fine
    if(!(player.isOnGround() || player.isFlying() || player.getAllowFlight())) {
        // If the player is falling, detect an imminent falling death
        double fallDistance = player.getFallDistance();
        Block landingBlock = null;
        int waterDepth = 0;
        Location location = player.getLocation();

        if(location.getY() > 256) {
            // If player is above Y 256, assume they fell at least to there
            fallDistance += location.getY() - 256;
            location.setY(256);
        }

        // Search the blocks directly beneath the player until we find what they would have landed on
        Block block = null;
        for(; location.getY() >= 0; location.add(0, -1, 0)) {
            block = location.getBlock();
            if(block != null) {
                landingBlock = block;

                if(Materials.isWater(landingBlock.getType())) {
                    // If the player falls through water, reset fall distance and inc the water depth
                    fallDistance = -1;
                    waterDepth += 1;

                    // Break if they have fallen through enough water to stop falling
                    if(waterDepth >= BREAK_FALL_WATER_DEPTH) break;
                } else {
                    // If the block is not water, reset the water depth
                    waterDepth = 0;

                    if(Materials.isColliding(landingBlock.getType()) || Materials.isLava(landingBlock.getType())) {
                        // Break if the player hits a solid block or lava
                        break;
                    } else if(landingBlock.getType() == Material.WEB) {
                        // If they hit web, reset their fall distance, but assume they keep falling
                        fallDistance = -1;
                    }
                }
            }

            fallDistance += 1;
        }

        double resistanceFactor = getResistanceFactor(player);
        boolean fireResistance = hasFireResistance(player);

        // Now decide if the landing would have killed them
        if(location.getBlockY() < 0) {
            // The player would have fallen into the void
            return new ImminentDeath(EntityDamageEvent.DamageCause.VOID, location, null, false);
        } else if(landingBlock != null) {
            if(Materials.isColliding(landingBlock.getType()) && player.getHealth() <= resistanceFactor * (fallDistance - SAFE_FALL_DISTANCE)) {
                // The player would have landed on a solid block and taken enough fall damage to kill them
                return new ImminentDeath(EntityDamageEvent.DamageCause.FALL, landingBlock.getLocation().add(0, 0.5, 0), null, false);
            } else if (Materials.isLava(landingBlock.getType()) && resistanceFactor > 0 && !fireResistance) {
                // The player would have landed in lava, and we give the lava the benefit of the doubt
                return new ImminentDeath(EntityDamageEvent.DamageCause.LAVA, landingBlock.getLocation(), landingBlock, false);
            }
        }
    }

    // If we didn't predict a falling death, detect combat log due to recent damage
    Damage damage = this.recentDamage.remove(player);
    if(damage != null && damage.time.plus(RECENT_DAMAGE_THRESHOLD).isAfter(Instant.now())) {
        // Player logged out too soon after taking damage
        return new ImminentDeath(damage.event.getCause(), player.getLocation(), null, true);
    }

    return null;
}
 
Example 3
Source File: SitListener.java    From NyaaUtils with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onClickBlock(PlayerInteractEvent event) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && !event.hasItem()) {
        Block block = event.getClickedBlock();
        BlockFace face = event.getBlockFace();
        if (face == BlockFace.DOWN || block.isLiquid() || !plugin.cfg.sit_blocks.contains(block.getType())) {
            return;
        }
        Block relative = block.getRelative(0, 1, 0);
        Player player = event.getPlayer();
        if (messageCooldown.getIfPresent(player.getUniqueId()) != null) {
            return;
        }
        messageCooldown.put(player.getUniqueId(), true);
        if (!player.hasPermission("nu.sit") || !enabledPlayers.contains(player.getUniqueId()) || player.isInsideVehicle() || !player.getPassengers().isEmpty() || player.getGameMode() == GameMode.SPECTATOR || !player.isOnGround()) {
            return;
        }
        if (relative.isLiquid() || !(relative.isEmpty() || relative.isPassable())) {
            player.sendMessage(I18n.format("user.sit.invalid_location"));
            return;
        }
        Vector vector = block.getBoundingBox().getCenter().clone();
        Location loc = vector.setY(block.getBoundingBox().getMaxY()).toLocation(player.getWorld()).clone();
        for (SitLocation sl : plugin.cfg.sit_locations.values()) {
            if (sl.blocks != null && sl.x != null && sl.y != null && sl.z != null && sl.blocks.contains(block.getType().name())) {
                loc.add(sl.x, sl.y, sl.z);
            }
        }
        if (block.getBlockData() instanceof Directional) {
            face = ((Directional) block.getBlockData()).getFacing();
            if (face == BlockFace.EAST) {
                loc.setYaw(90);
            } else if (face == BlockFace.WEST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(-180);
            }
        } else {
            if (face == BlockFace.WEST) {
                loc.setYaw(90);
            } else if (face == BlockFace.EAST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(-180);
            } else {
                loc.setYaw(player.getEyeLocation().getYaw());
            }
        }
        for (Entity e : loc.getWorld().getNearbyEntities(loc, 0.5, 0.7, 0.5)) {
            if (e instanceof LivingEntity) {
                if (e.hasMetadata(metadata_key) || (e instanceof Player && e.isInsideVehicle() && e.getVehicle().hasMetadata(metadata_key))) {
                    player.sendMessage(I18n.format("user.sit.invalid_location"));
                    return;
                }
            }
        }
        Location safeLoc = player.getLocation().clone();
        ArmorStand armorStand = loc.getWorld().spawn(loc, ArmorStand.class, (e) -> {
            e.setVisible(false);
            e.setPersistent(false);
            e.setCanPickupItems(false);
            e.setBasePlate(false);
            e.setArms(false);
            e.setMarker(true);
            e.setInvulnerable(true);
            e.setGravity(false);
        });
        if (armorStand != null) {
            armorStand.setMetadata(metadata_key, new FixedMetadataValue(plugin, true));
            if (armorStand.addPassenger(player)) {
                safeLocations.put(player.getUniqueId(), safeLoc);
            } else {
                armorStand.remove();
            }
        }
    }
}
 
Example 4
Source File: CreateCommand.java    From HolographicDisplays with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
	Player player = CommandValidator.getPlayerSender(sender);
	String hologramName = args[0];

	if (!hologramName.matches("[a-zA-Z0-9_\\-]+")) {
		throw new CommandException("The name must contain only alphanumeric chars, underscores and hyphens.");
	}

	CommandValidator.isTrue(!NamedHologramManager.isExistingHologram(hologramName), "A hologram with that name already exists.");

	Location spawnLoc = player.getLocation();
	boolean moveUp = player.isOnGround();

	if (moveUp) {
		spawnLoc.add(0.0, 1.2, 0.0);
	}

	NamedHologram hologram = new NamedHologram(spawnLoc, hologramName);

	if (args.length > 1) {
		String text = Utils.join(args, " ", 1, args.length);
		CommandValidator.isTrue(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty.");
		
		CraftHologramLine line = CommandValidator.parseHologramLine(hologram, text, true);
		hologram.getLinesUnsafe().add(line);
		player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + label + " edit " + hologram.getName() + ")");
	} else {
		hologram.appendTextLine("Default hologram. Change it with " + Colors.PRIMARY + "/" + label + " edit " + hologram.getName());
	}

	NamedHologramManager.addHologram(hologram);
	hologram.refreshAll();

	HologramDatabase.saveHologram(hologram);
	HologramDatabase.trySaveToDisk();
	Location look = player.getLocation();
	look.setPitch(90);
	player.teleport(look, TeleportCause.PLUGIN);
	player.sendMessage(Colors.PRIMARY + "You created a hologram named '" + hologram.getName() + "'.");

	if (moveUp) {
		player.sendMessage(Colors.SECONDARY_SHADOW + "(You were on the ground, the hologram was automatically moved up. If you use /" + label + " movehere " + hologram.getName() + ", the hologram will be moved to your feet)");
	}
}