下面列出了怎么用org.bukkit.entity.Tameable的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Change the owner of a pet.
*
* @param player
* @param oldOwner
* @param newOwner
* @param tameable
*/
private void changeOwner(Player player, AnimalTamer oldOwner, Player newOwner, Tameable tameable) {
if (chargePrice(player, changeOwnerPrice)) {
// Change owner.
tameable.setOwner(newOwner);
player.sendMessage(plugin.getChatHeader()
+ plugin.getPluginLang().getString("owner-changed", "This pet was given to a new owner!"));
newOwner.sendMessage(plugin.getChatHeader()
+ plugin.getPluginLang().getString("new-owner", "Player PLAYER gave you ownership of a pet!")
.replace("PLAYER", player.getName()));
// Create new event to allow other plugins to be aware of the ownership change.
PlayerChangeAnimalOwnershipEvent playerChangeAnimalOwnershipEvent = new PlayerChangeAnimalOwnershipEvent(
oldOwner, newOwner, tameable);
Bukkit.getServer().getPluginManager().callEvent(playerChangeAnimalOwnershipEvent);
}
}
@Override
public boolean checkTrait(Entity original, Entity nearby) {
if(original instanceof Tameable){
return (((Tameable) original).isTamed() || ((Tameable) nearby).isTamed());
}
return false;
}
@Override
public boolean checkTrait(Entity original) {
if(original instanceof Tameable){
return ((Tameable) original).isTamed();
}
return false;
}
@Override
public void applyTrait(Entity original, Entity spawned) {
if(original instanceof Tameable){
((Tameable) spawned).setTamed(((Tameable) original).isTamed());
((Tameable) spawned).setOwner(((Tameable) original).getOwner());
}
}
@Nullable
@Override
public OfflinePlayer convert(LivingEntity entity) {
if (entity instanceof Tameable) {
Tameable t = ((Tameable) entity);
if (t.isTamed()) {
return ((OfflinePlayer) t.getOwner());
}
}
return null;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
if (shouldHandleEvent(event)) {
Tameable tameable = (Tameable) event.getRightClicked();
AnimalTamer currentOwner = tameable.getOwner();
if (currentOwner == null || currentOwner.getName() == null) {
return;
}
// Has the player clicked on one of his own pets?
Player player = event.getPlayer();
boolean isOwner = player.getUniqueId().equals(currentOwner.getUniqueId());
// Retrieve new owner from the map and delete corresponding entry.
Player newOwner = plugin.getSetOwnerCommand().collectPendingSetOwnershipRequest(player);
// Has the player requested to free one of his pets?
boolean freePet = plugin.getFreeCommand().collectPendingFreeRequest(player);
// Cannot change ownership or free pet if not owner and no bypass permission.
if ((newOwner != null || freePet) && !isOwner && !player.hasPermission("petmaster.admin")) {
player.sendMessage(plugin.getChatHeader() + plugin.getPluginLang()
.getString("not-owner", "You do not own this pet!").replace("PLAYER", player.getName()));
return;
}
if (newOwner != null) {
changeOwner(player, currentOwner, newOwner, tameable);
} else if (freePet) {
freePet(player, currentOwner, tameable);
} else if ((displayToOwner || !isOwner) && player.hasPermission("petmaster.showowner")) {
displayHologramAndMessage(player, currentOwner, tameable);
}
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onEntityChangeBlockEvent(EntityChangeBlockEvent event) {
if (!GDFlags.BLOCK_BREAK) {
return;
}
final Block block = event.getBlock();
if (block.isEmpty()) {
return;
}
final World world = event.getBlock().getWorld();
if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUID())) {
return;
}
final Location location = block.getLocation();
final GDClaim targetClaim = this.baseStorage.getClaimAt(location);
final Entity source = event.getEntity();
GDPermissionUser user = null;
if (source instanceof Tameable) {
final UUID uuid = NMSUtil.getInstance().getTameableOwnerUUID(source);
if (uuid != null) {
user = PermissionHolderCache.getInstance().getOrCreateUser(uuid);
}
}
if (user == null && !NMSUtil.getInstance().isEntityMonster(event.getEntity())) {
final GDEntity gdEntity = EntityTracker.getCachedEntity(event.getEntity().getEntityId());
if (gdEntity != null) {
user = PermissionHolderCache.getInstance().getOrCreateUser(gdEntity.getOwnerUUID());
}
if (user == null && source instanceof FallingBlock) {
// always allow blocks to fall if no user found
return;
}
}
final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, targetClaim, Flags.BLOCK_BREAK, event.getEntity(), event.getBlock(), user, TrustTypes.BUILDER, true);
if (result == Tristate.FALSE) {
event.setCancelled(true);
return;
}
}
@CommandCompletion("@gdentityids @gddummy")
@CommandAlias("claimclear")
@Description("Allows clearing of entities within one or more claims.")
@Syntax("<entity_id> [claim_uuid]")
@Subcommand("claim clear")
public void execute(Player player, String target, @Optional String claimId) {
World world = player.getWorld();
if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUID())) {
GriefDefenderPlugin.sendMessage(player, MessageCache.getInstance().CLAIM_DISABLED_WORLD);
return;
}
UUID claimUniqueId = null;
GDClaim targetClaim = null;
if (claimId == null) {
targetClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAt(player.getLocation());
final Component result = targetClaim.allowEdit(player);
if (result != null) {
GriefDefenderPlugin.sendMessage(player, result);
return;
}
claimUniqueId = targetClaim.getUniqueId();
} else {
if (!player.hasPermission(GDPermissions.COMMAND_DELETE_CLAIMS)) {
GriefDefenderPlugin.sendMessage(player, MessageCache.getInstance().COMMAND_CLAIMCLEAR_UUID_DENY);
return;
}
try {
claimUniqueId = UUID.fromString(claimId);
} catch (IllegalArgumentException e) {
return;
}
}
if (targetClaim.isWilderness()) {
GriefDefenderPlugin.sendMessage(player, GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.CLAIM_ACTION_NOT_AVAILABLE,
ImmutableMap.of("type", TextComponent.of("the wilderness"))));
return;
}
int count = 0;
String[] parts = target.split(":");
if (parts.length > 1) {
target = parts[1];
}
for (Chunk chunk : targetClaim.getChunks()) {
for (Entity entity : chunk.getEntities()) {
if (entity instanceof Player) {
continue;
}
if (entity instanceof Villager || !(entity instanceof LivingEntity)) {
continue;
}
if (entity instanceof Tameable) {
final UUID ownerUniqueId = NMSUtil.getInstance().getTameableOwnerUUID(entity);
if (ownerUniqueId != null && !ownerUniqueId.equals(player.getUniqueId())) {
continue;
}
}
LivingEntity livingEntity = (LivingEntity) entity;
String entityName = entity.getType().getName().toLowerCase();
if (target.equalsIgnoreCase("any") || target.equalsIgnoreCase("all") || target.equalsIgnoreCase("minecraft") || target.equalsIgnoreCase(entityName)) {
livingEntity.setHealth(0);
count++;
}
}
}
if (count == 0) {
GriefDefenderPlugin.sendMessage(player, GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.COMMAND_CLAIMCLEAR_NO_ENTITIES,
ImmutableMap.of("type", TextComponent.of(target, TextColor.GREEN))));
} else {
GriefDefenderPlugin.sendMessage(player, GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.COMMAND_CLAIMCLEAR_NO_ENTITIES,
ImmutableMap.of(
"amount", count,
"type", TextComponent.of(target, TextColor.GREEN))));
}
}
@Override
protected boolean init(final @Nullable Class<? extends Ocelot> c, final @Nullable Ocelot e) {
if (TAMEABLE)
tamed = e == null ? 0 : ((Tameable) e).isTamed() ? 1 : -1;
return true;
}
@Override
public void set(final Ocelot entity) {
if (TAMEABLE) {
((Tameable) entity).setTamed(tamed != 0);
}
}
@Override
protected boolean match(final Ocelot entity) {
return tamed == 0 || ((Tameable) entity).isTamed() == (tamed == 1);
}
@Override
public boolean check(LivingEntity entity) {
return entity instanceof Tameable;
}
@Override
public void initiateEntityPet() {
super.initiateEntityPet();
((Tameable) getBukkitEntity()).setOwner(Bukkit.getOfflinePlayer(getPet().getOwnerUUID()));
}
/**
* Displays a hologram, and automatically delete it after a given delay.
*
* @param player
* @param owner
* @param tameable
*/
@SuppressWarnings("deprecation")
private void displayHologramAndMessage(Player player, AnimalTamer owner, Tameable tameable) {
if (hologramMessage) {
double offset = HORSE_OFFSET;
if (tameable instanceof Ocelot || version >= 14 && tameable instanceof Cat) {
if (!displayCat || !player.hasPermission("petmaster.showowner.cat")) {
return;
}
offset = CAT_OFFSET;
} else if (tameable instanceof Wolf) {
if (!displayDog || !player.hasPermission("petmaster.showowner.dog")) {
return;
}
offset = DOG_OFFSET;
} else if (version >= 11 && tameable instanceof Llama) {
if (!displayLlama || !player.hasPermission("petmaster.showowner.llama")) {
return;
}
offset = LLAMA_OFFSET;
} else if (version >= 12 && tameable instanceof Parrot) {
if (!displayParrot || !player.hasPermission("petmaster.showowner.parrot")) {
return;
}
offset = PARROT_OFFSET;
} else if (!displayHorse || !player.hasPermission("petmaster.showowner.horse")) {
return;
}
Location eventLocation = tameable.getLocation();
// Create location with offset.
Location hologramLocation = new Location(eventLocation.getWorld(), eventLocation.getX(),
eventLocation.getY() + offset, eventLocation.getZ());
final Hologram hologram = HologramsAPI.createHologram(plugin, hologramLocation);
hologram.appendTextLine(
ChatColor.GRAY + plugin.getPluginLang().getString("petmaster-hologram", "Pet owned by ")
+ ChatColor.GOLD + owner.getName());
// Runnable to delete hologram.
new BukkitRunnable() {
@Override
public void run() {
hologram.delete();
}
}.runTaskLater(plugin, hologramDuration);
}
String healthInfo = "";
if (showHealth) {
Animals animal = (Animals) tameable;
String currentHealth = String.format("%.1f", animal.getHealth());
String maxHealth = version < 9 ? String.format("%.1f", animal.getMaxHealth())
: String.format("%.1f", animal.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
healthInfo = ChatColor.GRAY + ". " + plugin.getPluginLang().getString("petmaster-health", "Health: ")
+ ChatColor.GOLD + currentHealth + "/" + maxHealth;
}
if (chatMessage) {
player.sendMessage(plugin.getChatHeader() + plugin.getPluginLang().getString("petmaster-chat", "Pet owned by ")
+ ChatColor.GOLD + owner.getName() + healthInfo);
}
if (actionBarMessage) {
try {
FancyMessageSender.sendActionBarMessage(player, "&o" + ChatColor.GRAY
+ plugin.getPluginLang().getString("petmaster-action-bar", "Pet owned by ") + ChatColor.GOLD
+ owner.getName() + healthInfo);
} catch (Exception e) {
plugin.getLogger().warning("Errors while trying to display action bar message for pet ownership.");
}
}
}
private boolean isDamagedOwned(Entity damaged) {
return damaged instanceof Tameable && ((Tameable) damaged).getOwner() != null;
}
private boolean isDamagerPlayerDifferentFromDamagedOwner(Entity damager, Entity damaged) {
return damager instanceof Player && !((Tameable) damaged).getOwner().getUniqueId().equals(damager.getUniqueId());
}
/**
* Determines whether the current PlayerInteractEntityEvent should be handled.
*
* @param event
* @return true if the event should be handled, false otherwise
*/
private boolean shouldHandleEvent(PlayerInteractEntityEvent event) {
return !plugin.getEnableDisableCommand().isDisabled() && event.getRightClicked() instanceof Tameable
// On Minecraft 1.9+, the event is fired once per hand (HAND and OFF_HAND).
&& (version < 9 || event.getHand() == EquipmentSlot.HAND);
}