132 lines
4.2 KiB
Java
132 lines
4.2 KiB
Java
package com.rsscripting.mobcage.util;
|
|
|
|
import com.rsscripting.mobcage.MobCage;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.ChestedHorse;
|
|
import org.bukkit.entity.LivingEntity;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.EntityEquipment;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
public class MobEquipmentUtil {
|
|
|
|
public static void handleEquipmentOnCapture(
|
|
MobCage plugin,
|
|
LivingEntity entity,
|
|
Player player
|
|
) {
|
|
|
|
EntityEquipment equipment = entity.getEquipment();
|
|
|
|
boolean hasEquipment =
|
|
hasItem(equipment != null ? equipment.getHelmet() : null) ||
|
|
hasItem(equipment != null ? equipment.getChestplate() : null) ||
|
|
hasItem(equipment != null ? equipment.getLeggings() : null) ||
|
|
hasItem(equipment != null ? equipment.getBoots() : null) ||
|
|
hasItem(equipment != null ? equipment.getItemInMainHand() : null) ||
|
|
hasItem(equipment != null ? equipment.getItemInOffHand() : null);
|
|
|
|
boolean hasChestedHorseEquipment = false;
|
|
|
|
if (entity instanceof ChestedHorse chestedHorse) {
|
|
|
|
if (hasItem(chestedHorse.getInventory().getSaddle())) {
|
|
hasChestedHorseEquipment = true;
|
|
}
|
|
|
|
if (chestedHorse.isCarryingChest()) {
|
|
hasChestedHorseEquipment = true;
|
|
|
|
for (ItemStack item : chestedHorse.getInventory().getStorageContents()) {
|
|
if (hasItem(item)) {
|
|
hasChestedHorseEquipment = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!hasEquipment && !hasChestedHorseEquipment) {
|
|
return;
|
|
}
|
|
|
|
boolean dropEquipment =
|
|
plugin.getConfig().getBoolean("drop-equip-on-capture");
|
|
|
|
if (dropEquipment) {
|
|
|
|
if (equipment != null) {
|
|
dropItem(entity, equipment.getHelmet());
|
|
dropItem(entity, equipment.getChestplate());
|
|
dropItem(entity, equipment.getLeggings());
|
|
dropItem(entity, equipment.getBoots());
|
|
dropItem(entity, equipment.getItemInMainHand());
|
|
dropItem(entity, equipment.getItemInOffHand());
|
|
}
|
|
|
|
if (entity instanceof ChestedHorse chestedHorse) {
|
|
|
|
if (chestedHorse.isCarryingChest()) {
|
|
|
|
for (ItemStack item : chestedHorse.getInventory().getStorageContents()) {
|
|
dropItem(entity, item);
|
|
}
|
|
|
|
// The chest itself is also a possession.
|
|
dropItem(
|
|
entity,
|
|
new ItemStack(Material.CHEST)
|
|
);
|
|
}
|
|
}
|
|
|
|
String message =
|
|
plugin.getConfig().getString("equipment-drop-message", "");
|
|
|
|
if (!message.isBlank()) {
|
|
player.sendRichMessage(message);
|
|
}
|
|
|
|
} else {
|
|
|
|
player.sendRichMessage(
|
|
"<yellow>The mob struggled during capture. During the struggle all of the mob's possessions were destroyed.</yellow>"
|
|
);
|
|
}
|
|
|
|
if (equipment != null) {
|
|
equipment.setHelmet(null);
|
|
equipment.setChestplate(null);
|
|
equipment.setLeggings(null);
|
|
equipment.setBoots(null);
|
|
equipment.setItemInMainHand(null);
|
|
equipment.setItemInOffHand(null);
|
|
}
|
|
|
|
if (entity instanceof ChestedHorse chestedHorse) {
|
|
|
|
chestedHorse.getInventory().setSaddle(null);
|
|
|
|
if (chestedHorse.isCarryingChest()) {
|
|
chestedHorse.getInventory().clear();
|
|
chestedHorse.setCarryingChest(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static boolean hasItem(ItemStack item) {
|
|
return item != null && !item.getType().isAir();
|
|
}
|
|
|
|
private static void dropItem(LivingEntity entity, ItemStack item) {
|
|
|
|
if (!hasItem(item)) {
|
|
return;
|
|
}
|
|
|
|
entity.getWorld().dropItemNaturally(
|
|
entity.getLocation(),
|
|
item.clone()
|
|
);
|
|
}
|
|
} |