Fortune Plugin + WorldGuard don't work together

Discussion in 'Plugin Development' started by andrew4543, May 30, 2014.

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

    andrew4543

    I made a fortune plugin for pickaxes and blocks, so that all blocks are effected by fortune. But WorldGaurd regions are ignored, any help that somebody knows?
    This is the code.
    Code:
    import java.util.Map;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.ExperienceOrb;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
     
    @eventHandler
    public void onBlockBreak(BlockBreakEvent event)
    {
    Block brokenBlock = event.getBlock();
    ItemStack itemInHand = event.getPlayer().getItemInHand();
     
    if (event.getPlayer() == null)
    return;
     
    if (isValid(itemInHand.getType(), brokenBlock.getType()))
    {
    if (!event.getPlayer().hasPermission("fortuneplus." + brokenBlock.getType().name().toLowerCase()) )
    return;
     
    Map<Enchantment, Integer> enchantments = itemInHand.getEnchantments();
    if (enchantments.containsKey(Enchantment.LOOT_BONUS_BLOCKS))
    {
    event.setCancelled(true);
     
    int numDrops = getNumDrops(enchantments.get(Enchantment.LOOT_BONUS_BLOCKS));
     
    if (numDrops == -1)
    return;
    else
    {
    brokenBlock.getWorld().dropItemNaturally(brokenBlock.getLocation(), new ItemStack(getDropType(brokenBlock.getType()), numDrops));
    ((ExperienceOrb)brokenBlock.getWorld().spawn(brokenBlock.getLocation(), ExperienceOrb.class)).setExperience(1);
    brokenBlock.setType(Material.AIR);
    }
    }
    }
    }
     
    private boolean isValid(Material pMat, Material bMat)
    {
    switch (bMat)
    {
    case IRON_ORE:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case GOLD_ORE:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case GOLD_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case IRON_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case LAPIS_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case OBSIDIAN:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case DIAMOND_ORE:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case DIAMOND_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case COAL_ORE:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case COAL_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case EMERALD_ORE:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case EMERALD_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case STONE:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case CLAY:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case DIRT:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case WOOD:
    return pMat == Material.DIAMOND_AXE;
    case WOOL:
    return pMat == Material.SHEARS;
    case REDSTONE_ORE:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case REDSTONE_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    case QUARTZ_BLOCK:
    return pMat == Material.IRON_PICKAXE || pMat == Material.DIAMOND_PICKAXE;
    default:
    return false;
    }
    }
     
    private int getNumDrops(int fortuneLevel)
    {
    switch (fortuneLevel)
    {
    case 5:
    return 2;
    case 10:
    return 3;
    case 7:
    return 3;
    case 15:
    return 4;
    case 20:
    return 5;
    case 25:
    return 5;
    case 30:
    return 6;
    case 35:
    return 7;
    case 40:
    return 8;
    case 45:
    return 9;
    case 50:
    return 10;
    case 55:
    return 11;
    case 60:
    return 12;
    case 65:
    return 13;
    case 70:
    return 14;
    case 75:
    return 15;
    default:
    return -1;
    }
    }
     
    private Material getDropType(Material bMat)
    {
    switch (bMat)
    {
    case IRON_ORE:
    return Material.IRON_INGOT;
    case IRON_BLOCK:
    return Material.IRON_BLOCK;
    case GOLD_ORE:
    return Material.GOLD_INGOT;
    case GOLD_BLOCK:
    return Material.GOLD_BLOCK;
    case LAPIS_BLOCK:
    return Material.LAPIS_BLOCK;
    case OBSIDIAN:
    return Material.OBSIDIAN;
    case DIAMOND_ORE:
    return Material.DIAMOND;
    case DIAMOND_BLOCK:
    return Material.DIAMOND_BLOCK;
    case COAL_ORE:
    return Material.COAL;
    case COAL_BLOCK:
    return Material.COAL_BLOCK;
    case EMERALD_ORE:
    return Material.EMERALD;
    case EMERALD_BLOCK:
    return Material.EMERALD_BLOCK;
    case STONE:
    return Material.STONE;
    case CLAY:
    return Material.CLAY;
    case DIRT:
    return Material.DIRT;
    case WOOD:
    return Material.WOOD;
    case WOOL:
    return Material.WOOL;
    case REDSTONE_ORE:
    return Material.REDSTONE;
    case REDSTONE_BLOCK:
    return Material.REDSTONE_BLOCK;
    case QUARTZ_BLOCK:
    return Material.QUARTZ_BLOCK;
    default:
    return Material.AIR;
    }
    }
    }
     
  2. Offline

    iTornado1234

    andrew4543 First off, please put it in the code. When you click to edit it should show this thing called Code and put the code in there. Secondly, any errors?
     
  3. Offline

    andrew4543

    iTornado1234 no no errors, just when people without permission try to break a block inside a world guard region that has build deny on it. It doesn't stop them, and it works.

    I know i am going to have to add a way to make it so that the plugin checks world guard regions first, but i have no idea how

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  4. Offline

    iTornado1234

    andrew4543 It's cause I bet the WG region has the lower priority. Make yours have a lower priority. If that doesn't work then maybe check for the region by hooking into the wg api and if it is true then cancel that event.
     
  5. Offline

    andrew4543

    iTornado1234 I tried it, but it does not stop me. here is the code
    Code:
        @EventHandler (priority = EventPriority.LOWEST)
        public void onBlockBreak(BlockBreakEvent event)
        {
            Block brokenBlock = event.getBlock();
            ItemStack itemInHand = event.getPlayer().getItemInHand();
         
            if (event.getPlayer() == null)
                return;
     
            if (isValid(itemInHand.getType(), brokenBlock.getType()))
            {
                if (!event.getPlayer().hasPermission("fortuneplus." + brokenBlock.getType().name().toLowerCase()) )
                    return;
             
                Map<Enchantment, Integer> enchantments = itemInHand.getEnchantments();
                if (enchantments.containsKey(Enchantment.LOOT_BONUS_BLOCKS))
                {
                    event.setCancelled(true);
     
                    int numDrops = getNumDrops(enchantments.get(Enchantment.LOOT_BONUS_BLOCKS));
                                         
                    if (numDrops == -1)
                        return;
                    else
                    {
                        brokenBlock.getWorld().dropItemNaturally(brokenBlock.getLocation(), new ItemStack(getDropType(brokenBlock.getType()), numDrops));
                        ((ExperienceOrb)brokenBlock.getWorld().spawn(brokenBlock.getLocation(), ExperienceOrb.class)).setExperience(1);
                        brokenBlock.setType(Material.AIR);
                    }
                }
            }
        }
        
     
  6. Offline

    iTornado1234

    andrew4543 Did you try checking for the region by hooking into the wg api and then canceling if the player who is trying to break a block that is in the region.
     
  7. Offline

    andrew4543

  8. Offline

    andrew4543

    I took it a step forward, by making it where you make the drop go straight into the inventory, but it wont work. First it would only drop one block, when it should drop more, and it wont go into the next stack, only into the first stack.(once it gets to 64 it stops)Also it would not check world Guard even when it is a build path. And now it the block wont even break!
    Code:
        @EventHandler (priority = EventPriority.LOWEST)
        public void onBlockBreak(BlockBreakEvent event)
        {
            Block brokenBlock = event.getBlock();
            ItemStack itemInHand = event.getPlayer().getItemInHand();
            Player gp = event.getPlayer();
            ItemStack DropType = new ItemStack(getDropType(brokenBlock.getType(), getNumDrops(0)));
            PlayerInventory pi = gp.getInventory();
            int slot = pi.firstEmpty();
            int cslot = pi.first(DropType);
            Player g = event.getPlayer();
       
            if (g == null)
                return;
     
            if (isValid(itemInHand.getType(), brokenBlock.getType()))
            {
       
                Map<Enchantment, Integer> enchantments = itemInHand.getEnchantments();
                if (enchantments.containsKey(Enchantment.LOOT_BONUS_BLOCKS))
                {
                    event.setCancelled(true);
     
                    int numDrops = getNumDrops(enchantments.get(Enchantment.LOOT_BONUS_BLOCKS));
                                   
                    if (numDrops == -1)
                        return;
               
     
                    else
                    pi.addItem(DropType);
                    brokenBlock.getWorld();
                    brokenBlock.getLocation();
                    brokenBlock.setType(Material.AIR);               
                    return;
               
                    }
                }
       
        }
     
Thread Status:
Not open for further replies.

Share This Page