Search in sources :

Example 1 with Equipment

use of net.citizensnpcs.api.trait.trait.Equipment in project Denizen-For-Bukkit by DenizenScript.

the class EquipCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Map<String, dItem> equipment = (Map<String, dItem>) scriptEntry.getObject("equipment");
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("entities", entities.toString()) + aH.debugObj("equipment", equipment.toString()));
    for (dEntity entity : entities) {
        if (entity.isGeneric()) {
            dB.echoError(scriptEntry.getResidingQueue(), "Cannot equip generic entity " + entity.identify() + "!");
        } else if (entity.isCitizensNPC()) {
            dNPC npc = entity.getDenizenNPC();
            if (npc != null) {
                Equipment trait = npc.getEquipmentTrait();
                if (equipment.get("hand") != null) {
                    trait.set(0, equipment.get("hand").getItemStack());
                }
                if (equipment.get("head") != null) {
                    trait.set(1, equipment.get("head").getItemStack());
                }
                if (equipment.get("chest") != null) {
                    trait.set(2, equipment.get("chest").getItemStack());
                }
                if (equipment.get("legs") != null) {
                    trait.set(3, equipment.get("legs").getItemStack());
                }
                if (equipment.get("boots") != null) {
                    trait.set(4, equipment.get("boots").getItemStack());
                }
                if (equipment.get("offhand") != null) {
                    trait.set(5, equipment.get("offhand").getItemStack());
                }
                if (npc.isSpawned()) {
                    LivingEntity livingEntity = npc.getLivingEntity();
                    if (livingEntity.getType() == EntityType.HORSE) {
                        if (equipment.get("saddle") != null) {
                            ((Horse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                        }
                        if (equipment.get("horse_armor") != null) {
                            ((Horse) livingEntity).getInventory().setArmor(equipment.get("horse_armor").getItemStack());
                        }
                    } else if (livingEntity.getType() == EntityType.PIG) {
                        if (equipment.get("saddle") != null) {
                            dItem saddle = equipment.get("saddle");
                            if (saddle.getItemStack().getType() == Material.SADDLE) {
                                ((Pig) livingEntity).setSaddle(true);
                            } else {
                                ((Pig) livingEntity).setSaddle(false);
                            }
                        }
                    }
                }
            }
        } else {
            LivingEntity livingEntity = entity.getLivingEntity();
            if (livingEntity != null) {
                if (livingEntity.getType() == EntityType.HORSE) {
                    if (equipment.get("saddle") != null) {
                        ((Horse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                    }
                    if (equipment.get("horse_armor") != null) {
                        ((Horse) livingEntity).getInventory().setArmor(equipment.get("horse_armor").getItemStack());
                    }
                } else if (livingEntity.getType() == EntityType.PIG) {
                    if (equipment.get("saddle") != null) {
                        dItem saddle = equipment.get("saddle");
                        if (saddle.getItemStack().getType() == Material.SADDLE) {
                            ((Pig) livingEntity).setSaddle(true);
                        } else {
                            ((Pig) livingEntity).setSaddle(false);
                        }
                    }
                } else {
                    if (equipment.get("hand") != null) {
                        NMSHandler.getInstance().getEntityHelper().setItemInHand(livingEntity, equipment.get("hand").getItemStack());
                    }
                    if (equipment.get("head") != null) {
                        livingEntity.getEquipment().setHelmet(equipment.get("head").getItemStack());
                    }
                    if (equipment.get("chest") != null) {
                        livingEntity.getEquipment().setChestplate(equipment.get("chest").getItemStack());
                    }
                    if (equipment.get("legs") != null) {
                        livingEntity.getEquipment().setLeggings(equipment.get("legs").getItemStack());
                    }
                    if (equipment.get("boots") != null) {
                        livingEntity.getEquipment().setBoots(equipment.get("boots").getItemStack());
                    }
                    if (equipment.get("offhand") != null) {
                        NMSHandler.getInstance().getEntityHelper().setItemInOffHand(livingEntity, equipment.get("offhand").getItemStack());
                    }
                }
            }
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) Equipment(net.citizensnpcs.api.trait.trait.Equipment) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Horse(org.bukkit.entity.Horse) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) HashMap(java.util.HashMap) Map(java.util.Map) Pig(org.bukkit.entity.Pig)

Example 2 with Equipment

use of net.citizensnpcs.api.trait.trait.Equipment in project Denizen-For-Bukkit by DenizenScript.

the class dNPCRegistry method onInventoryClick.

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    Inventory inventory = event.getInventory();
    if (inventory.getHolder() instanceof dNPC) {
        dNPC npc = (dNPC) inventory.getHolder();
        npc.getInventory().setContents(inventory.getContents());
        Equipment equipment = npc.getEquipmentTrait();
        for (int i = 0; i < 5; i++) {
            equipment.set(i, inventory.getItem(i));
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) Equipment(net.citizensnpcs.api.trait.trait.Equipment) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 3 with Equipment

use of net.citizensnpcs.api.trait.trait.Equipment in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNPCHelper method onInventoryClick.

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    Inventory inventory = event.getInventory();
    if (inventory.getHolder() instanceof NPCTag) {
        NPCTag npc = (NPCTag) inventory.getHolder();
        npc.getInventory().setContents(inventory.getContents());
        Equipment equipment = npc.getEquipmentTrait();
        for (int i = 0; i < 5; i++) {
            equipment.set(i, inventory.getItem(i));
        }
    }
}
Also used : Equipment(net.citizensnpcs.api.trait.trait.Equipment) NPCTag(com.denizenscript.denizen.objects.NPCTag) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Equipment

use of net.citizensnpcs.api.trait.trait.Equipment in project Denizen-For-Bukkit by DenizenScript.

the class HeadCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    Element skin = scriptEntry.getElement("skin");
    dMaterial material = scriptEntry.getdObject("material");
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("entities", entities.toString()) + (skin != null ? skin.debug() : "") + (material != null ? material.debug() : ""));
    ItemStack item = null;
    // Create head item with chosen skin, or item/skin
    if (skin != null) {
        item = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
        ItemMeta itemMeta = item.getItemMeta();
        ((SkullMeta) itemMeta).setOwner(skin.asString().replaceAll("[pP]@", ""));
        item.setItemMeta(itemMeta);
    } else if (material != null) {
        item = new ItemStack(material.getMaterial());
    }
    for (dEntity entity : entities) {
        if (entity.isCitizensNPC()) {
            if (!entity.getDenizenNPC().getCitizen().hasTrait(Equipment.class)) {
                entity.getDenizenNPC().getCitizen().addTrait(Equipment.class);
            }
            Equipment trait = entity.getDenizenNPC().getCitizen().getTrait(Equipment.class);
            trait.set(1, item);
        } else if (entity.isPlayer()) {
            entity.getPlayer().getInventory().setHelmet(item);
        } else {
            if (entity.isLivingEntity() && entity.getLivingEntity().getEquipment() != null) {
                entity.getLivingEntity().getEquipment().setHelmet(item);
            } else {
                dB.echoError(scriptEntry.getResidingQueue(), entity.identify() + " is not a living entity!");
            }
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dMaterial(net.aufdemrand.denizen.objects.dMaterial) Equipment(net.citizensnpcs.api.trait.trait.Equipment) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Element(net.aufdemrand.denizencore.objects.Element) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) SkullMeta(org.bukkit.inventory.meta.SkullMeta) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 5 with Equipment

use of net.citizensnpcs.api.trait.trait.Equipment in project Citizens2 by CitizensDev.

the class GenericEquipper method equip.

@Override
public void equip(Player equipper, NPC toEquip) {
    ItemStack hand = equipper.getInventory().getItemInHand();
    Equipment trait = toEquip.getTrait(Equipment.class);
    EquipmentSlot slot = EquipmentSlot.HAND;
    Material type = hand == null ? Material.AIR : hand.getType();
    // First, determine the slot to edit
    if (type.name().equals("ELYTRA") && !equipper.isSneaking()) {
        slot = EquipmentSlot.CHESTPLATE;
    } else {
        switch(type) {
            case SKULL_ITEM:
            case PUMPKIN:
            case JACK_O_LANTERN:
            case LEATHER_HELMET:
            case CHAINMAIL_HELMET:
            case GOLD_HELMET:
            case IRON_HELMET:
            case DIAMOND_HELMET:
                if (!equipper.isSneaking()) {
                    slot = EquipmentSlot.HELMET;
                }
                break;
            case LEATHER_CHESTPLATE:
            case CHAINMAIL_CHESTPLATE:
            case GOLD_CHESTPLATE:
            case IRON_CHESTPLATE:
            case DIAMOND_CHESTPLATE:
                if (!equipper.isSneaking()) {
                    slot = EquipmentSlot.CHESTPLATE;
                }
                break;
            case LEATHER_LEGGINGS:
            case CHAINMAIL_LEGGINGS:
            case GOLD_LEGGINGS:
            case IRON_LEGGINGS:
            case DIAMOND_LEGGINGS:
                if (!equipper.isSneaking()) {
                    slot = EquipmentSlot.LEGGINGS;
                }
                break;
            case LEATHER_BOOTS:
            case CHAINMAIL_BOOTS:
            case GOLD_BOOTS:
            case IRON_BOOTS:
            case DIAMOND_BOOTS:
                if (!equipper.isSneaking()) {
                    slot = EquipmentSlot.BOOTS;
                }
                break;
            case AIR:
                if (equipper.isSneaking()) {
                    for (int i = 0; i < 6; i++) {
                        if (trait.get(i) != null && trait.get(i).getType() != Material.AIR) {
                            equipper.getWorld().dropItemNaturally(toEquip.getEntity().getLocation(), trait.get(i));
                            trait.set(i, null);
                        }
                    }
                    Messaging.sendTr(equipper, Messages.EQUIPMENT_EDITOR_ALL_ITEMS_REMOVED, toEquip.getName());
                } else {
                    return;
                }
                break;
            default:
                break;
        }
    }
    // Drop any previous equipment on the ground
    ItemStack equippedItem = trait.get(slot);
    if (equippedItem != null && equippedItem.getType() != Material.AIR) {
        equipper.getWorld().dropItemNaturally(toEquip.getEntity().getLocation(), equippedItem);
    }
    // Now edit the equipment based on the slot
    if (type != Material.AIR) {
        // Set the proper slot with one of the item
        ItemStack clone = hand.clone();
        clone.setAmount(1);
        trait.set(slot, clone);
        hand.setAmount(hand.getAmount() - 1);
        equipper.getInventory().setItemInHand(hand);
    }
}
Also used : Equipment(net.citizensnpcs.api.trait.trait.Equipment) EquipmentSlot(net.citizensnpcs.api.trait.trait.Equipment.EquipmentSlot) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

Equipment (net.citizensnpcs.api.trait.trait.Equipment)7 List (java.util.List)3 ItemStack (org.bukkit.inventory.ItemStack)3 EntityTag (com.denizenscript.denizen.objects.EntityTag)2 NPCTag (com.denizenscript.denizen.objects.NPCTag)2 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)2 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)2 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)2 LivingEntity (org.bukkit.entity.LivingEntity)2 EventHandler (org.bukkit.event.EventHandler)2 Inventory (org.bukkit.inventory.Inventory)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 SkullMeta (org.bukkit.inventory.meta.SkullMeta)2 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)1 net.aufdemrand.denizen.objects.dMaterial (net.aufdemrand.denizen.objects.dMaterial)1