Search in sources :

Example 11 with BlockIterator

use of org.bukkit.util.BlockIterator in project acidisland by tastybento.

the class AcidInventory method onWaterBottleFill.

/**
 * Event that covers filling a bottle
 */
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onWaterBottleFill(final PlayerInteractEvent e) {
    if (DEBUG)
        plugin.getLogger().info("DEBUG: " + e.getEventName());
    Player player = e.getPlayer();
    if (!player.getWorld().getName().equalsIgnoreCase(Settings.worldName))
        return;
    if (Settings.acidDamage == 0D || !Settings.acidBottle)
        return;
    if (!player.getItemInHand().getType().equals(Material.GLASS_BOTTLE)) {
        return;
    }
    // plugin.getLogger().info(e.getEventName() + " called");
    // Look at what the player was looking at
    BlockIterator iter = new BlockIterator(player, 10);
    Block lastBlock = iter.next();
    while (iter.hasNext()) {
        lastBlock = iter.next();
        if (lastBlock.getType() == Material.AIR)
            continue;
        break;
    }
    // plugin.getLogger().info(lastBlock.getType().toString());
    if (lastBlock.getType().equals(Material.WATER) || lastBlock.getType().equals(Material.STATIONARY_WATER) || lastBlock.getType().equals(Material.CAULDRON)) {
        // They *may* have filled a bottle with water
        // Check inventory for POTIONS in a tick
        plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

            @Override
            public void run() {
                // plugin.getLogger().info("Checking inventory");
                PlayerInventory inv = e.getPlayer().getInventory();
                if (inv.contains(Material.POTION)) {
                    // plugin.getLogger().info("POTION in inventory");
                    // They have a POTION of some kind in inventory
                    int i = 0;
                    for (ItemStack item : inv.getContents()) {
                        if (item != null) {
                            // item.getType().toString());
                            if (item.getType().equals(Material.POTION)) {
                                NMSAbstraction nms = null;
                                try {
                                    nms = Util.checkVersion();
                                } catch (Exception ex) {
                                    return;
                                }
                                if (!nms.isPotion(item)) {
                                    // plugin.getLogger().info("Water bottle found!");
                                    ItemMeta meta = item.getItemMeta();
                                    meta.setDisplayName(plugin.myLocale(e.getPlayer().getUniqueId()).acidBottle);
                                    // ArrayList<String> lore = new
                                    // ArrayList<String>(Arrays.asList("Poison",
                                    // "Beware!", "Do not drink!"));
                                    meta.setLore(lore);
                                    item.setItemMeta(meta);
                                    inv.setItem(i, item);
                                }
                            }
                        }
                        i++;
                    }
                }
            }
        });
    }
}
Also used : BlockIterator(org.bukkit.util.BlockIterator) Player(org.bukkit.entity.Player) Block(org.bukkit.block.Block) ASkyBlock(com.wasteofplastic.acidisland.ASkyBlock) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack) NMSAbstraction(com.wasteofplastic.acidisland.nms.NMSAbstraction) ItemMeta(org.bukkit.inventory.meta.ItemMeta) EventHandler(org.bukkit.event.EventHandler)

Example 12 with BlockIterator

use of org.bukkit.util.BlockIterator in project Denizen-For-Bukkit by DenizenScript.

the class dPlayer method getAttribute.

