下面列出了org.bukkit.entity.Entity#hasMetadata ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@EventHandler(ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
if (event == null || ChunkManager.getInstance().getDisabledWorlds().contains(event.getLocation().getWorld().getName())) {
return; // do not do anything in case explosions get cancelled
}
final Entity detonator = event.getEntity();
if (detonator == null || detonator.hasMetadata("ObbyEntity")) {
return;
}
if (event.getLocation().getBlock().hasMetadata("ObbyEntity")) {
return;
}
if (event.getYield() <= 0.51) {
return;
}
ChunkManager.getInstance().handleExplosion(event);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void EntityDamageByEntityEvent(EntityDamageByEntityEvent e) {
Entity damager = e.getDamager();
Entity damaged = e.getEntity();
// Block self damaging (Enderpearls cause weird behavior)
if (damager.getUniqueId().equals(damaged.getUniqueId()))
return;
if (damaged instanceof Player)
CEventHandler.handleEvent((Player) damaged, e, damageTaken);
if (damager instanceof Player)
CEventHandler.handleEvent((Player) damager, e, damageGiven);
else if (damager instanceof Arrow)
if (damager.hasMetadata("ce.bow.item") || damager.hasMetadata("ce.bow.enchantment"))
CEventHandler.handleBows((Player) ((Projectile) damager).getShooter(), e);
}
@EventHandler
private void entityDamageByEntity(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager();
if (damager instanceof Player) {
Player player = (Player) damager;
ItemStack item = player.getInventory().getItemInMainHand();
CustomItem customItem = CustomItemManager.getCustomItem(item);
if (customItem != null) {
if (verifyCustomItem(customItem, player, true)) {
customItem.onAttack(event, new PlayerDetails(item, player));
}
}
} else {
if (damager.hasMetadata("CustomItem-bow")) {
Object[] data = (Object[]) damager.getMetadata("CustomItem-bow").get(0).value();
((CustomBow) data[0]).onProjectileDamageEntity(event, (DelayedPlayerDetails) data[1]);
}
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onHit(EntityDamageByEntityEvent event) {
final Entity damager = event.getDamager();
if (!event.isCancelled()
|| event.getCause() != EntityDamageEvent.DamageCause.PROJECTILE
|| !(damager instanceof Projectile)
|| !damager.hasMetadata(METADATA_KEY)) return;
final Player shooter = (Player) ((Projectile) damager).getShooter();
if (match.getParticipant(shooter) == null) return;
damager.remove();
shooter.getInventory().addItem(new ItemStack(Material.ARROW));
}
public boolean matchesRequirements(Player player, Entity damaged) {
if (damaged.getType().name().equals("ARMOR_STAND"))
return false;
if (player.getWorld() != damaged.getWorld())
return false;
if (damaged instanceof Player) {
if (!plugin.configStore.showPlayers && !damaged.hasMetadata("NPC"))
return false;
} else {
if (!plugin.configStore.showMobs)
return false;
}
if (!plugin.configStore.showNPC && damaged.hasMetadata("NPC"))
return false;
if (!plugin.configStore.showMiniaturePets && damaged.hasMetadata("MiniaturePet"))
return false;
if (plugin.healthUtil.isDisabled(player.getLocation()))
return false;
if (plugin.configStore.worlds.contains(player.getWorld().getName()))
return false;
if (plugin.configStore.usePerms && !player.hasPermission("ActionHealth.Health"))
return false;
if (player.getUniqueId() == damaged.getUniqueId())
return false;
if (plugin.toggle.contains(player.getUniqueId())) {
sendMessage(player);
return false;
}
return true;
}
/**
* Removes monsters around location l
*
* @param l location
*/
public void removeMobs(final Location l) {
if (!inWorld(l)) {
return;
}
//plugin.getLogger().info("DEBUG: removing mobs");
// Don't remove mobs if at spawn
if (this.isAtSpawn(l)) {
//plugin.getLogger().info("DEBUG: at spawn!");
return;
}
final int px = l.getBlockX();
final int py = l.getBlockY();
final int pz = l.getBlockZ();
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
final Chunk c = l.getWorld().getChunkAt(new Location(l.getWorld(), px + x * 16, py, pz + z * 16));
if (c.isLoaded()) {
for (final Entity e : c.getEntities()) {
//plugin.getLogger().info("DEBUG: " + e.getType());
// Don't remove if the entity is an NPC or has a name tag
if (e.getCustomName() != null || e.hasMetadata("NPC"))
continue;
if (e instanceof Monster && !Settings.mobWhiteList.contains(e.getType())) {
e.remove();
}
}
}
}
}
}
/**
* Despawn all npc's on a single world.
*
* @param world World to despawn npc's on.
*/
public void despawnAll(World world) {
for(Entity entity : world.getEntities()) {
if(entity.hasMetadata("NPC")) {
entity.remove();
}
}
}
public static UUID getWhoPlaced(Entity tnt) {
if (tnt.getType().equals(EntityType.PRIMED_TNT)) {
if (tnt.hasMetadata("source")) {
return (UUID) tnt.getMetadata("source").get(0).value();
}
}
return null;
}
@EventHandler(ignoreCancelled = true)
public void fixEntityDamage(EntityDamageByEntityEvent event) {
Entity projectile = event.getDamager();
if (projectile.hasMetadata("customProjectile")) {
// If the custom projectile replaced an arrow, recreate some effects specific to arrows
if (projectile.hasMetadata("damage")) {
boolean critical = projectile.getMetadata("critical").get(0).asBoolean();
int knockback = projectile.getMetadata("knockback").get(0).asInt();
double damage = projectile.getMetadata("damage").get(0).asDouble();
double speed = projectile.getVelocity().length();
// Reproduce the damage calculation from nms.EntityArrow with the addition of our modifier
int finalDamage = (int) Math.ceil(speed * damage * this.velocityMod);
if (critical) {
finalDamage += match.getRandom().nextInt(finalDamage / 2 + 2);
}
event.setDamage(finalDamage);
// Flame arrows - target burns for 5 seconds always
if (projectile.getFireTicks() > 0) {
event.getEntity().setFireTicks(100);
}
// Reproduce the knockback calculation for punch bows
if (knockback > 0) {
Vector projectileVelocity = projectile.getVelocity();
double horizontalSpeed =
Math.sqrt(
projectileVelocity.getX() * projectileVelocity.getX()
+ projectileVelocity.getZ() * projectileVelocity.getZ());
Vector velocity = event.getEntity().getVelocity();
velocity.setX(
velocity.getX() + projectileVelocity.getX() * knockback * 0.6 / horizontalSpeed);
velocity.setY(velocity.getY() + 0.1);
velocity.setZ(
velocity.getZ() + projectileVelocity.getZ() * knockback * 0.6 / horizontalSpeed);
event.getEntity().setVelocity(velocity);
}
// If the projectile is not an arrow, play an impact sound.
if (event.getEntity() instanceof Player
&& (projectile instanceof Projectile && !(projectile instanceof Arrow))) {
Projectile customProjectile = (Projectile) projectile;
if (customProjectile.getShooter() instanceof Player) {
Player bukkitShooter = (Player) customProjectile.getShooter();
MatchPlayer shooter = match.getPlayer(bukkitShooter);
if (shooter != null && event.getEntity() != null) {
shooter.playSound(PROJECTILE_SOUND);
}
}
}
}
// Apply any potion effects attached to the projectile
if (event.getEntity() instanceof LivingEntity) {
for (PotionEffect potionEffect : this.potionEffects) {
((LivingEntity) event.getEntity()).addPotionEffect(potionEffect);
}
}
}
}
@EventHandler(ignoreCancelled = true)
public void fixEntityDamage(EntityDamageByEntityEvent event) {
Entity projectile = event.getDamager();
if(projectile.hasMetadata("customProjectile")) {
// If the custom projectile replaced an arrow, recreate some effects specific to arrows
if(projectile.hasMetadata("damage")) {
boolean critical = projectile.getMetadata("critical").get(0).asBoolean();
int knockback = projectile.getMetadata("knockback").get(0).asInt();
double damage = projectile.getMetadata("damage").get(0).asDouble();
double speed = projectile.getVelocity().length();
// Reproduce the damage calculation from nms.EntityArrow with the addition of our modifier
int finalDamage = (int) Math.ceil(speed * damage * this.velocityMod);
if(critical) {
finalDamage += random.nextInt(finalDamage / 2 + 2);
}
event.setDamage(finalDamage);
// Flame arrows - target burns for 5 seconds always
if(projectile.getFireTicks() > 0) {
event.getEntity().setFireTicks(100);
}
// Reproduce the knockback calculation for punch bows
if(knockback > 0) {
Vector projectileVelocity = projectile.getVelocity();
double horizontalSpeed = Math.sqrt(projectileVelocity.getX() * projectileVelocity.getX() +
projectileVelocity.getZ() * projectileVelocity.getZ());
Vector velocity = event.getEntity().getVelocity();
velocity.setX(velocity.getX() + projectileVelocity.getX() * knockback * 0.6 / horizontalSpeed);
velocity.setY(velocity.getY() + 0.1);
velocity.setZ(velocity.getZ() + projectileVelocity.getZ() * knockback * 0.6 / horizontalSpeed);
event.getEntity().setVelocity(velocity);
}
}
// Apply any potion effects attached to the projectile
if(event.getEntity() instanceof LivingEntity) {
for(PotionEffect potionEffect : this.potionEffects) {
((LivingEntity) event.getEntity()).addPotionEffect(potionEffect);
}
}
}
}
public static boolean hasValidMetadata(Entity entity, String metaTag){
return entity.hasMetadata(metaTag) &&
entity.getMetadata(metaTag).size() != 0;
}
private boolean checkMetadata(Entity entity){
return entity.hasMetadata("NPC");
}
@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();
}
}
}
}
@Override
protected Boolean execute(String playerID) throws QuestRuntimeException {
Location location = loc.getLocation(playerID);
int[] neededAmounts = new int[types.length];
for (int i = 0; i < neededAmounts.length; i++) {
neededAmounts[i] = 0;
}
Collection<Entity> entities = location.getWorld().getEntities();
loop:
for (Entity entity : entities) {
if (!(entity instanceof LivingEntity)) {
continue;
}
if (name != null && (entity.getCustomName() == null || !entity.getCustomName().equals(name))) {
continue;
}
if (marked != null) {
if (!entity.hasMetadata("betonquest-marked")) {
continue;
}
List<MetadataValue> meta = entity.getMetadata("betonquest-marked");
for (MetadataValue m : meta) {
if (!m.asString().equals(marked)) {
continue loop;
}
}
}
double r = range.getDouble(playerID);
if (entity.getLocation().distanceSquared(location) < r * r) {
EntityType theType = entity.getType();
for (int i = 0; i < types.length; i++) {
if (theType == types[i]) {
neededAmounts[i]++;
break;
}
}
}
}
for (int i = 0; i < amounts.length; i++) {
if (neededAmounts[i] < amounts[i].getInt(playerID)) {
return false;
}
}
return true;
}
private boolean onInteract(Player player, Entity entity) {
// check if it's the right entity type
if (!entity.getType().equals(mobType)) {
return false;
}
// if the entity should have a name and it does not match, return
if (name != null && (entity.getCustomName() == null || !entity.getCustomName().equals(name))) {
return false;
}
// check if the entity is correctly marked
if (marked != null) {
if (!entity.hasMetadata("betonquest-marked")) {
return false;
}
List<MetadataValue> meta = entity.getMetadata("betonquest-marked");
for (MetadataValue m : meta) {
if (!m.asString().equals(marked)) {
return false;
}
}
}
// check if the player has this objective
String playerID = PlayerConverter.getID(player);
if (containsPlayer(playerID) && checkConditions(playerID)) {
// Check location matches
if (loc != null) {
try {
Location location = loc.getLocation(playerID);
double r = range.getDouble(playerID);
if (!entity.getWorld().equals(location.getWorld())
|| entity.getLocation().distance(location) > r) {
return false;
}
} catch (QuestRuntimeException e) {
LogUtils.getLogger().log(Level.WARNING, "Error while handling '" + instruction.getID() + "' objective: " + e.getMessage());
LogUtils.logThrowable(e);
}
}
// get data off the player
EntityInteractData playerData = (EntityInteractData) dataMap.get(playerID);
// check if player already interacted with entity
if (playerData.containsEntity(entity))
return false;
// right mob is interacted with, handle data update
playerData.subtract();
playerData.addEntity(entity);
if (playerData.isZero()) {
completeObjective(playerID);
} else if (notify && playerData.getAmount() % notifyInterval == 0) {
// send a notification
Config.sendNotify(playerID, "mobs_to_click", new String[]{String.valueOf(playerData.getAmount())},
"mobs_to_click,info");
}
return true;
}
return false;
}
@Override
protected Void execute(String playerID) throws QuestRuntimeException {
Location location = loc.getLocation(playerID);
Collection<Entity> entities = location.getWorld().getEntities();
loop:
for (Entity entity : entities) {
if (!(entity instanceof LivingEntity)) {
continue;
}
if (name != null && (entity.getCustomName() == null || !entity.getCustomName().equals(name))) {
continue;
}
if (marked != null) {
if (!entity.hasMetadata("betonquest-marked")) {
continue;
}
List<MetadataValue> meta = entity.getMetadata("betonquest-marked");
for (MetadataValue m : meta) {
if (!m.asString().equals(marked)) {
continue loop;
}
}
}
double r = range.getDouble(playerID);
if (entity.getLocation().distanceSquared(location) < r * r) {
EntityType entityType = entity.getType();
for (EntityType allowedType : types) {
if (entityType == allowedType) {
if (kill) {
LivingEntity living = (LivingEntity) entity;
living.damage(living.getHealth() + 10);
} else {
entity.remove();
}
break;
}
}
}
}
return null;
}
/**
* Checks if new entities can be added to island
* @param island
* @param bypass - true if this is being done by a player with authorization to bypass limits
* @param ent - the entity
* @return true if at the limit, false if not
*/
private boolean atLimit(Island island, boolean bypass, Entity ent) {
int count = 0;
checkLimits:
if (bypass || Settings.entityLimits.get(ent.getType()) > 0) {
// If bypass, just tag the creature. If not, then we need to count creatures
if (!bypass) {
// Run through all the current entities on this world
for (Entity entity: ent.getWorld().getEntities()) {
// If it is the right one
if (entity.getType().equals(ent.getType())) {
if (DEBUG)
plugin.getLogger().info("DEBUG: " + entity.getType() + " found");
// Check spawn location
if (entity.hasMetadata("spawnLoc")) {
if (DEBUG)
plugin.getLogger().info("DEBUG: has meta");
// Get the meta data
List<MetadataValue> values = entity.getMetadata("spawnLoc");
for (MetadataValue v : values) {
// There is a chance another plugin also uses the meta data spawnLoc
if (v.getOwningPlugin().equals(plugin)) {
// Get the island spawn location
Location spawnLoc = Util.getLocationString(v.asString());
if (DEBUG)
plugin.getLogger().info("DEBUG: entity spawnLoc = " + spawnLoc);
if (spawnLoc != null && spawnLoc.equals(island.getCenter())) {
// Entity is on this island
count++;
if (DEBUG)
plugin.getLogger().info("DEBUG: entity is on island. Number = " + count);
if (count >= Settings.entityLimits.get(ent.getType())) {
// No more allowed!
if (DEBUG)
plugin.getLogger().info("DEBUG: no more allowed! >=" + count);
break checkLimits;
}
}
}
}
}
}
}
}
// Okay to spawn, but tag it
ent.setMetadata("spawnLoc", new FixedMetadataValue(plugin, Util.getStringLocation(island.getCenter())));
if (DEBUG)
plugin.getLogger().info("DEBUG: spawn okay");
return false;
}
// Cancel - no spawning - tell nearby players
if (DEBUG)
plugin.getLogger().info("DEBUG: spawn cancelled");
return true;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void EntityDamageEvent(EntityDamageEvent e) {
Entity damaged = e.getEntity();
if (damaged instanceof Player) {
CEventHandler.handleEvent((Player) damaged, e, damageNature);
if (damaged.hasMetadata("ce.springs")) {
e.setCancelled(true);
Vector vel = damaged.getVelocity();
vel.setY((vel.getY() * -0.75) > 1 ? vel.getY() * -0.75 : 0);
damaged.setVelocity(vel);
}
}
}
public static boolean isNPC(Entity entity) {
return entity.hasMetadata("NPC");
}
/**
* Check if an entity is a NPC.
*
* @param entity Entity to check.
* @return Entity is a npc?
*/
public boolean isNPC(Entity entity) {
return entity.hasMetadata("NPC");
}