World guard API

Discussion in 'Plugin Development' started by CoolGamerXD, Jun 14, 2015.

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

    CoolGamerXD

    Hello! I have been developing a plugin. In which I need a world guard support. Basically what it is when we right click on block with gold nugget it will spawn a zombie. Zombie will break the block under to it. Now what I want is if zombie is under the block in which the world guard's flag "build" is deny it wont do anything to that block. I have tried alot but could not get the success. Here's the code.

    Code:
     
    public void onPlayerInteract(final PlayerInteractEvent e) {
           
       
           
            final ItemStack stack = new ItemStack(Material.GOLD_NUGGET);
            ItemMeta meta5 = stack.getItemMeta();
            meta5.setDisplayName(ChatColor.RED + "Buddy Spawner");
            stack.setItemMeta(meta5);
           
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (!(e.getPlayer().getInventory().getItemInHand().getItemMeta().hasDisplayName())) return;
            if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Buddy Spawner")) {
       
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "rg flag __GLOBAL__ mob-spawning -w " + e.getPlayer().getWorld().getName() + " allow");
            final Zombie s = e.getPlayer().getWorld().spawn(e.getPlayer().getLocation().add(1,0,0), Zombie.class);
            s.setCustomName(e.getPlayer().getName() + "'s" + " Buddy"); // Name will be like Ayush_03's Buddy.
            s.setCustomNameVisible(true); // Visibles the custom name.
           
            ItemStack Tier_1 = new ItemStack(Material.DIAMOND_PICKAXE);
            Tier_1.addUnsafeEnchantment(Enchantment.DIG_SPEED, 1000); // Efficiency level 1000.
            Tier_1.addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 1000);
            Tier_1.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
            ItemMeta meta1 = Tier_1.getItemMeta();
            meta1.setDisplayName(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "Porotic Pick");
            Tier_1.setItemMeta(meta1);
            e.getPlayer().getInventory().removeItem(stack);
            e.getPlayer().updateInventory();
            s.getEquipment().setItemInHand(new ItemStack(Tier_1));
            s.getEquipment().setItemInHandDropChance(100F); // 100% chance to drop the pick_axe.
            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "rg flag __GLOBAL__ mob-spawning -w " + e.getPlayer().getWorld().getName() + " deny");
           
            final Player p = e.getPlayer();
           
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
               scheduler.scheduleSyncRepeatingTask(this, new Runnable() { // Repeats every second.
                    @Override
                  public void run() {
                       
                        WorldGuardPlugin wg = (WorldGuardPlugin)Bukkit.getServer().getPluginManager().getPlugin("WorldGuard");
                           
                        if (wg.canBuild(p, s.getLocation().getBlock().getRelative(BlockFace.DOWN))) {
                             
                      if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.IRON_ORE) { // Gets every block under the zombie.
                         
                           
                          s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                          s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear(); 
                         
                          PlayerInventory pi = e.getPlayer().getInventory();
                          pi.addItem(new ItemStack(Material.IRON_INGOT, 201 )); // Adds the drop to inventory.
                         
                         
                          } else {
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).setType(Material.IRON_ORE);
                          }
                             
    
                         
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.COAL_ORE) {
                             
                              PlayerInventory pi = e.getPlayer().getInventory();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                             
                             
                              pi.addItem(new ItemStack(Material.COAL, 201));
                             
                              } 
                             
                         
                         
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.DIAMOND_BLOCK) {
                             
                              PlayerInventory pi = e.getPlayer().getInventory();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                             
                             
                              pi.addItem(new ItemStack(Material.DIAMOND_BLOCK, 201));
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                              } 
                             
                         
                     
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.GOLD_BLOCK) {
                              PlayerInventory pi = e.getPlayer().getInventory();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                             
                             
                              pi.addItem(new ItemStack(Material.GOLD_BLOCK, 201));
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                          }
                         
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.QUARTZ_BLOCK) {
                              PlayerInventory pi = e.getPlayer().getInventory();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                             
                             
                              pi.addItem(new ItemStack(Material.QUARTZ_BLOCK, 201));
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                          }
                         
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.LAPIS_BLOCK) {
                              PlayerInventory pi = e.getPlayer().getInventory();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                             
                              pi.addItem(new ItemStack(Material.LAPIS_BLOCK, 201));
                             
                          }
                         
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.EMERALD_BLOCK) {
                              PlayerInventory pi = e.getPlayer().getInventory();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                             
                              pi.addItem(new ItemStack(Material.EMERALD_BLOCK, 201));
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                          }
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.IRON_BLOCK) {
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                              PlayerInventory pi = e.getPlayer().getInventory();
                             
                              pi.addItem(new ItemStack(Material.IRON_BLOCK, 201));
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                          }
                         
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.NETHERRACK) {
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                              PlayerInventory pi = e.getPlayer().getInventory();
                             
                              pi.addItem(new ItemStack(Material.NETHERRACK, 201));
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                         
                          }
                         
                          if (s.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.DIAMOND_ORE) {
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).breakNaturally();
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getDrops().clear();
                              PlayerInventory pi = e.getPlayer().getInventory();
                             
                              pi.addItem(new ItemStack(Material.DIAMOND, 201));
                              s.getLocation().getBlock().getRelative(BlockFace.DOWN).getState();
                   
                          } 
                             
                   
                          }
                        }
                   
         }, 1L * 20L, 0); // Runs every second.
            }
        }
           
       @EventHandler
        public void onPlayerDamage(EntityDamageEvent e) {
           
            if(!(e.getEntity() instanceof Player)) return;
            EntityDamageEvent.DamageCause cause = e.getCause();
            if (cause == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
                     
                e.setCancelled(true);           
    }
       }
        }
     
  2. Offline

    NathanWolf

    Where in there are you checking for region build perms?

    The basic idea is to get the ApplicableRegionSet for the location, then use testState to see if the player has the BUILD flag.

    It is a little horrifying that you're dispatching commands to WG to set/unset flags! There is a setting in WG's configs that will let your plugin spawn mobs, I suggest you just set that.
     
  3. Offline

    CoolGamerXD

    Well for the thing you are saying horrifying is that I am making this plugin for my friend. He has prison server in which mob spawning is set to false. So I am just setting it to true for a moment to let mob spawn.

    Secondly, I want that so mob can break the block not for player.
     
Thread Status:
Not open for further replies.

Share This Page