下面列出了org.bukkit.entity.Entity#getMetadata ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static @Nullable ProjectileDefinition getProjectileDefinition(Entity entity) {
MetadataValue metadataValue = entity.getMetadata("projectileDefinition", PGM.get());
if (metadataValue != null) {
return (ProjectileDefinition) metadataValue.value();
} else if (launchingDefinition.get() != null) {
return launchingDefinition.get();
} else {
return null;
}
}
private boolean noCooldown(Entity e) {
for(MetadataValue meta : e.getMetadata("lastDrop")) {
if(meta.getOwningPlugin() == plugin) {
return System.currentTimeMillis() - meta.asFloat() > 100;
}
}
return true;
}
private boolean isTracked(Entity entity) {
return entity.getMetadata(METADATA_KEY, PGM.get()) != null;
}
static @Nullable ProjectileDefinition getProjectileDefinition(Entity entity) {
final MetadataValue metadataValue = entity.getMetadata(METADATA_KEY, PGM.get());
return metadataValue == null ? null : (ProjectileDefinition) metadataValue.value();
}
@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;
}