Getting a block and removing it

Discussion in 'Plugin Development' started by GamerBah, Jul 25, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    GamerBah

    Hi there, I'm having some trouble figuring out a way to get the location of a block and then set it to nothing (the setting the block isn't the hard part, it's the getting of the blocks location that is an issue)

    I need to get the blocks d, dn, ds, de, dw, etc etc from the onClick event, and use them in the onInventoryClick method. I have no real clue how to do this, but I might be looking over a simple solution.

    Code:
    @EventHandler
        public void onClick(PlayerInteractEvent event) {
            if (event.getAction() != Action.LEFT_CLICK_BLOCK)
                return;
            if (event.getItem() == null)
                return;
            if (event.getItem().getType().equals(AIR))
                return;
    
            Player p = event.getPlayer();
            ItemStack inHand = p.getItemInHand();
    
            /*
             * if (inHand.getType().toString().toLowerCase().contains("sword") &&
             * inHand.getType().toString().toLowerCase().contains("axe") &&
             * inHand.getType().toString().toLowerCase().contains("spade") &&
             * inHand.getType().toString().toLowerCase().contains("helmet") &&
             * inHand.getType().toString().toLowerCase() .contains("chestplate") &&
             * inHand.getType().toString().toLowerCase().contains("tunic") &&
             * inHand.getType().toString().toLowerCase() .contains("leggings") &&
             * inHand.getType().toString().toLowerCase().contains("boots") &&
             * inHand.getType().toString().toLowerCase().contains("hoe") &&
             * inHand.getType().toString().toLowerCase().contains("bow") &&
             * inHand.getType().toString().toLowerCase().contains("book") &&
             * inHand.getType().toString().toLowerCase().contains("flint") &&
             * inHand.getType().toString().toLowerCase().contains("shears")) {
             */
    
            Block c = event.getClickedBlock();
            Block d = c.getRelative(DOWN);
            Block dn = d.getRelative(NORTH);
            Block ds = d.getRelative(SOUTH);
            Block de = d.getRelative(EAST);
            Block dw = d.getRelative(WEST);
            Block dnw = dn.getRelative(WEST);
            Block dne = dn.getRelative(EAST);
            Block dsw = ds.getRelative(WEST);
            Block dse = ds.getRelative(EAST);
            Block dd = d.getRelative(DOWN);
            Block ddn = dn.getRelative(DOWN);
            Block dds = ds.getRelative(DOWN);
            Block dde = de.getRelative(DOWN);
            Block ddw = dw.getRelative(DOWN);
            Block ddnw = dnw.getRelative(DOWN);
            Block ddne = dne.getRelative(DOWN);
            Block ddsw = dsw.getRelative(DOWN);
            Block ddse = dse.getRelative(DOWN);
    
            if (c.getType().equals(ENCHANTMENT_TABLE) && d.getType().equals(BEACON)
            /*
             * && dn.equals(REDSTONE_WIRE) && ds.equals(REDSTONE_WIRE) &&
             * dw.equals(REDSTONE_WIRE) && de.equals(REDSTONE_WIRE) &&
             * dnw.equals(REDSTONE_TORCH_ON) && dne.equals(REDSTONE_TORCH_ON) &&
             * dsw.equals(REDSTONE_TORCH_ON) && dse.equals(REDSTONE_TORCH_ON) &&
             * dd.equals(DIAMOND_BLOCK) && ddn.equals(GOLD_BLOCK) &&
             * dds.equals(GOLD_BLOCK) && ddw.equals(GOLD_BLOCK) &&
             * dde.equals(GOLD_BLOCK) && ddnw.equals(IRON_BLOCK) &&
             * ddne.equals(IRON_BLOCK) && ddsw.equals(IRON_BLOCK) &&
             * ddse.equals(IRON_BLOCK)
             */) {
                Inventory inv = Bukkit.getServer().createInventory(p, 27,
                        "Item Upgrade Altar");
                if(!(inHand.getEnchantments().size() > 0)) {
                    p.sendMessage(GRAY + "" + ITALIC + "A strange voice whispers to you: ");
                    p.sendMessage(GRAY + "" + ITALIC + "\"You must empower your weapon before placing it on this altar.\"");
                    p.playSound(p.getLocation(), Sound.AMBIENCE_CAVE, 1, 1);
                } else {
                    for (int i = 0; i <= formatEnchantments(inHand).size() - 1; i++) {
                        String enchantment = formatEnchantments(inHand).get(i);
                        List<String> lore = new ArrayList<String>();
                        int level = Integer.parseInt(enchantment.substring(enchantment.length() - 1, enchantment.length()));
                        String level10 = enchantment.substring(0, enchantment.length());
                        if(level10.contains("10")) {
                            lore.add(RED + "Upgrade Unavailable");
                            lore.add("");
                            lore.add(AQUA + "You've upgraded this");
                            lore.add(AQUA + "enchantment to the max!");
                            inv.setItem(11 + i, createBook(GOLD + enchantment.substring(0, enchantment.length() - 3) + " Upgrade", lore));
                        } else {
                            lore.add(0, WHITE + "Increases your " + AQUA + enchantment);
                            lore.add(1, WHITE + "to "+ AQUA + enchantment.substring(0, enchantment.length() - 2)
                                    + " " + (level + 1));
                            inv.setItem(11 + i, createBook(GREEN + enchantment.substring(0, enchantment.length() - 2) + " Upgrade", lore));
                        }
                    }
                   
                    ItemStack cancel = new ItemStack(BARRIER);
                    ItemMeta im = cancel.getItemMeta();
                    im.setDisplayName(RED + "Cancel Upgrade");
                    cancel.setItemMeta(im);
                   
                    inv.setItem(22, cancel);
                   
                    p.openInventory(inv);
                    loc = event.getClickedBlock().getLocation();
                }
            }
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if(e.getInventory().getName().equals("Item Upgrade Altar")) {
                Player p = (Player) e.getWhoClicked();
                if(e.getCurrentItem().getType().equals(ENCHANTED_BOOK)) {   
                    ItemStack inHand = p.getItemInHand();
                    ItemStack clickedBook = e.getCurrentItem();
                    String enchantment = clickedBook.getItemMeta().getDisplayName().substring(0,
                            clickedBook.getItemMeta().getDisplayName().length() - 8);
                    Enchantment ench = null;
                    if(enchantment.equals(GREEN + "Sharpness")) ench = DAMAGE_ALL;
                    if(enchantment.equals(GREEN + "Smite")) ench = DAMAGE_UNDEAD;
                    if(enchantment.equals(GREEN + "Bane of Arthropods")) ench = DAMAGE_ARTHROPODS;
                    if(enchantment.equals(GREEN + "Unbreaking")) ench = DURABILITY;
                    if(enchantment.equals(GREEN + "Fire Aspect")) ench = FIRE_ASPECT;
                    if(enchantment.equals(GREEN + "Knockback")) ench = KNOCKBACK;
                    if(enchantment.equals(GREEN + "Looting")) ench = LOOT_BONUS_MOBS;
                    if(enchantment.equals(GREEN + "Power")) ench = ARROW_DAMAGE;
                    if(enchantment.equals(GREEN + "Punch")) ench = ARROW_KNOCKBACK;
                    if(enchantment.equals(GREEN + "Flame")) ench = ARROW_FIRE;
                    if(enchantment.equals(GREEN + "Protection")) ench = PROTECTION_ENVIRONMENTAL;
                    if(enchantment.equals(GREEN + "Blast Protection")) ench = PROTECTION_EXPLOSIONS;
                    if(enchantment.equals(GREEN + "Feather Falling")) ench = PROTECTION_FALL;
                    if(enchantment.equals(GREEN + "Fire Protection")) ench = PROTECTION_FIRE;
                    if(enchantment.equals(GREEN + "Projectile Protection")) ench = PROTECTION_PROJECTILE;
                    if(enchantment.equals(GREEN + "Luck")) ench = LUCK;
                    if(enchantment.equals(GREEN + "Lure")) ench = LURE;
                    if(enchantment.equals(GREEN + "Thorns")) ench = THORNS;
                    if(enchantment.equals(GREEN + "Fortune")) ench = LOOT_BONUS_BLOCKS;
                    if(enchantment.equals(GREEN + "Depth Strider")) ench = DEPTH_STRIDER;
                    if(enchantment.equals(GREEN + "Respiration")) ench = OXYGEN;
                    if(enchantment.equals(GREEN + "Efficiency")) ench = DIG_SPEED;
                    if(clickedBook.getItemMeta().getDisplayName().contains(GOLD + "Upgrade") || ench == null) {
                        p.playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1, 1);
                        e.setCancelled(true);
                    }
                    inHand.addUnsafeEnchantment(ench, (inHand.getEnchantmentLevel(ench) + 1));
                    p.closeInventory();
                    Bukkit.getWorld(p.getWorld().getName()).strikeLightningEffect(loc);
                    p.playSound(p.getLocation(), Sound.AMBIENCE_THUNDER, 1, 1);
                }
               
                if(e.getCurrentItem().getType().equals(BARRIER)) {
                    p.closeInventory();
                }
                e.setCancelled(true);
            }
        }
    As always, help is much appreciated!!
     
  2. Offline

    DoggyCode™

    @GamerBah
    Code:
                Block b = event.getClickedBlock();
                Location loc = b.getLocation();
    
    ? is that something that you looked for?
     
  3. Offline

    GamerBah

    Using that in the onInventoryClick event isn't going to do anything, as it's not getting a block
     
  4. Offline

    DoggyCode™

    @GamerBah Ah, I thought you ment that you wanted the location out of the right-clicked block with Interact event. Hmm, try:
    Code:
    ItemStack itemStack = event.getCurrentItem();
    I know that we're not casting block, but a block that's not placed is a ItemStack. ^ So that should get the clicked item/block/itemstack...

    EDIT: And ItemStack doesn''t have a location, so why would you need the location of a item/block in the inventory? Do you mean like which slot it's in? I've got no clue.

    @GamerBah
     
  5. Offline

    GamerBah

    To explain better, the block I clicked in the onClick event needs to be used in the onInventoryClick event, but I have no way of getting it to the onInventoryClick
     
  6. Offline

    DoggyCode™

    @GamerBah Umm, I've got no clue how you can do this..
     
  7. Offline

    GamerBah

    Still looking for a solution
     
  8. Offline

    ark9026

    @GamerBah
    Store the data in a HashMap or similar?
     
Thread Status:
Not open for further replies.

Share This Page