Search in sources :

Example 51 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class RecallAction method perform.

@Override
public SpellResult perform(CastContext context) {
    this.context = context;
    enabledTypes.clear();
    warps.clear();
    commands.clear();
    Mage mage = context.getMage();
    MageController controller = context.getController();
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    Set<String> unlockedWarps = new HashSet<>();
    ConfigurationSection mageData = mage.getData();
    String unlockedString = mageData.getString(unlockKey);
    if (unlockedString != null && !unlockedString.isEmpty()) {
        unlockedWarps.addAll(Arrays.asList(StringUtils.split(unlockedString, ',')));
    }
    Set<String> friends = new HashSet<>();
    String friendString = mageData.getString(friendKey);
    if (friendString != null && !friendString.isEmpty()) {
        friends.addAll(Arrays.asList(StringUtils.split(friendString, ',')));
    }
    ConfigurationSection warpConfig = null;
    if (parameters.contains("warps")) {
        warpConfig = ConfigurationUtils.getConfigurationSection(parameters, "warps");
    }
    ConfigurationSection commandConfig = null;
    if (parameters.contains("commands")) {
        commandConfig = ConfigurationUtils.getConfigurationSection(parameters, "commands");
    }
    if (parameters.contains("unlock")) {
        String unlockWarp = parameters.getString("unlock");
        if (unlockWarp == null || unlockWarp.isEmpty() || unlockedWarps.contains(unlockWarp)) {
            return SpellResult.NO_ACTION;
        }
        if (warpConfig == null && commandConfig == null) {
            return SpellResult.FAIL;
        }
        unlockedWarps.add(unlockWarp);
        unlockedString = StringUtils.join(unlockedWarps, ",");
        mageData.set(unlockKey, unlockedString);
        String warpName = unlockWarp;
        ConfigurationSection config = warpConfig == null ? null : warpConfig.getConfigurationSection(unlockWarp);
        if (config != null) {
            warpName = config.getString("name", warpName);
        } else {
            config = commandConfig == null ? null : commandConfig.getConfigurationSection(unlockWarp);
            if (config != null) {
                warpName = config.getString("name", warpName);
            }
        }
        String unlockMessage = context.getMessage("unlock_warp").replace("$name", warpName);
        context.sendMessage(unlockMessage);
        return SpellResult.CAST;
    }
    if (parameters.contains("lock")) {
        String lockWarpString = parameters.getString("lock");
        String[] lockWarps = StringUtils.split(lockWarpString, ',');
        boolean locked = false;
        for (String lockWarp : lockWarps) {
            if (unlockedWarps.contains(lockWarp)) {
                locked = true;
                unlockedWarps.remove(lockWarp);
            }
        }
        if (locked) {
            unlockedString = StringUtils.join(unlockedWarps, ",");
            mageData.set(unlockKey, unlockedString);
        }
        return locked ? SpellResult.DEACTIVATE : SpellResult.NO_ACTION;
    }
    if (parameters.contains("addfriend")) {
        String friendName = parameters.getString("addfriend");
        if (friendName == null || friendName.isEmpty()) {
            return SpellResult.NO_ACTION;
        }
        Player online = DeprecatedUtils.getPlayer(friendName);
        if (online == null) {
            return SpellResult.FAIL;
        }
        String uuid = online.getUniqueId().toString();
        if (friends.contains(uuid)) {
            return SpellResult.NO_ACTION;
        }
        friends.add(uuid);
        friendString = StringUtils.join(friends, ",");
        mageData.set(friendKey, friendString);
        String message = context.getMessage("add_friend").replace("$name", online.getDisplayName());
        context.sendMessage(message);
        return SpellResult.CAST;
    }
    if (parameters.contains("removefriend")) {
        String friendName = parameters.getString("removefriend");
        Player online = DeprecatedUtils.getPlayer(friendName);
        if (online == null) {
            return SpellResult.FAIL;
        }
        String uuid = online.getUniqueId().toString();
        if (!friends.contains(uuid)) {
            return SpellResult.NO_ACTION;
        }
        friends.remove(uuid);
        friendString = StringUtils.join(friends, ",");
        mageData.set(friendKey, friendString);
        String message = context.getMessage("remove_friend").replace("$name", online.getDisplayName());
        context.sendMessage(message);
        return SpellResult.DEACTIVATE;
    }
    Location playerLocation = mage.getLocation();
    for (RecallType testType : RecallType.values()) {
        // Special-case for warps
        if (testType == RecallType.WARP) {
            if (warpConfig != null) {
                Collection<String> warpKeys = warpConfig.getKeys(false);
                for (String warpKey : warpKeys) {
                    ConfigurationSection config = warpConfig.getConfigurationSection(warpKey);
                    boolean isLocked = config.getBoolean("locked", false);
                    if (!isLocked || unlockedWarps.contains(warpKey)) {
                        warps.put(warpKey, config);
                    }
                }
            }
        } else // Special-case for commands
        if (testType == RecallType.COMMAND) {
            if (commandConfig != null) {
                Collection<String> commandKeys = commandConfig.getKeys(false);
                for (String commandKey : commandKeys) {
                    ConfigurationSection config = commandConfig.getConfigurationSection(commandKey);
                    boolean isLocked = config.getBoolean("locked", false);
                    if (!isLocked || unlockedWarps.contains(commandKey)) {
                        commands.put(commandKey, config);
                    }
                }
            }
        } else {
            if (parameters.getBoolean("allow_" + testType.name().toLowerCase(), true)) {
                enabledTypes.add(testType);
            }
        }
    }
    if (warps.size() > 0) {
        enabledTypes.add(RecallType.WARP);
    }
    if (commands.size() > 0) {
        enabledTypes.add(RecallType.COMMAND);
    }
    if (parameters.contains("warp")) {
        String warpName = parameters.getString("warp");
        Waypoint waypoint = getWarp(warpName);
        if (tryTeleport(player, waypoint)) {
            return SpellResult.CAST;
        }
        return SpellResult.FAIL;
    } else if (parameters.contains("command")) {
        String commandName = parameters.getString("command");
        Waypoint waypoint = getCommand(context, commandName);
        if (tryTeleport(player, waypoint)) {
            return SpellResult.CAST;
        }
        return SpellResult.FAIL;
    } else if (parameters.contains("type")) {
        String typeString = parameters.getString("type", "");
        if (parameters.getBoolean("allow_marker", true)) {
            if (typeString.equalsIgnoreCase("remove")) {
                if (removeMarker()) {
                    return SpellResult.TARGET_SELECTED;
                }
                return SpellResult.FAIL;
            }
            if (typeString.equalsIgnoreCase("place")) {
                Block block = context.getLocation().getBlock();
                if (parameters.getBoolean("marker_requires_build", true) && !context.hasBuildPermission(block)) {
                    return SpellResult.NO_TARGET;
                }
                if (hasMarker() && parameters.getBoolean("confirm_marker", true)) {
                    showMarkerConfirm(context);
                    return SpellResult.CAST;
                }
                if (placeMarker(block)) {
                    return SpellResult.TARGET_SELECTED;
                }
                return SpellResult.FAIL;
            }
        }
        RecallType recallType = RecallType.valueOf(typeString.toUpperCase());
        Waypoint location = getWaypoint(player, recallType, 0, parameters, context);
        if (tryTeleport(player, location)) {
            return SpellResult.CAST;
        }
        return SpellResult.FAIL;
    }
    List<Waypoint> allWaypoints = new ArrayList<>();
    for (RecallType selectedType : enabledTypes) {
        if (selectedType == RecallType.FRIENDS) {
            for (String friendId : friends) {
                Waypoint targetLocation = getFriend(friendId);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.WARP) {
            for (String warpKey : warps.keySet()) {
                Waypoint targetLocation = getWarp(warpKey);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.COMMAND) {
            for (String commandKey : commands.keySet()) {
                Waypoint targetLocation = getCommand(context, commandKey);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.WAND) {
            List<LostWand> lostWands = mage.getLostWands();
            for (int i = 0; i < lostWands.size(); i++) {
                Waypoint targetLocation = getWaypoint(player, selectedType, i, parameters, context);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.FIELDS) {
            Map<String, Location> fields = controller.getHomeLocations(player);
            if (fields != null) {
                for (Map.Entry<String, Location> fieldEntry : fields.entrySet()) {
                    Location location = fieldEntry.getValue().clone();
                    location.setX(location.getX() + 0.5);
                    location.setZ(location.getZ() + 0.5);
                    allWaypoints.add(new Waypoint(RecallType.FIELDS, location, fieldEntry.getKey(), context.getMessage("cast_field"), context.getMessage("no_target_field"), context.getMessage("description_field", ""), getIcon(context, parameters, "icon_field"), true));
                }
            }
        } else {
            Waypoint targetLocation = getWaypoint(player, selectedType, 0, parameters, context);
            if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                allWaypoints.add(targetLocation);
            }
        }
    }
    if (allWaypoints.size() == 0) {
        return SpellResult.NO_TARGET;
    }
    options.clear();
    Collections.sort(allWaypoints);
    String inventoryTitle = context.getMessage("title", "Recall");
    int invSize = (int) Math.ceil(allWaypoints.size() / 9.0f) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    int index = 0;
    for (Waypoint waypoint : allWaypoints) {
        ItemStack waypointItem;
        if (waypoint.iconURL != null && !waypoint.iconURL.isEmpty()) {
            waypointItem = InventoryUtils.getURLSkull(waypoint.iconURL);
        } else {
            waypointItem = waypoint.icon.getItemStack(1);
        }
        ItemMeta meta = waypointItem == null ? null : waypointItem.getItemMeta();
        if (meta == null) {
            waypointItem = new ItemStack(DefaultWaypointMaterial);
            meta = waypointItem.getItemMeta();
            controller.getLogger().warning("Invalid waypoint icon for " + waypoint.name);
        }
        meta.setDisplayName(waypoint.name);
        if (waypoint.description != null && waypoint.description.length() > 0) {
            List<String> lore = new ArrayList<>();
            InventoryUtils.wrapText(waypoint.description, lore);
            meta.setLore(lore);
        }
        waypointItem.setItemMeta(meta);
        waypointItem = InventoryUtils.makeReal(waypointItem);
        InventoryUtils.hideFlags(waypointItem, (byte) 63);
        InventoryUtils.setMeta(waypointItem, "waypoint", "true");
        CompatibilityUtils.makeUnbreakable(waypointItem);
        displayInventory.setItem(index, waypointItem);
        options.put(index, waypoint);
        index++;
    }
    mage.activateGUI(this, displayInventory);
    return SpellResult.CAST;
}
Also used : ArrayList(java.util.ArrayList) MageController(com.elmakers.mine.bukkit.api.magic.MageController) ArrayList(java.util.ArrayList) List(java.util.List) ItemMeta(org.bukkit.inventory.meta.ItemMeta) HashSet(java.util.HashSet) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Collection(java.util.Collection) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) Inventory(org.bukkit.inventory.Inventory) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) Location(org.bukkit.Location)

Example 52 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class DisarmAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity target = context.getTargetEntity();
    if (target == null || !(target instanceof LivingEntity)) {
        return SpellResult.NO_TARGET;
    }
    LivingEntity entity = (LivingEntity) target;
    EntityEquipment equipment = entity.getEquipment();
    ItemStack stack = null;
    Integer originalSlot = null;
    boolean isMainHand = false;
    if (displayName == null) {
        stack = equipment.getItemInMainHand();
        isMainHand = true;
    } else {
        // This is not compatible
        keepInInventory = false;
        // Must be a player in this case
        if (!(entity instanceof Player)) {
            return SpellResult.PLAYER_REQUIRED;
        }
        PlayerInventory playerInventory = ((Player) entity).getInventory();
        for (originalSlot = 0; originalSlot < playerInventory.getSize(); originalSlot++) {
            ItemStack item = playerInventory.getItem(originalSlot);
            if (InventoryUtils.isEmpty(item))
                continue;
            ItemMeta meta = item.getItemMeta();
            if (meta == null || !meta.hasDisplayName())
                continue;
            if (meta.getDisplayName().equals(displayName)) {
                stack = item;
                isMainHand = originalSlot == playerInventory.getHeldItemSlot();
                break;
            }
        }
    }
    if (InventoryUtils.isEmpty(stack)) {
        return SpellResult.NO_TARGET;
    }
    // Special case for wands
    MageController controller = context.getController();
    Mage targetMage = controller.getMage(entity);
    if (com.elmakers.mine.bukkit.wand.Wand.isWand(stack) && controller.isMage(entity)) {
        Mage mage = context.getMage();
        // This gets overridden by superpower...
        if (!mage.isSuperPowered() && targetMage.isSuperProtected()) {
            return SpellResult.NO_TARGET;
        }
        Wand activeWand = targetMage.getActiveWand();
        if (activeWand != null && isMainHand) {
            targetMage.getActiveWand().deactivate();
            stack = equipment.getItemInMainHand();
        }
    }
    Integer targetSlot = null;
    PlayerInventory targetInventory = null;
    ItemStack swapItem = null;
    Random random = context.getRandom();
    if (entity instanceof Player && keepInInventory) {
        Player targetPlayer = (Player) entity;
        targetInventory = targetPlayer.getInventory();
        originalSlot = targetInventory.getHeldItemSlot();
        List<Integer> validSlots = new ArrayList<>();
        ItemStack[] contents = targetInventory.getContents();
        for (int i = minSlot; i <= maxSlot; i++) {
            if (contents[i] == null || contents[i].getType() == Material.AIR) {
                validSlots.add(i);
            }
        }
        // Randomly choose a slot if no empty one was found
        if (validSlots.size() == 0) {
            int swapSlot = random.nextInt(maxSlot - minSlot) + minSlot;
            swapItem = targetInventory.getItem(swapSlot);
            validSlots.add(swapSlot);
        }
        int chosen = random.nextInt(validSlots.size());
        targetSlot = validSlots.get(chosen);
    }
    if (displayName != null) {
        ((Player) entity).getInventory().setItem(originalSlot, null);
    } else {
        equipment.setItemInMainHand(swapItem);
    }
    if (targetSlot != null && targetInventory != null) {
        targetInventory.setItem(targetSlot, stack);
        if (originalSlot != null) {
            DisarmUndoAction disarmUndo = new DisarmUndoAction(targetMage, originalSlot, targetSlot);
            context.registerForUndo(disarmUndo);
        }
    } else {
        Location location = entity.getLocation();
        location.setY(location.getY() + 1);
        Item item = entity.getWorld().dropItemNaturally(location, stack);
        Vector velocity = item.getVelocity();
        velocity.setY(velocity.getY() * 5);
        SafetyUtils.setVelocity(item, velocity);
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) PlayerInventory(org.bukkit.inventory.PlayerInventory) LivingEntity(org.bukkit.entity.LivingEntity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Item(org.bukkit.entity.Item) Random(java.util.Random) EntityEquipment(org.bukkit.inventory.EntityEquipment) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Location(org.bukkit.Location)

Example 53 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class PlayerSelectAction method start.

@Override
public SpellResult start(CastContext context) {
    Mage mage = context.getMage();
    MageController controller = context.getController();
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    List<Player> allPlayers = null;
    players.clear();
    if (allowCrossWorld) {
        allPlayers = new ArrayList<>(controller.getPlugin().getServer().getOnlinePlayers());
    } else {
        allPlayers = context.getLocation().getWorld().getPlayers();
    }
    Collections.sort(allPlayers, new Comparator<Player>() {

        @Override
        public int compare(Player p1, Player p2) {
            return p1.getDisplayName().compareTo(p2.getDisplayName());
        }
    });
    int index = 0;
    for (Player targetPlayer : allPlayers) {
        if (!context.getTargetsCaster() && targetPlayer == player)
            continue;
        if (targetPlayer.hasPotionEffect(PotionEffectType.INVISIBILITY))
            continue;
        if (!context.canTarget(targetPlayer))
            continue;
        players.put(index++, new WeakReference<>(targetPlayer));
    }
    if (players.size() == 0)
        return SpellResult.NO_TARGET;
    String inventoryTitle = context.getMessage("title", "Select Player");
    int invSize = ((players.size() + 9) / 9) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    for (Map.Entry<Integer, WeakReference<Player>> entry : players.entrySet()) {
        Player targetPlayer = entry.getValue().get();
        if (targetPlayer == null)
            continue;
        String name = targetPlayer.getName();
        String displayName = targetPlayer.getDisplayName();
        @SuppressWarnings("deprecation") ItemStack playerItem = new ItemStack(Material.SKULL_ITEM, 1, (short) 0, (byte) 3);
        ItemMeta meta = playerItem.getItemMeta();
        meta.setDisplayName(displayName);
        if (meta instanceof SkullMeta) {
            SkullMeta skullData = (SkullMeta) meta;
            skullData.setOwner(name);
        }
        if (!name.equals(displayName)) {
            List<String> lore = new ArrayList<>();
            lore.add(name);
            meta.setLore(lore);
        }
        playerItem.setItemMeta(meta);
        displayInventory.setItem(entry.getKey(), playerItem);
    }
    active = true;
    mage.activateGUI(this, displayInventory);
    return SpellResult.NO_ACTION;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) SkullMeta(org.bukkit.inventory.meta.SkullMeta) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) WeakReference(java.lang.ref.WeakReference) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 54 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class GiveItemAction method prepare.

@Override
public void prepare(CastContext context, ConfigurationSection parameters) {
    super.prepare(context, parameters);
    MageController controller = context.getController();
    permissionNode = parameters.getString("permission", null);
    String itemKey = parameters.getString("item");
    item = controller.createItem(itemKey);
    if (item == null) {
        context.getLogger().warning("Invalid item: " + itemKey);
    } else {
        String name = parameters.getString("name", null);
        List<String> lore = ConfigurationUtils.getStringList(parameters, "lore");
        if ((name != null && !name.isEmpty()) || (lore != null && !lore.isEmpty())) {
            ItemMeta meta = item.getItemMeta();
            if (name != null && !name.isEmpty()) {
                meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
            }
            if (lore != null && !lore.isEmpty()) {
                for (int i = 0; i < lore.size(); i++) {
                    lore.set(i, ChatColor.translateAlternateColorCodes('&', lore.get(i)));
                }
                meta.setLore(lore);
            }
            item.setItemMeta(meta);
        }
    }
    String costKey = parameters.getString("requires");
    if (costKey != null && !costKey.isEmpty()) {
        requireItem = controller.createItem(costKey);
        if (requireItem == null) {
            context.getLogger().warning("Invalid required item: " + costKey);
        }
    }
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 55 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class LockAction method giveKey.

protected void giveKey(Mage mage, String keyName, String keyDescription) {
    if (!InventoryUtils.hasItem(mage.getInventory(), keyName)) {
        ItemStack keyItem = null;
        keyItem = iconType.getItemStack(1);
        ItemMeta meta = keyItem.getItemMeta();
        meta.setDisplayName(keyName);
        if (!keyDescription.isEmpty()) {
            List<String> lore = new ArrayList<>();
            String[] lines = StringUtils.split(keyDescription, '\n');
            for (String line : lines) {
                lore.add(line);
            }
            meta.setLore(lore);
        }
        keyItem.setItemMeta(meta);
        keyItem = CompatibilityUtils.makeReal(keyItem);
        CompatibilityUtils.makeUnplaceable(keyItem);
        InventoryUtils.makeKeep(keyItem);
        mage.giveItem(keyItem);
    }
}
Also used : ArrayList(java.util.ArrayList) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

ItemMeta (org.bukkit.inventory.meta.ItemMeta)361 ItemStack (org.bukkit.inventory.ItemStack)205 ArrayList (java.util.ArrayList)87 Player (org.bukkit.entity.Player)45 Inventory (org.bukkit.inventory.Inventory)33 Map (java.util.Map)29 Enchantment (org.bukkit.enchantments.Enchantment)23 EventHandler (org.bukkit.event.EventHandler)23 SkullMeta (org.bukkit.inventory.meta.SkullMeta)22 CompoundTag (com.wasteofplastic.org.jnbt.CompoundTag)20 ListTag (com.wasteofplastic.org.jnbt.ListTag)20 StringTag (com.wasteofplastic.org.jnbt.StringTag)20 Tag (com.wasteofplastic.org.jnbt.Tag)20 PlayerInventory (org.bukkit.inventory.PlayerInventory)17 IOException (java.io.IOException)15 Material (org.bukkit.Material)14 PotionMeta (org.bukkit.inventory.meta.PotionMeta)14 Mage (com.elmakers.mine.bukkit.api.magic.Mage)12 BookMeta (org.bukkit.inventory.meta.BookMeta)12 List (java.util.List)11