org.bukkit.entity.Tameable Java Examples

The following examples show how to use org.bukkit.entity.Tameable. 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: PlayerInteractListener.java    From PetMaster with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #2
Source File: TameableTrait.java    From StackMob-3 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean checkTrait(Entity original, Entity nearby) {
    if(original instanceof Tameable){
        return (((Tameable) original).isTamed() || ((Tameable) nearby).isTamed());
    }
    return false;
}
 
Example #3
Source File: TameableTrait.java    From StackMob-3 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean checkTrait(Entity original) {
    if(original instanceof Tameable){
        return ((Tameable) original).isTamed();
    }
    return false;
}
 
Example #4
Source File: TameableTrait.java    From StackMob-3 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void applyTrait(Entity original, Entity spawned) {
    if(original instanceof Tameable){
        ((Tameable) spawned).setTamed(((Tameable) original).isTamed());
        ((Tameable) spawned).setOwner(((Tameable) original).getOwner());
    }
}
 
Example #5
Source File: PlayerInteractListener.java    From PetMaster with GNU General Public License v3.0 5 votes vote down vote up
@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);
		}
	}
}
 
Example #6
Source File: ExprEntityTamer.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
public OfflinePlayer convert(LivingEntity entity) {
	if (entity instanceof Tameable) {
		Tameable t = ((Tameable) entity);
		if (t.isTamed()) {
			return ((OfflinePlayer) t.getOwner());
		}
	}
	return null;
}
 
Example #7
Source File: CondIsTameable.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean check(LivingEntity entity) {
	return entity instanceof Tameable;
}
 
Example #8
Source File: PlayerAttackListener.java    From PetMaster with GNU General Public License v3.0 4 votes vote down vote up
private boolean isDamagerPlayerDifferentFromDamagedOwner(Entity damager, Entity damaged) {
	return damager instanceof Player && !((Tameable) damaged).getOwner().getUniqueId().equals(damager.getUniqueId());
}
 
Example #9
Source File: PlayerAttackListener.java    From PetMaster with GNU General Public License v3.0 4 votes vote down vote up
private boolean isDamagedOwned(Entity damaged) {
	return damaged instanceof Tameable && ((Tameable) damaged).getOwner() != null;
}
 
Example #10
Source File: PlayerInteractListener.java    From PetMaster with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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.");
		}
	}
}
 
Example #11
Source File: EntityHorsePet.java    From SonarPet with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void initiateEntityPet() {
    super.initiateEntityPet();
    ((Tameable) getBukkitEntity()).setOwner(Bukkit.getOfflinePlayer(getPet().getOwnerUUID()));
}
 
Example #12
Source File: EntityEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@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;
    }
}
 
Example #13
Source File: OcelotData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean match(final Ocelot entity) {
	return tamed == 0 || ((Tameable) entity).isTamed() == (tamed == 1);
}
 
Example #14
Source File: OcelotData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void set(final Ocelot entity) {
	if (TAMEABLE) {
		((Tameable) entity).setTamed(tamed != 0);
	}
}
 
Example #15
Source File: OcelotData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example #16
Source File: CommandClaimClear.java    From GriefDefender with MIT License 4 votes vote down vote up
@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))));
    }
}
 
Example #17
Source File: PlayerInteractListener.java    From PetMaster with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 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);
}