@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    if (offlinePlayer == null) {
        return null;
    }
    // Defined in dEntity
    if (attribute.startsWith("is_player")) {
        return Element.TRUE.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("debug.log")) {
        dB.log(debug());
        return Element.TRUE.getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("debug.no_color")) {
        return new Element(ChatColor.stripColor(debug())).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("debug")) {
        return new Element(debug()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("prefix")) {
        return new Element(prefix).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("chat_history_list")) {
        return // TODO: UUID?
        new dList(PlayerTags.playerChatHistory.get(getName())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("chat_history")) {
        int x = 1;
        if (attribute.hasContext(1) && aH.matchesInteger(attribute.getContext(1))) {
            x = attribute.getIntContext(1);
        }
        // No playerchathistory? Return null.
        if (!PlayerTags.playerChatHistory.containsKey(getName())) {
            // TODO: UUID?
            return null;
        }
        // TODO: UUID?
        List<String> messages = PlayerTags.playerChatHistory.get(getName());
        if (messages.size() < x || x < 1) {
            return null;
        }
        return new Element(messages.get(x - 1)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("flag") && attribute.hasContext(1)) {
        String flag_name = attribute.getContext(1);
        if (attribute.getAttribute(2).equalsIgnoreCase("is_expired") || attribute.startsWith("isexpired")) {
            return new Element(!FlagManager.playerHasFlag(this, flag_name)).getAttribute(attribute.fulfill(2));
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("size") && !FlagManager.playerHasFlag(this, flag_name)) {
            return new Element(0).getAttribute(attribute.fulfill(2));
        }
        if (FlagManager.playerHasFlag(this, flag_name)) {
            FlagManager.Flag flag = DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag(this, flag_name);
            return new dList(flag.toString(), true, flag.values()).getAttribute(attribute.fulfill(1));
        }
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("has_flag") && attribute.hasContext(1)) {
        String flag_name = attribute.getContext(1);
        return new Element(FlagManager.playerHasFlag(this, flag_name)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("list_flags")) {
        dList allFlags = new dList(DenizenAPI.getCurrentInstance().flagManager().listPlayerFlags(this));
        dList searchFlags = null;
        if (!allFlags.isEmpty() && attribute.hasContext(1)) {
            searchFlags = new dList();
            String search = attribute.getContext(1);
            if (search.startsWith("regex:")) {
                try {
                    Pattern pattern = Pattern.compile(search.substring(6), Pattern.CASE_INSENSITIVE);
                    for (String flag : allFlags) {
                        if (pattern.matcher(flag).matches()) {
                            searchFlags.add(flag);
                        }
                    }
                } catch (Exception e) {
                    dB.echoError(e);
                }
            } else {
                search = CoreUtilities.toLowerCase(search);
                for (String flag : allFlags) {
                    if (CoreUtilities.toLowerCase(flag).contains(search)) {
                        searchFlags.add(flag);
                    }
                }
            }
        }
        return searchFlags == null ? allFlags.getAttribute(attribute.fulfill(1)) : searchFlags.getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("current_step")) {
        String outcome = "null";
        if (attribute.hasContext(1)) {
            try {
                outcome = DenizenAPI.getCurrentInstance().getSaves().getString("Players." + getName() + ".Scripts." + dScript.valueOf(attribute.getContext(1)).getName() + ".Current Step");
            } catch (Exception e) {
                outcome = "null";
            }
        }
        return new Element(outcome).getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("money")) {
        if (Depends.economy != null) {
            // -->
            if (attribute.startsWith("money.currency_singular")) {
                return new Element(Depends.economy.currencyNameSingular()).getAttribute(attribute.fulfill(2));
            }
            // -->
            if (attribute.startsWith("money.currency")) {
                return new Element(Depends.economy.currencyNamePlural()).getAttribute(attribute.fulfill(2));
            }
            return new Element(Depends.economy.getBalance(getOfflinePlayer())).getAttribute(attribute.fulfill(1));
        } else {
            if (!attribute.hasAlternative()) {
                dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
            }
            return null;
        }
    }
    if (attribute.startsWith("target")) {
        int range = 50;
        int attribs = 1;
        // -->
        if (attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) {
            attribs = 2;
            range = attribute.getIntContext(2);
        }
        List<Entity> entities = getPlayerEntity().getNearbyEntities(range, range, range);
        ArrayList<LivingEntity> possibleTargets = new ArrayList<LivingEntity>();
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity) {
                // if we have a context for entity types, check the entity
                if (attribute.hasContext(1)) {
                    String context = attribute.getContext(1);
                    if (CoreUtilities.toLowerCase(context).startsWith("li@")) {
                        context = context.substring(3);
                    }
                    for (String ent : context.split("\\|")) {
                        boolean valid = false;
                        if (ent.equalsIgnoreCase("npc") && dEntity.isCitizensNPC(entity)) {
                            valid = true;
                        } else if (dEntity.matches(ent)) {
                            // only accept generic entities that are not NPCs
                            if (dEntity.valueOf(ent).isGeneric()) {
                                if (dEntity.isCitizensNPC(entity)) {
                                    valid = true;
                                }
                            } else {
                                valid = true;
                            }
                        }
                        if (valid) {
                            possibleTargets.add((LivingEntity) entity);
                        }
                    }
                } else {
                    // no entity type specified
                    possibleTargets.add((LivingEntity) entity);
                    entity.getType();
                }
            }
        }
        // Find the valid target
        BlockIterator bi;
        try {
            bi = new BlockIterator(getPlayerEntity(), range);
        } catch (IllegalStateException e) {
            return null;
        }
        Block b;
        Location l;
        int bx, by, bz;
        double ex, ey, ez;
        // Loop through player's line of sight
        while (bi.hasNext()) {
            b = bi.next();
            bx = b.getX();
            by = b.getY();
            bz = b.getZ();
            if (b.getType() != Material.AIR) {
                // Line of sight is broken
                break;
            } else {
                // Check for entities near this block in the line of sight
                for (LivingEntity possibleTarget : possibleTargets) {
                    l = possibleTarget.getLocation();
                    ex = l.getX();
                    ey = l.getY();
                    ez = l.getZ();
                    if ((bx - .50 <= ex && ex <= bx + 1.50) && (bz - .50 <= ez && ez <= bz + 1.50) && (by - 1 <= ey && ey <= by + 2.5)) {
                        // Entity is close enough, so return it
                        return new dEntity(possibleTarget).getDenizenObject().getAttribute(attribute.fulfill(attribs));
                    }
                }
            }
        }
        return null;
    }
    // workaround for <e@entity.list_effects>
    if (attribute.startsWith("list_effects")) {
        dList effects = new dList();
        for (PotionEffect effect : getPlayerEntity().getActivePotionEffects()) {
            effects.add(effect.getType().getName() + "," + effect.getAmplifier() + "," + effect.getDuration() + "t");
        }
        return effects.getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("list")) {
        dB.echoError("DO NOT USE PLAYER.LIST AS A TAG, please use <server.list_online_players> and related tags!");
        List<String> players = new ArrayList<String>();
        if (attribute.startsWith("list.online")) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                players.add(player.getName());
            }
            return new dList(players).getAttribute(attribute.fulfill(2));
        } else if (attribute.startsWith("list.offline")) {
            for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
                if (!player.isOnline()) {
                    players.add("p@" + player.getUniqueId().toString());
                }
            }
            return new dList(players).getAttribute(attribute.fulfill(2));
        } else {
            for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
                players.add("p@" + player.getUniqueId().toString());
            }
            return new dList(players).getAttribute(attribute.fulfill(1));
        }
    }
    if (attribute.startsWith("name") && !isOnline()) // This can be parsed later with more detail if the player is online, so only check for offline.
    {
        return new Element(getName()).getAttribute(attribute.fulfill(1));
    } else if (attribute.startsWith("uuid") && !isOnline()) // This can be parsed later with more detail if the player is online, so only check for offline.
    {
        return new Element(offlinePlayer.getUniqueId().toString()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("type")) {
        return new Element("Player").getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("save_name")) {
        return new Element(getSaveName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("bed_spawn")) {
        if (getOfflinePlayer().getBedSpawnLocation() == null) {
            return null;
        }
        return new dLocation(getOfflinePlayer().getBedSpawnLocation()).getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("location") && !isOnline()) {
        return getLocation().getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("first_played")) {
        attribute = attribute.fulfill(1);
        if (attribute.startsWith("milliseconds") || attribute.startsWith("in_milliseconds")) {
            return new Element(getOfflinePlayer().getFirstPlayed()).getAttribute(attribute.fulfill(1));
        }
        return new Duration(getOfflinePlayer().getFirstPlayed() / 50).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("has_played_before")) {
        return new Element(true).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("health.is_scaled")) {
        return new Element(getPlayerEntity().isHealthScaled()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("health.scale")) {
        return new Element(getPlayerEntity().getHealthScale()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("exhaustion")) {
        return new Element(getPlayerEntity().getExhaustion()).getAttribute(attribute.fulfill(1));
    }
    // Handle dEntity oxygen tags here to allow getting them when the player is offline
    if (attribute.startsWith("oxygen.max")) {
        return new Duration((long) getMaximumAir()).getAttribute(attribute.fulfill(2));
    }
    if (attribute.startsWith("oxygen")) {
        return new Duration((long) getRemainingAir()).getAttribute(attribute.fulfill(1));
    }
    // Same with health tags
    if (attribute.startsWith("health.formatted")) {
        return EntityHealth.getHealthFormatted(new dEntity(getPlayerEntity()), attribute);
    }
    if (attribute.startsWith("health.percentage")) {
        double maxHealth = getPlayerEntity().getMaxHealth();
        if (attribute.hasContext(2)) {
            maxHealth = attribute.getIntContext(2);
        }
        return new Element((getPlayerEntity().getHealth() / maxHealth) * 100).getAttribute(attribute.fulfill(2));
    }
    if (attribute.startsWith("health.max")) {
        return new Element(getMaxHealth()).getAttribute(attribute.fulfill(2));
    }
    if (attribute.matches("health")) {
        return new Element(getHealth()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_banned")) {
        BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(getName());
        if (ban == null) {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        } else if (ban.getExpiration() == null) {
            return Element.TRUE.getAttribute(attribute.fulfill(1));
        }
        return new Element(ban.getExpiration().after(new Date())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_online")) {
        return new Element(isOnline()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_op")) {
        return new Element(getOfflinePlayer().isOp()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_whitelisted")) {
        return new Element(getOfflinePlayer().isWhitelisted()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("last_played")) {
        attribute = attribute.fulfill(1);
        if (attribute.startsWith("milliseconds") || attribute.startsWith("in_milliseconds")) {
            if (isOnline()) {
                return new Element(System.currentTimeMillis()).getAttribute(attribute.fulfill(1));
            }
            return new Element(getOfflinePlayer().getLastPlayed()).getAttribute(attribute.fulfill(1));
        }
        if (isOnline()) {
            return new Duration(System.currentTimeMillis() / 50).getAttribute(attribute);
        }
        return new Duration(getOfflinePlayer().getLastPlayed() / 50).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("groups")) {
        if (Depends.permissions == null) {
            if (!attribute.hasAlternative()) {
                dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
            }
            return null;
        }
        dList list = new dList();
        // TODO: optionally specify world
        for (String group : Depends.permissions.getGroups()) {
            if (Depends.permissions.playerInGroup(null, offlinePlayer, group)) {
                list.add(group);
            }
        }
        return list.getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("ban_info")) {
        attribute.fulfill(1);
        BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(getName());
        if (ban == null) {
            return null;
        } else if (ban.getExpiration() != null && ban.getExpiration().before(new Date())) {
            return null;
        }
        // -->
        if (attribute.startsWith("expiration") && ban.getExpiration() != null) {
            return new Duration(ban.getExpiration().getTime() / 50).getAttribute(attribute.fulfill(1));
        } else // -->
        if (attribute.startsWith("reason")) {
            return new Element(ban.getReason()).getAttribute(attribute.fulfill(1));
        } else // -->
        if (attribute.startsWith("created")) {
            return new Duration(ban.getCreated().getTime() / 50).getAttribute(attribute.fulfill(1));
        }
        return null;
    }
    // -->
    if (attribute.startsWith("in_group")) {
        if (Depends.permissions == null) {
            if (!attribute.hasAlternative()) {
                dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
            }
            return null;
        }
        String group = attribute.getContext(1);
        // Non-world specific permission
        if (attribute.getAttribute(2).startsWith("global")) {
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.playerInGroup((World) null, getName(), group)).getAttribute(attribute.fulfill(2));
        } else // Permission in certain world
        if (attribute.getAttribute(2).startsWith("world")) {
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.playerInGroup(attribute.getContext(2), getName(), group)).getAttribute(attribute.fulfill(2));
        } else // Permission in current world
        if (isOnline()) {
            return new Element(Depends.permissions.playerInGroup(getPlayerEntity(), group)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("permission") || attribute.startsWith("has_permission")) {
        String permission = attribute.getContext(1);
        // Non-world specific permission
        if (attribute.getAttribute(2).startsWith("global")) {
            if (Depends.permissions == null) {
                if (!attribute.hasAlternative()) {
                    dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
                }
                return null;
            }
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.has((World) null, getName(), permission)).getAttribute(attribute.fulfill(2));
        } else // Permission in certain world
        if (attribute.getAttribute(2).startsWith("world")) {
            if (Depends.permissions == null) {
                if (!attribute.hasAlternative()) {
                    dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
                }
                return null;
            }
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.has(attribute.getContext(2), getName(), permission)).getAttribute(attribute.fulfill(2));
        } else // Permission in current world
        if (isOnline()) {
            return new Element(getPlayerEntity().hasPermission(permission)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("inventory")) {
        return getInventory().getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("enderchest")) {
        return getEnderChest().getAttribute(attribute.fulfill(1));
    }
    // Player is required to be online after this point...
    if (!isOnline()) {
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("open_inventory")) {
        return new dInventory(getPlayerEntity().getOpenInventory().getTopInventory()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("item_on_cursor")) {
        return new dItem(getPlayerEntity().getItemOnCursor()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("item_in_hand.slot")) {
        return new Element(getPlayerEntity().getInventory().getHeldItemSlot() + 1).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.lines")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new dList(sidebar.getLines()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.title")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new Element(sidebar.getTitle()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.scores")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        dList scores = new dList();
        for (int score : sidebar.getScores()) {
            scores.add(String.valueOf(score));
        }
        return scores.getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.start")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new Element(sidebar.getStart()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.increment")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new Element(sidebar.getIncrement()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("skin_blob")) {
        return new Element(NMSHandler.getInstance().getProfileEditor().getPlayerSkinBlob(getPlayerEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("selected_npc")) {
        if (getPlayerEntity().hasMetadata("selected")) {
            return getSelectedNPC().getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("entity") && !attribute.startsWith("entity_")) {
        return new dEntity(getPlayerEntity()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("ip") || attribute.startsWith("host_name")) {
        attribute = attribute.fulfill(1);
        // -->
        if (attribute.startsWith("address_only")) {
            return new Element(getPlayerEntity().getAddress().toString()).getAttribute(attribute.fulfill(1));
        }
        String host = getPlayerEntity().getAddress().getHostName();
        // -->
        if (attribute.startsWith("address")) {
            return new Element(getPlayerEntity().getAddress().toString()).getAttribute(attribute.fulfill(1));
        }
        return new Element(host).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("name.display")) {
        return new Element(getPlayerEntity().getDisplayName()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("name.list")) {
        return new Element(getPlayerEntity().getPlayerListName()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("nameplate")) {
        return new Element(NMSHandler.getInstance().getProfileEditor().getPlayerName(getPlayerEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("name")) {
        return new Element(getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_finished")) {
        dScript script = dScript.valueOf(attribute.getContext(1));
        if (script == null) {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
        return new Element(FinishCommand.getScriptCompletes(getName(), script.getName()) > 0).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_failed")) {
        dScript script = dScript.valueOf(attribute.getContext(1));
        if (script == null) {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
        return new Element(FailCommand.getScriptFails(getName(), script.getName()) > 0).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("compass_target")) {
        Location target = getPlayerEntity().getCompassTarget();
        if (target != null) {
            return new dLocation(target).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("chunk_loaded") && attribute.hasContext(1)) {
        dChunk chunk = dChunk.valueOf(attribute.getContext(1));
        if (chunk == null) {
            return null;
        }
        return new Element(hasChunkLoaded(chunk.chunk)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("can_fly") || attribute.startsWith("allowed_flight")) {
        return new Element(getPlayerEntity().getAllowFlight()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("fly_speed")) {
        return new Element(getPlayerEntity().getFlySpeed()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("food_level.formatted")) {
        double maxHunger = getPlayerEntity().getMaxHealth();
        if (attribute.hasContext(2)) {
            maxHunger = attribute.getIntContext(2);
        }
        int foodLevel = getFoodLevel();
        if (foodLevel / maxHunger < .10) {
            return new Element("starving").getAttribute(attribute.fulfill(2));
        } else if (foodLevel / maxHunger < .40) {
            return new Element("famished").getAttribute(attribute.fulfill(2));
        } else if (foodLevel / maxHunger < .75) {
            return new Element("parched").getAttribute(attribute.fulfill(2));
        } else if (foodLevel / maxHunger < 1) {
            return new Element("hungry").getAttribute(attribute.fulfill(2));
        } else {
            return new Element("healthy").getAttribute(attribute.fulfill(2));
        }
    }
    // -->
    if (attribute.startsWith("saturation")) {
        return new Element(getPlayerEntity().getSaturation()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("food_level")) {
        return new Element(getFoodLevel()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("gamemode")) {
        attribute = attribute.fulfill(1);
        // -->
        if (attribute.startsWith("id")) {
            return new Element(getPlayerEntity().getGameMode().getValue()).getAttribute(attribute.fulfill(1));
        }
        return new Element(getPlayerEntity().getGameMode().name()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("is_blocking")) {
        return new Element(getPlayerEntity().isBlocking()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("ping")) {
        return new Element(NMSHandler.getInstance().getPlayerHelper().getPing(getPlayerEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_flying")) {
        return new Element(getPlayerEntity().isFlying()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_sleeping")) {
        return new Element(getPlayerEntity().isSleeping()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_sneaking")) {
        return new Element(getPlayerEntity().isSneaking()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_sprinting")) {
        return new Element(getPlayerEntity().isSprinting()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_achievement")) {
        Achievement ach = Achievement.valueOf(attribute.getContext(1).toUpperCase());
        return new Element(getPlayerEntity().hasAchievement(ach)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("statistic")) {
        Statistic statistic = Statistic.valueOf(attribute.getContext(1).toUpperCase());
        if (statistic == null) {
            return null;
        }
        // -->
        if (attribute.getAttribute(2).startsWith("qualifier")) {
            dObject obj = ObjectFetcher.pickObjectFor(attribute.getContext(2));
            try {
                if (obj instanceof dMaterial) {
                    return new Element(getPlayerEntity().getStatistic(statistic, ((dMaterial) obj).getMaterial())).getAttribute(attribute.fulfill(2));
                } else if (obj instanceof dEntity) {
                    return new Element(getPlayerEntity().getStatistic(statistic, ((dEntity) obj).getBukkitEntityType())).getAttribute(attribute.fulfill(2));
                } else {
                    return null;
                }
            } catch (Exception e) {
                dB.echoError("Invalid statistic: " + statistic + " for this player!");
                return null;
            }
        }
        try {
            return new Element(getPlayerEntity().getStatistic(statistic)).getAttribute(attribute.fulfill(1));
        } catch (Exception e) {
            dB.echoError("Invalid statistic: " + statistic + " for this player!");
            return null;
        }
    }
    // -->
    if (attribute.startsWith("time_asleep")) {
        return new Duration(getPlayerEntity().getSleepTicks() / 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("time")) {
        return new Element(getPlayerEntity().getPlayerTime()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("walk_speed")) {
        return new Element(getPlayerEntity().getWalkSpeed()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("weather")) {
        if (getPlayerEntity().getPlayerWeather() != null) {
            return new Element(getPlayerEntity().getPlayerWeather().name()).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("xp.level")) {
        return new Element(getPlayerEntity().getLevel()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("xp.to_next_level")) {
        return new Element(getPlayerEntity().getExpToLevel()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("xp.total")) {
        return new Element(getPlayerEntity().getTotalExperience()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("xp")) {
        return new Element(getPlayerEntity().getExp() * 100).getAttribute(attribute.fulfill(1));
    }
    if (Depends.chat != null) {
        // -->
        if (attribute.startsWith("chat_prefix")) {
            String prefix = Depends.chat.getPlayerPrefix(getWorld().getName(), getOfflinePlayer());
            if (prefix == null) {
                return null;
            }
            return new Element(prefix).getAttribute(attribute.fulfill(1));
        } else // -->
        if (attribute.startsWith("chat_suffix")) {
            String suffix = Depends.chat.getPlayerSuffix(getWorld().getName(), getOfflinePlayer());
            if (suffix == null) {
                return null;
            }
            return new Element(suffix).getAttribute(attribute.fulfill(1));
        }
    }
    // Iterate through this object's properties' attributes
    for (Property property : PropertyParser.getProperties(this)) {
        String returned = property.getAttribute(attribute);
        if (returned != null) {
            return returned;
        }
    }
    return new dEntity(getPlayerEntity()).getAttribute(attribute);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) BlockIterator(org.bukkit.util.BlockIterator) PotionEffect(org.bukkit.potion.PotionEffect) ArrayList(java.util.ArrayList) FlagManager(net.aufdemrand.denizen.flags.FlagManager) LivingEntity(org.bukkit.entity.LivingEntity) ImprovedOfflinePlayer(net.aufdemrand.denizen.nms.abstracts.ImprovedOfflinePlayer) Property(net.aufdemrand.denizencore.objects.properties.Property) Pattern(java.util.regex.Pattern) Player(org.bukkit.entity.Player) ImprovedOfflinePlayer(net.aufdemrand.denizen.nms.abstracts.ImprovedOfflinePlayer) Date(java.util.Date) Block(org.bukkit.block.Block) Sidebar(net.aufdemrand.denizen.nms.abstracts.Sidebar)

Example 13 with BlockIterator

use of org.bukkit.util.BlockIterator in project Denizen-For-Bukkit by DenizenScript.

the class dLocation method getAttribute.

@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // -->
    if (attribute.startsWith("above")) {
        return new dLocation(this.clone().add(0, 1, 0)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("below")) {
        return new dLocation(this.clone().add(0, -1, 0)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("block")) {
        return new dLocation(getWorld(), getBlockX(), getBlockY(), getBlockZ()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("center")) {
        return new dLocation(getWorld(), getBlockX() + 0.5, getBlockY() + 0.5, getBlockZ() + 0.5).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("highest")) {
        return new dLocation(getWorld().getHighestBlockAt(this).getLocation().add(0, -1, 0)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("base_color")) {
        DyeColor color = ((Banner) getBlock().getState()).getBaseColor();
        return new Element(color != null ? color.name() : "BLACK").getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_inventory")) {
        return new Element(getBlock().getState() instanceof InventoryHolder).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("inventory")) {
        dObject obj = Element.handleNull(identify() + ".inventory", getInventory(), "dInventory", attribute.hasAlternative());
        return obj == null ? null : obj.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("material")) {
        return dMaterial.getMaterialFrom(getBlock().getType(), getBlock().getData()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("patterns")) {
        dList list = new dList();
        for (org.bukkit.block.banner.Pattern pattern : ((Banner) getBlock().getState()).getPatterns()) {
            list.add(pattern.getColor().name() + "/" + pattern.getPattern().name());
        }
        return list.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("head_rotation")) {
        return new Element(getSkullRotation(((Skull) getBlock().getState()).getRotation()) + 1).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("switched")) {
        Material type = getBlock().getType();
        if (type == Material.IRON_DOOR_BLOCK || type == Material.WOODEN_DOOR || type == Material.DARK_OAK_DOOR || type == Material.BIRCH_DOOR || type == Material.ACACIA_DOOR || type == Material.JUNGLE_DOOR || type == Material.SPRUCE_DOOR) {
            Location location = this;
            int data = getBlock().getData();
            if (data >= 8) {
                location = clone().add(0, -1, 0);
            }
            return new Element((location.getBlock().getData() & 0x4) > 0).getAttribute(attribute.fulfill(1));
        } else if (type == Material.TRAP_DOOR || type == Material.IRON_TRAPDOOR) {
            return new Element((getBlock().getData() & 0x4) > 0).getAttribute(attribute.fulfill(1));
        } else {
            return new Element((getBlock().getData() & 0x8) > 0).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("sign_contents")) {
        if (getBlock().getState() instanceof Sign) {
            return new dList(Arrays.asList(((Sign) getBlock().getState()).getLines())).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("spawner_type")) {
        if (getBlock().getState() instanceof CreatureSpawner) {
            return new dEntity(DenizenEntityType.getByName(((CreatureSpawner) getBlock().getState()).getSpawnedType().name())).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("drops")) {
        Collection<ItemStack> its = getBlock().getDrops();
        dList list = new dList();
        for (ItemStack it : its) {
            list.add(new dItem(it).identify());
        }
        return list.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("skull_type")) {
        BlockState blockState = getBlock().getState();
        if (blockState instanceof Skull) {
            String t = ((Skull) blockState).getSkullType().name();
            return new Element(t).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("skull_name")) {
        BlockState blockState = getBlock().getState();
        if (blockState instanceof Skull) {
            PlayerProfile profile = NMSHandler.getInstance().getBlockHelper().getPlayerProfile((Skull) blockState);
            String n = profile.getName();
            if (n == null) {
                n = ((Skull) blockState).getOwningPlayer().getName();
            }
            return new Element(n).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("skull_skin")) {
        BlockState blockState = getBlock().getState();
        if (blockState instanceof Skull) {
            PlayerProfile profile = NMSHandler.getInstance().getBlockHelper().getPlayerProfile((Skull) blockState);
            String name = profile.getName();
            UUID uuid = profile.getUniqueId();
            String texture = profile.getTexture();
            attribute = attribute.fulfill(1);
            // -->
            if (attribute.startsWith("full")) {
                return new Element((uuid != null ? uuid : name != null ? name : null) + (texture != null ? "|" + texture : "")).getAttribute(attribute.fulfill(1));
            }
            return new Element(uuid != null ? uuid.toString() : name != null ? name : null).getAttribute(attribute);
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("simple.formatted")) {
        return new Element("X '" + getBlockX() + "', Y '" + getBlockY() + "', Z '" + getBlockZ() + "', in world '" + getWorld().getName() + "'").getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("simple")) {
        if (getWorld() == null) {
            return new Element(getBlockX() + "," + getBlockY() + "," + getBlockZ()).getAttribute(attribute.fulfill(1));
        } else {
            return new Element(getBlockX() + "," + getBlockY() + "," + getBlockZ() + "," + getWorld().getName()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("precise_impact_normal")) {
        int range = attribute.getIntContext(1);
        if (range < 1) {
            range = 200;
        }
        double xzLen = Math.cos((getPitch() % 360) * (Math.PI / 180));
        double nx = xzLen * Math.sin(-getYaw() * (Math.PI / 180));
        double ny = Math.sin(getPitch() * (Math.PI / 180));
        double nz = xzLen * Math.cos(getYaw() * (Math.PI / 180));
        Location location = NMSHandler.getInstance().getEntityHelper().getImpactNormal(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
        if (location != null) {
            return new dLocation(location).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("precise_cursor_on")) {
        int range = attribute.getIntContext(1);
        if (range < 1) {
            range = 200;
        }
        double xzLen = Math.cos((getPitch() % 360) * (Math.PI / 180));
        double nx = xzLen * Math.sin(-getYaw() * (Math.PI / 180));
        double ny = Math.sin(getPitch() * (Math.PI / 180));
        double nz = xzLen * Math.cos(getYaw() * (Math.PI / 180));
        Location location = NMSHandler.getInstance().getEntityHelper().rayTrace(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
        if (location != null) {
            return new dLocation(location).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("points_between")) {
        dLocation target = dLocation.valueOf(attribute.getContext(1));
        if (target == null) {
            return null;
        }
        attribute = attribute.fulfill(1);
        // <--[tag]
        // @attribute <l@location.points_between[<location>].distance[<#.#>]>
        // @returns dList(dLocation)
        // @description
        // Finds all locations between this location and another, separated by the specified distance each.
        // -->
        double rad = 1d;
        if (attribute.startsWith("distance")) {
            rad = attribute.getDoubleContext(1);
            attribute = attribute.fulfill(1);
        }
        dList list = new dList();
        org.bukkit.util.Vector rel = target.toVector().subtract(this.toVector());
        double len = rel.length();
        rel = rel.multiply(1d / len);
        for (double i = 0d; i < len; i += rad) {
            list.add(new dLocation(this.clone().add(rel.clone().multiply(i))).identify());
        }
        return list.getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("facing_blocks")) {
        int range = attribute.getIntContext(1);
        if (range < 1) {
            range = 100;
        }
        dList list = new dList();
        BlockIterator iterator = new BlockIterator(this, 0, range);
        while (iterator.hasNext()) {
            list.add(new dLocation(iterator.next().getLocation()).identify());
        }
        return list.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("line_of_sight") && attribute.hasContext(1)) {
        dLocation location = dLocation.valueOf(attribute.getContext(1));
        if (location != null) {
            return new Element(NMSHandler.getInstance().getEntityHelper().canTrace(getWorld(), toVector(), location.toVector())).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("direction.vector")) {
        double xzLen = Math.cos((getPitch() % 360) * (Math.PI / 180));
        double nx = xzLen * Math.sin(-getYaw() * (Math.PI / 180));
        double ny = Math.sin(getPitch() * (Math.PI / 180));
        double nz = xzLen * Math.cos(getYaw() * (Math.PI / 180));
        return new dLocation(getWorld(), nx, -ny, nz).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("direction")) {
        // Get the cardinal direction from this location to another
        if (attribute.hasContext(1) && dLocation.matches(attribute.getContext(1))) {
            // Subtract this location's vector from the other location's vector,
            // not the other way around
            dLocation target = dLocation.valueOf(attribute.getContext(1));
            attribute = attribute.fulfill(1);
            EntityHelper entityHelper = NMSHandler.getInstance().getEntityHelper();
            // -->
            if (attribute.startsWith("yaw")) {
                return new Element(entityHelper.normalizeYaw(entityHelper.getYaw(target.toVector().subtract(this.toVector()).normalize()))).getAttribute(attribute.fulfill(1));
            } else {
                return new Element(entityHelper.getCardinal(entityHelper.getYaw(target.toVector().subtract(this.toVector()).normalize()))).getAttribute(attribute);
            }
        } else // Get a cardinal direction from this location's yaw
        {
            return new Element(NMSHandler.getInstance().getEntityHelper().getCardinal(getYaw())).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("face") && attribute.hasContext(1)) {
        Location two = dLocation.valueOf(attribute.getContext(1));
        return new dLocation(NMSHandler.getInstance().getEntityHelper().faceLocation(this, two)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("facing")) {
        if (attribute.hasContext(1)) {
            // The default number of degrees if there is no degrees attribute
            int degrees = 45;
            // The attribute to fulfill from
            int attributePos = 1;
            // -->
            if (attribute.getAttribute(2).startsWith("degrees") && attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) {
                degrees = attribute.getIntContext(2);
                attributePos++;
            }
            if (dLocation.matches(attribute.getContext(1))) {
                return new Element(NMSHandler.getInstance().getEntityHelper().isFacingLocation(this, dLocation.valueOf(attribute.getContext(1)), degrees)).getAttribute(attribute.fulfill(attributePos));
            } else if (dEntity.matches(attribute.getContext(1))) {
                return new Element(NMSHandler.getInstance().getEntityHelper().isFacingLocation(this, dEntity.valueOf(attribute.getContext(1)).getBukkitEntity().getLocation(), degrees)).getAttribute(attribute.fulfill(attributePos));
            }
        }
    }
    // -->
    if (attribute.startsWith("pitch")) {
        return new Element(getPitch()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("with_pose")) {
        String context = attribute.getContext(1);
        Float pitch = 0f;
        Float yaw = 0f;
        if (dEntity.matches(context)) {
            dEntity ent = dEntity.valueOf(context);
            if (ent.isSpawned()) {
                pitch = ent.getBukkitEntity().getLocation().getPitch();
                yaw = ent.getBukkitEntity().getLocation().getYaw();
            }
        } else if (context.split(",").length == 2) {
            String[] split = context.split(",");
            pitch = Float.valueOf(split[0]);
            yaw = Float.valueOf(split[1]);
        }
        dLocation loc = dLocation.valueOf(identify());
        loc.setPitch(pitch);
        loc.setYaw(yaw);
        return loc.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("yaw.simple")) {
        float yaw = NMSHandler.getInstance().getEntityHelper().normalizeYaw(getYaw());
        if (yaw < 45) {
            return new Element("South").getAttribute(attribute.fulfill(2));
        } else if (yaw < 135) {
            return new Element("West").getAttribute(attribute.fulfill(2));
        } else if (yaw < 225) {
            return new Element("North").getAttribute(attribute.fulfill(2));
        } else if (yaw < 315) {
            return new Element("East").getAttribute(attribute.fulfill(2));
        } else {
            return new Element("South").getAttribute(attribute.fulfill(2));
        }
    }
    // -->
    if (attribute.startsWith("yaw.raw")) {
        return new Element(getYaw()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("yaw")) {
        return new Element(NMSHandler.getInstance().getEntityHelper().normalizeYaw(getYaw())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("rotate_around_x") && attribute.hasContext(1)) {
        double angle = attribute.getDoubleContext(1);
        double cos = Math.cos(angle);
        double sin = Math.sin(angle);
        double y = (getY() * cos) - (getZ() * sin);
        double z = (getY() * sin) + (getZ() * cos);
        Location location = clone();
        location.setY(y);
        location.setZ(z);
        return new dLocation(location).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("rotate_around_y") && attribute.hasContext(1)) {
        double angle = attribute.getDoubleContext(1);
        double cos = Math.cos(angle);
        double sin = Math.sin(angle);
        double x = (getX() * cos) + (getZ() * sin);
        double z = (getX() * -sin) + (getZ() * cos);
        Location location = clone();
        location.setX(x);
        location.setZ(z);
        return new dLocation(location).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("rotate_around_z") && attribute.hasContext(1)) {
        double angle = attribute.getDoubleContext(1);
        double cos = Math.cos(angle);
        double sin = Math.sin(angle);
        double x = (getX() * cos) - (getY() * sin);
        double y = (getZ() * sin) + (getY() * cos);
        Location location = clone();
        location.setX(x);
        location.setY(y);
        return new dLocation(location).getAttribute(attribute.fulfill(1));
    }
    if (attribute.matches("find") || attribute.startsWith("nearest")) {
        attribute.fulfill(1);
        // -->
        if (attribute.startsWith("blocks") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) {
            ArrayList<dLocation> found = new ArrayList<dLocation>();
            int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10;
            List<dMaterial> materials = new ArrayList<dMaterial>();
            if (attribute.hasContext(1)) {
                materials = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);
            }
            // Avoid NPE from invalid materials
            if (materials == null) {
                return null;
            }
            int max = Settings.blockTagsMaxBlocks();
            int index = 0;
            attribute.fulfill(2);
            Location tstart = getBlock().getLocation();
            double tstartY = tstart.getY();
            fullloop: for (int x = -(radius); x <= radius; x++) {
                for (int y = -(radius); y <= radius; y++) {
                    double newY = y + tstartY;
                    if (newY < 0 || newY > 255) {
                        continue;
                    }
                    for (int z = -(radius); z <= radius; z++) {
                        index++;
                        if (index > max) {
                            break fullloop;
                        }
                        if (Utilities.checkLocation(this, tstart.clone().add(x + 0.5, y + 0.5, z + 0.5), radius)) {
                            if (!materials.isEmpty()) {
                                for (dMaterial material : materials) {
                                    if (material.hasData() && material.getData() != 0) {
                                        // TODO: less arbitrary matching
                                        if (material.matchesMaterialData(tstart.clone().add(x, y, z).getBlock().getState().getData())) {
                                            found.add(new dLocation(tstart.clone().add(x, y, z)));
                                        }
                                    } else if (material.getMaterial() == tstart.clone().add(x, y, z).getBlock().getType()) {
                                        found.add(new dLocation(tstart.clone().add(x, y, z)));
                                    }
                                }
                            } else {
                                found.add(new dLocation(tstart.clone().add(x, y, z)));
                            }
                        }
                    }
                }
            }
            Collections.sort(found, new Comparator<dLocation>() {

                @Override
                public int compare(dLocation loc1, dLocation loc2) {
                    return dLocation.this.compare(loc1, loc2);
                }
            });
            return new dList(found).getAttribute(attribute);
        } else // -->
        if (attribute.startsWith("surface_blocks") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) {
            ArrayList<dLocation> found = new ArrayList<dLocation>();
            double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
            List<dMaterial> materials = new ArrayList<dMaterial>();
            if (attribute.hasContext(1)) {
                materials = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);
            }
            // Avoid NPE from invalid materials
            if (materials == null) {
                return null;
            }
            int max = Settings.blockTagsMaxBlocks();
            int index = 0;
            attribute.fulfill(2);
            Location loc = getBlock().getLocation().add(0.5f, 0.5f, 0.5f);
            fullloop: for (double x = -(radius); x <= radius; x++) {
                for (double y = -(radius); y <= radius; y++) {
                    for (double z = -(radius); z <= radius; z++) {
                        index++;
                        if (index > max) {
                            break fullloop;
                        }
                        if (Utilities.checkLocation(loc, getBlock().getLocation().add(x + 0.5, y + 0.5, z + 0.5), radius)) {
                            Location l = getBlock().getLocation().clone().add(x, y, z);
                            if (!materials.isEmpty()) {
                                for (dMaterial material : materials) {
                                    if (material.matchesMaterialData(getBlock().getLocation().clone().add(x, y, z).getBlock().getType().getNewData(getBlock().getLocation().clone().add(x, y, z).getBlock().getData()))) {
                                        if (l.clone().add(0, 1, 0).getBlock().getType() == Material.AIR && l.clone().add(0, 2, 0).getBlock().getType() == Material.AIR && l.getBlock().getType() != Material.AIR) {
                                            found.add(new dLocation(getBlock().getLocation().clone().add(x + 0.5, y, z + 0.5)));
                                        }
                                    }
                                }
                            } else {
                                if (l.clone().add(0, 1, 0).getBlock().getType() == Material.AIR && l.clone().add(0, 2, 0).getBlock().getType() == Material.AIR && l.getBlock().getType() != Material.AIR) {
                                    found.add(new dLocation(getBlock().getLocation().clone().add(x + 0.5, y, z + 0.5)));
                                }
                            }
                        }
                    }
                }
            }
            Collections.sort(found, new Comparator<dLocation>() {

                @Override
                public int compare(dLocation loc1, dLocation loc2) {
                    return dLocation.this.compare(loc1, loc2);
                }
            });
            return new dList(found).getAttribute(attribute);
        } else // -->
        if (attribute.startsWith("players") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) {
            ArrayList<dPlayer> found = new ArrayList<dPlayer>();
            double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
            attribute.fulfill(2);
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (!player.isDead() && Utilities.checkLocation(this, player.getLocation(), radius)) {
                    found.add(new dPlayer(player));
                }
            }
            Collections.sort(found, new Comparator<dPlayer>() {

                @Override
                public int compare(dPlayer pl1, dPlayer pl2) {
                    return dLocation.this.compare(pl1.getLocation(), pl2.getLocation());
                }
            });
            return new dList(found).getAttribute(attribute);
        } else // -->
        if (attribute.startsWith("npcs") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) {
            ArrayList<dNPC> found = new ArrayList<dNPC>();
            double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
            attribute.fulfill(2);
            for (dNPC npc : DenizenAPI.getSpawnedNPCs()) {
                if (Utilities.checkLocation(this.getBlock().getLocation(), npc.getLocation(), radius)) {
                    found.add(npc);
                }
            }
            Collections.sort(found, new Comparator<dNPC>() {

                @Override
                public int compare(dNPC npc1, dNPC npc2) {
                    return dLocation.this.compare(npc1.getLocation(), npc2.getLocation());
                }
            });
            return new dList(found).getAttribute(attribute);
        } else // -->
        if (attribute.startsWith("entities") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) {
            dList ent_list = new dList();
            if (attribute.hasContext(1)) {
                ent_list = dList.valueOf(attribute.getContext(1));
            }
            ArrayList<dEntity> found = new ArrayList<dEntity>();
            double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
            attribute.fulfill(2);
            for (Entity entity : getWorld().getEntities()) {
                if (Utilities.checkLocation(this, entity.getLocation(), radius)) {
                    dEntity current = new dEntity(entity);
                    if (!ent_list.isEmpty()) {
                        for (String ent : ent_list) {
                            if (current.comparedTo(ent)) {
                                found.add(current);
                                break;
                            }
                        }
                    } else {
                        found.add(current);
                    }
                }
            }
            Collections.sort(found, new Comparator<dEntity>() {

                @Override
                public int compare(dEntity ent1, dEntity ent2) {
                    return dLocation.this.compare(ent1.getLocation(), ent2.getLocation());
                }
            });
            return new dList(found).getAttribute(attribute);
        } else // -->
        if (attribute.startsWith("living_entities") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) {
            ArrayList<dEntity> found = new ArrayList<dEntity>();
            double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
            attribute.fulfill(2);
            for (Entity entity : getWorld().getEntities()) {
                if (entity instanceof LivingEntity && Utilities.checkLocation(this, entity.getLocation(), radius)) {
                    found.add(new dEntity(entity));
                }
            }
            Collections.sort(found, new Comparator<dEntity>() {

                @Override
                public int compare(dEntity ent1, dEntity ent2) {
                    return dLocation.this.compare(ent1.getLocation(), ent2.getLocation());
                }
            });
            return new dList(found).getAttribute(attribute);
        }
    }
    // -->
    if (attribute.startsWith("find_path") && attribute.hasContext(1)) {
        dLocation two = dLocation.valueOf(attribute.getContext(1));
        if (two == null) {
            return null;
        }
        List<dLocation> locs = PathFinder.getPath(this, two);
        dList list = new dList();
        for (dLocation loc : locs) {
            list.add(loc.identify());
        }
        return list.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("formatted.citizens")) {
        return new Element(getX() + ":" + getY() + ":" + getZ() + ":" + getWorld().getName()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("formatted")) {
        return new Element("X '" + getX() + "', Y '" + getY() + "', Z '" + getZ() + "', in world '" + getWorld().getName() + "'").getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("chunk") || attribute.startsWith("get_chunk")) {
        return new dChunk(this).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("raw")) {
        dLocation rawLocation = new dLocation(this);
        rawLocation.setRaw(true);
        return rawLocation.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("world")) {
        return dWorld.mirrorBukkitWorld(getWorld()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("x")) {
        return new Element(getX()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("y")) {
        return new Element(getY()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("z")) {
        return new Element(getZ()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("notable_name")) {
        String notname = NotableManager.getSavedId(this);
        if (notname == null) {
            return null;
        }
        return new Element(notname).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("add") && attribute.hasContext(1)) {
        // TODO: Just dLocation.valueOf?
        String[] ints = attribute.getContext(1).replace("l@", "").split(",", 4);
        if (ints.length >= 3) {
            if ((aH.matchesDouble(ints[0]) || aH.matchesInteger(ints[0])) && (aH.matchesDouble(ints[1]) || aH.matchesInteger(ints[1])) && (aH.matchesDouble(ints[2]) || aH.matchesInteger(ints[2]))) {
                return new dLocation(this.clone().add(Double.valueOf(ints[0]), Double.valueOf(ints[1]), Double.valueOf(ints[2]))).getAttribute(attribute.fulfill(1));
            }
        } else if (dLocation.matches(attribute.getContext(1))) {
            return new dLocation(this.clone().add(dLocation.valueOf(attribute.getContext(1)))).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("sub") && attribute.hasContext(1)) {
        // TODO: Just dLocation.valueOf?
        String[] ints = attribute.getContext(1).replace("l@", "").split(",", 4);
        if (ints.length == 3 || ints.length == 4) {
            if ((aH.matchesDouble(ints[0]) || aH.matchesInteger(ints[0])) && (aH.matchesDouble(ints[1]) || aH.matchesInteger(ints[1])) && (aH.matchesDouble(ints[2]) || aH.matchesInteger(ints[2]))) {
                return new dLocation(this.clone().subtract(Double.valueOf(ints[0]), Double.valueOf(ints[1]), Double.valueOf(ints[2]))).getAttribute(attribute.fulfill(1));
            }
        } else if (dLocation.matches(attribute.getContext(1))) {
            return new dLocation(this.clone().subtract(dLocation.valueOf(attribute.getContext(1)))).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("mul") && attribute.hasContext(1)) {
        return new dLocation(this.clone().multiply(Double.parseDouble(attribute.getContext(1)))).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("div") && attribute.hasContext(1)) {
        return new dLocation(this.clone().multiply(1D / Double.parseDouble(attribute.getContext(1)))).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("normalize")) {
        double len = Math.sqrt(Math.pow(getX(), 2) + Math.pow(getY(), 2) + Math.pow(getZ(), 2));
        if (len == 0) {
            return this.getAttribute(attribute.fulfill(1));
        } else {
            return new dLocation(this.clone().multiply(1D / len)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("vector_length")) {
        return new Element(Math.sqrt(Math.pow(getX(), 2) + Math.pow(getY(), 2) + Math.pow(getZ(), 2))).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("distance_squared") && attribute.hasContext(1)) {
        if (dLocation.matches(attribute.getContext(1))) {
            dLocation toLocation = dLocation.valueOf(attribute.getContext(1));
            if (!getWorld().getName().equalsIgnoreCase(toLocation.getWorld().getName())) {
                if (!attribute.hasAlternative()) {
                    dB.echoError("Can't measure distance between two different worlds!");
                }
                return null;
            }
            return new Element(this.distanceSquared(toLocation)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("distance") && attribute.hasContext(1)) {
        if (dLocation.matches(attribute.getContext(1))) {
            dLocation toLocation = dLocation.valueOf(attribute.getContext(1));
            // -->
            if (attribute.getAttribute(2).startsWith("horizontal")) {
                // -->
                if (attribute.getAttribute(3).startsWith("multiworld")) {
                    return new Element(Math.sqrt(Math.pow(this.getX() - toLocation.getX(), 2) + Math.pow(this.getZ() - toLocation.getZ(), 2))).getAttribute(attribute.fulfill(3));
                } else if (this.getWorld() == toLocation.getWorld()) {
                    return new Element(Math.sqrt(Math.pow(this.getX() - toLocation.getX(), 2) + Math.pow(this.getZ() - toLocation.getZ(), 2))).getAttribute(attribute.fulfill(2));
                }
            } else // -->
            if (attribute.getAttribute(2).startsWith("vertical")) {
                // -->
                if (attribute.getAttribute(3).startsWith("multiworld")) {
                    return new Element(Math.abs(this.getY() - toLocation.getY())).getAttribute(attribute.fulfill(3));
                } else if (this.getWorld() == toLocation.getWorld()) {
                    return new Element(Math.abs(this.getY() - toLocation.getY())).getAttribute(attribute.fulfill(2));
                }
            }
            if (!getWorld().getName().equalsIgnoreCase(toLocation.getWorld().getName())) {
                if (!attribute.hasAlternative()) {
                    dB.echoError("Can't measure distance between two different worlds!");
                }
                return null;
            } else {
                return new Element(this.distance(toLocation)).getAttribute(attribute.fulfill(1));
            }
        }
    }
    // -->
    if (attribute.startsWith("is_within") && attribute.hasContext(1)) {
        if (dEllipsoid.matches(attribute.getContext(1))) {
            dEllipsoid ellipsoid = dEllipsoid.valueOf(attribute.getContext(1));
            if (ellipsoid != null) {
                return new Element(ellipsoid.contains(this)).getAttribute(attribute.fulfill(1));
            }
        } else {
            dCuboid cuboid = dCuboid.valueOf(attribute.getContext(1));
            if (cuboid != null) {
                return new Element(cuboid.isInsideCuboid(this)).getAttribute(attribute.fulfill(1));
            }
        }
    }
    // -->
    if (attribute.startsWith("biome.formatted")) {
        return new Element(CoreUtilities.toLowerCase(getBlock().getBiome().name()).replace('_', ' ')).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("biome")) {
        return new dBiome(getBlock().getBiome()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("cuboids")) {
        List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(this);
        dList cuboid_list = new dList();
        for (dCuboid cuboid : cuboids) {
            cuboid_list.add(cuboid.identify());
        }
        return cuboid_list.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("ellipsoids")) {
        List<dEllipsoid> ellipsoids = dEllipsoid.getNotableEllipsoidsContaining(this);
        dList ellipsoid_list = new dList();
        for (dEllipsoid ellipsoid : ellipsoids) {
            ellipsoid_list.add(ellipsoid.identify());
        }
        return ellipsoid_list.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_liquid")) {
        return new Element(getBlock().isLiquid()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("light.from_blocks") || attribute.startsWith("light.blocks")) {
        return new Element(getBlock().getLightFromBlocks()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("light.from_sky") || attribute.startsWith("light.sky")) {
        return new Element(getBlock().getLightFromSky()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("light")) {
        return new Element(getBlock().getLightLevel()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("power")) {
        return new Element(getBlock().getBlockPower()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("type")) {
        return new Element("Location").getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("command_block_name") && getBlock().getType() == Material.COMMAND) {
        return new Element(((CommandBlock) getBlock().getState()).getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("command_block") && getBlock().getType() == Material.COMMAND) {
        return new Element(((CommandBlock) getBlock().getState()).getCommand()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("furnace_burn_time")) {
        return new Element(((Furnace) getBlock().getState()).getBurnTime()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("furnace_cook_time")) {
        return new Element(((Furnace) getBlock().getState()).getCookTime()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("attached_to")) {
        BlockFace face = BlockFace.SELF;
        MaterialData data = getBlock().getState().getData();
        if (data instanceof Attachable) {
            face = ((Attachable) data).getAttachedFace();
        }
        if (face != BlockFace.SELF) {
            return new dLocation(getBlock().getRelative(face).getLocation()).getAttribute(attribute.fulfill(1));
        }
    }
    // Iterate through this object's properties' attributes
    for (Property property : PropertyParser.getProperties(this)) {
        String returned = property.getAttribute(attribute);
        if (returned != null) {
            return returned;
        }
    }
    return new Element(identify()).getAttribute(attribute);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Comparator(java.util.Comparator) Player(org.bukkit.entity.Player) PlayerProfile(net.aufdemrand.denizen.nms.util.PlayerProfile) DyeColor(org.bukkit.DyeColor) java.util(java.util) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack) BlockIterator(org.bukkit.util.BlockIterator) LivingEntity(org.bukkit.entity.LivingEntity) InventoryHolder(org.bukkit.inventory.InventoryHolder) Property(net.aufdemrand.denizencore.objects.properties.Property) EntityHelper(net.aufdemrand.denizen.nms.interfaces.EntityHelper) Material(org.bukkit.Material) Vector(org.bukkit.util.Vector) Attachable(org.bukkit.material.Attachable) Location(org.bukkit.Location)

Example 14 with BlockIterator

use of org.bukkit.util.BlockIterator in project Glowstone by GlowstoneMC.

the class ItemBucket method rightClickAir.

@Override
public void rightClickAir(GlowPlayer player, ItemStack holding) {
    Iterator<Block> itr = new BlockIterator(player, 5);
    Block target = null;
    // Used to determine the side the block was clicked on:
    Block previousTarget = null;
    BlockType targetBlockType = null;
    boolean validTarget = false;
    // Find the next available non-air liquid block type which is collectible in a radius of 5 blocks
    while (itr.hasNext()) {
        previousTarget = target;
        target = itr.next();
        targetBlockType = ItemTable.instance().getBlock(target.getType());
        if (targetBlockType instanceof BlockLiquid) {
            if (((BlockLiquid) targetBlockType).isCollectible((GlowBlockState) target.getState())) {
                validTarget = true;
                break;
            }
        }
    }
    if (target != null && validTarget) {
        // Get the direction of the bucket fill
        BlockFace face;
        if (previousTarget != null) {
            face = target.getFace(previousTarget);
        } else {
            face = BlockFace.SELF;
        }
        Material replaceWith = ((BlockLiquid) targetBlockType).getBucketType();
        PlayerBucketFillEvent event = EventFactory.callEvent(new PlayerBucketFillEvent(player, target, face, holding.getType(), holding));
        if (event.isCancelled()) {
            return;
        }
        if (player.getGameMode() != GameMode.CREATIVE) {
            if (holding.getAmount() == 1) {
                holding.setType(replaceWith);
            } else {
                holding.setAmount(holding.getAmount() - 1);
                player.getInventory().addItem(new ItemStack(replaceWith));
            }
        }
        target.setType(Material.AIR);
    }
}
Also used : BlockIterator(org.bukkit.util.BlockIterator) PlayerBucketFillEvent(org.bukkit.event.player.PlayerBucketFillEvent) BlockLiquid(net.glowstone.block.blocktype.BlockLiquid) BlockType(net.glowstone.block.blocktype.BlockType) BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack)

Example 15 with BlockIterator

use of org.bukkit.util.BlockIterator in project AreaShop by NLthijs48.

the class SignLinkerManager method onPlayerInteract.

/**
 * On player interactions.
 * @param event The PlayerInteractEvent
 */
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (isInSignLinkMode(event.getPlayer())) {
        event.setCancelled(true);
        Player player = event.getPlayer();
        SignLinker linker = signLinkers.get(event.getPlayer().getUniqueId());
        if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            // Get the region
            BlockIterator blockIterator = new BlockIterator(player, 100);
            while (blockIterator.hasNext()) {
                Block next = blockIterator.next();
                List<GeneralRegion> regions = Utils.getRegions(next.getLocation());
                if (regions.size() == 1) {
                    linker.setRegion(regions.get(0));
                    return;
                } else if (regions.size() > 1) {
                    Set<String> names = new HashSet<>();
                    for (GeneralRegion region : regions) {
                        names.add(region.getName());
                    }
                    plugin.message(player, "linksigns-multipleRegions", Utils.createCommaSeparatedList(names));
                    plugin.message(player, "linksigns-multipleRegionsAdvice");
                    return;
                }
            }
            // No regions found within the maximum range
            plugin.message(player, "linksigns-noRegions");
        } else if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
            Block block = null;
            BlockIterator blockIterator = new BlockIterator(player, 100);
            while (blockIterator.hasNext() && block == null) {
                Block next = blockIterator.next();
                if (next.getType() != Material.AIR) {
                    block = next;
                }
            }
            if (block == null || !(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) {
                plugin.message(player, "linksigns-noSign");
                return;
            }
            RegionSign regionSign = SignsFeature.getSignByLocation(block.getLocation());
            if (regionSign != null) {
                plugin.message(player, "linksigns-alreadyRegistered", regionSign.getRegion());
                return;
            }
            Sign sign = (Sign) block.getState().getData();
            linker.setSign(block.getLocation(), block.getType(), sign.getFacing());
        }
    }
}
Also used : BlockIterator(org.bukkit.util.BlockIterator) Player(org.bukkit.entity.Player) HashSet(java.util.HashSet) Set(java.util.Set) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) Block(org.bukkit.block.Block) Sign(org.bukkit.material.Sign) RegionSign(me.wiefferink.areashop.features.signs.RegionSign) RegionSign(me.wiefferink.areashop.features.signs.RegionSign) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BlockIterator (org.bukkit.util.BlockIterator)23 Block (org.bukkit.block.Block)20 Player (org.bukkit.entity.Player)10 Location (org.bukkit.Location)7 Material (org.bukkit.Material)6 EventHandler (org.bukkit.event.EventHandler)6 Entity (org.bukkit.entity.Entity)5 ItemStack (org.bukkit.inventory.ItemStack)5 ArrayList (java.util.ArrayList)4 ASkyBlock (com.wasteofplastic.acidisland.ASkyBlock)3 ASkyBlock (com.wasteofplastic.askyblock.ASkyBlock)3 RegionSign (me.wiefferink.areashop.features.signs.RegionSign)3 World (org.bukkit.World)3 HashMultiset (com.google.common.collect.HashMultiset)2 Multiset (com.google.common.collect.Multiset)2 Island (com.wasteofplastic.acidisland.Island)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileWriter (java.io.FileWriter)2 FilenameFilter (java.io.FilenameFilter)2