@EventHandler public void onEntityTameEvent(EntityTameEvent event) { /* Get the entity being tamed. */ LivingEntity entity = event.getEntity(); if (StackUtils.hasRequiredData(entity)) { /* Get the new quantity. Current size, less one (One being tamed) */ int newQuantity = StackUtils.getStackSize(entity) - 1; /* Clear the name of the entity that was just tamed. */ entity.setCustomName(""); /* If there are any mobs remaining in the stack, then peel em off to form a new stack. */ if (newQuantity > 0) { LivingEntity newEntity = getPlugin().getStackUtils().peelOffStack(entity, false); /* If there was an age in question, then assign it. */ if (newEntity instanceof Ageable) { ((Ageable) newEntity).setAge(((Ageable) event.getEntity()).getAge()); } /* * Yes I know the following aren't possible, but if someone uses a plugin that allows for taming of other mobs through NMS, * then this will account for that. */ /* If the stack had a colour, assign it. */ if (newEntity instanceof Colorable) { ((Colorable) newEntity).setColor(((Colorable) event.getEntity()).getColor()); } /* If it was a sheep, keep it's sheared status. */ if (newEntity instanceof Sheep) { ((Sheep) newEntity).setSheared(((Sheep) event.getEntity()).isSheared()); } } } }
@EventHandler public void interactEvent(PlayerInteractEntityEvent event) { /* If a LivingEntity was right clicked with a name tag, and stack custom named mobs is false, then follow. */ if (!getPlugin().getConfig().getBoolean("stack-custom-named-mobs") && event.getPlayer().getItemInHand().getType() == Material.NAME_TAG && event.getRightClicked() instanceof LivingEntity) { LivingEntity entity = (LivingEntity) event.getRightClicked(); ; /* Initialised blank name tag to get default name if it changes in future updates. */ ItemStack normalNameTag = new ItemStack(Material.NAME_TAG, 1, (byte) 0); /* Get the item the player is holding. */ ItemStack itemInHand = event.getPlayer().getItemInHand(); /* If the creature has the required data and the name tag isn't blank, then follow. */ if (StackUtils.hasRequiredData(entity) && !itemInHand .getItemMeta() .getDisplayName() .equalsIgnoreCase(normalNameTag.getItemMeta().getDisplayName())) { /* If there is more than one creature in the stack, then peel one off and don't allow it to stack again. */ if (StackUtils.getStackSize(entity) > 1) { getPlugin().getStackUtils().peelOffStack(entity, false); } else { entity.removeMetadata("quantity", getPlugin()); } } } }
@EventHandler(ignoreCancelled = true) public void onEntityExplode(EntityExplodeEvent event) { Entity entity = event.getEntity(); int quantity = StackUtils.getStackSize(entity); // Only bother with entities that are actually stacked. if (quantity < 2) { return; } if (!plugin.getConfig().getBoolean("exploding-creeper-kills-stack")) { // If we're not killing the full stack, peel off the rest. entity = plugin.getStackUtils().peelOffStack(entity); // Set 1 tick of no damage to prevent resulting explosion harming the new stack. if (entity instanceof LivingEntity) { ((LivingEntity) entity).setNoDamageTicks(1); } return; } // Amplify explosions if configured to do so. if (plugin.getConfig().getBoolean("magnify-stack-explosion.enable")) { /* * Charged creepers have a power of 6, TNT has a power of 4, normal creepers have a * power of 3. For everything else, 1 is probably a safe default. Really, it's a minimum * of 2 as that's how many mobs must be in the stack to reach this point. */ float power = quantity + (entity instanceof Creeper ? ((Creeper) entity).isPowered() ? 5 : 2 : 0); // Cap explosion power to configured maximum. quantity = Math.max( 1, Math.min( quantity, plugin.getConfig().getInt("magnify-stack-explosion.max-creeper-explosion-size"))); // Remove the entity - exploding entities don't fly off dying, they vanish with the explosion. entity.remove(); // Create the explosion. event.getLocation().getWorld().createExplosion(event.getLocation(), power); } }