Search in sources :

Example 1 with EquipmentSlot

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

the class EquipmentEditor method onPlayerChat.

@EventHandler(ignoreCancelled = true)
public void onPlayerChat(final AsyncPlayerChatEvent event) {
    EquipmentSlot slot = null;
    if (event.getMessage().contains("helmet") && event.getPlayer().hasPermission("citizens.npc.edit.equip.any-helmet")) {
        slot = EquipmentSlot.HELMET;
    }
    if (event.getMessage().contains("offhand") && event.getPlayer().hasPermission("citizens.npc.edit.equip.offhand")) {
        slot = EquipmentSlot.OFF_HAND;
    }
    if (slot == null) {
        return;
    }
    final EquipmentSlot finalSlot = slot;
    Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {

        @Override
        public void run() {
            if (!event.getPlayer().isValid())
                return;
            ItemStack hand = event.getPlayer().getInventory().getItemInHand();
            if (hand.getType() == Material.AIR || hand.getAmount() <= 0) {
                return;
            }
            ItemStack old = npc.getTrait(Equipment.class).get(finalSlot);
            if (old != null && old.getType() != Material.AIR) {
                event.getPlayer().getWorld().dropItemNaturally(event.getPlayer().getLocation(), old);
            }
            ItemStack newStack = hand.clone();
            newStack.setAmount(1);
            npc.getTrait(Equipment.class).set(finalSlot, newStack);
            hand.setAmount(hand.getAmount() - 1);
            event.getPlayer().getInventory().setItemInHand(hand);
        }
    });
    event.setCancelled(true);
}
Also used : EquipmentSlot(net.citizensnpcs.api.trait.trait.Equipment.EquipmentSlot) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 2 with EquipmentSlot

use of net.citizensnpcs.api.trait.trait.Equipment.EquipmentSlot 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

EquipmentSlot (net.citizensnpcs.api.trait.trait.Equipment.EquipmentSlot)2 ItemStack (org.bukkit.inventory.ItemStack)2 Equipment (net.citizensnpcs.api.trait.trait.Equipment)1 Material (org.bukkit.Material)1 EventHandler (org.bukkit.event.EventHandler)1