Letting pickaxes mine any block

Discussion in 'Plugin Development' started by Hex_27, Jan 9, 2015.

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

    Hex_27

    currently, if I mine an iron ore with a wooden pickaxe, then that block wouldn't be broken. Is there a way to allow wooden pickaxe and stone pickaxe to break any ore?
     
  2. Offline

    Skionz

    @Hex_27 Get the block when it breaks, get the drops, drop the drops.
     
  3. BlockBreakEvent ---> If item is wooden pickaxe/stone pickaxe ---> Drop event.getBlock().getDrops(). You can implement a custom fortune enchantment detector.
     
  4. Offline

    Hex_27

    @KingFaris11 I already do that. It still doesn't work. Refer to bottom explanation...
    @Skionz I do. I set the block to air for custom drops. It doesn't work for some reason. But it works if i use like Wood pick for coal and so on.
     
    Last edited: Jan 9, 2015
  5. Offline

    Skionz

    @Hex_27 Use World#dropItem() and drop coal.
     
  6. Offline

    1Rogue

    Code:java
    1. //constant for pickaxes
    2. private static final Set<Material> pickaxes = Collections.unmodifiableSet(EnumSet.of(Material.WOOD_PICKAXE,
    3. Material.STONE_PICKAXE,
    4. Material.GOLD_PICKAXE,
    5. Material.IRON_PICKAXE));
    6. //cache, rather than making pickaxe itemstacks on every block break
    7. private static final ItemStack diamondPick = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    8.  
    9. @EventHandler
    10. public void onBreak(BlockBreakEvent event) {
    11. ItemStack i = event.getPlayer().getItemInHand();
    12. if (i != null && pickaxes.contains(i.getType())) {
    13. //Proceed to drop your own items as if your pickaxe was diamond
    14. event.getBlock().getDrops(diamondPick).forEach(item
    15. -> event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), item));
    16. event.getBlock().setType(Material.AIR); //Clear the normal drops
    17. }
    18. }


    If you aren't using Java 8 you simply grab the return value of .getDrops and iterate over it.
     
    Last edited: Jan 9, 2015
  7. Offline

    Hex_27

    @1Rogue nononononono....... It doesn't even CALL the event when I break redstone ore with a wood pick. I can code all that drop junk, but the event isn't even called when I break any ore other than coal, with a wood pick.
     
  8. Offline

    1Rogue

    [​IMG]

    Code exactly as used (same code):
    Show Spoiler
    Code:java
    1. /*
    2. * Copyright (C) 2015 Codelanx
    3. *
    4. * This program is free software: you can redistribute it and/or modify
    5. * it under the terms of the GNU General Public License as published by
    6. * the Free Software Foundation, either version 3 of the License, or
    7. * (at your option) any later version.
    8. *
    9. * This program is distributed in the hope that it will be useful,
    10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12. * GNU General Public License for more details.
    13. *
    14. * You should have received a copy of the GNU General Public License
    15. * along with this program. If not, see <[url]http://www.gnu.org/licenses/>[/url].
    16. */
    17. package com.codelanx.bukkittest;
    18.  
    19. import java.util.Collections;
    20. import java.util.EnumSet;
    21. import java.util.Set;
    22. import org.bukkit.Material;
    23. import org.bukkit.event.EventHandler;
    24. import org.bukkit.event.Listener;
    25. import org.bukkit.event.block.BlockBreakEvent;
    26. import org.bukkit.inventory.ItemStack;
    27. import org.bukkit.plugin.java.JavaPlugin;
    28.  
    29. /**
    30. * Class description for {@link BukkitTest}
    31. *
    32. * @since 1.0.0
    33. * @author 1Rogue
    34. * @version 1.0.0
    35. */
    36. public class BukkitTest extends JavaPlugin implements Listener {
    37.  
    38. @Override
    39. public void onEnable() {
    40. this.getServer().getPluginManager().registerEvents(this, this);
    41. }
    42.  
    43. //constant for pickaxes
    44. private static final Set<Material> pickaxes = Collections.unmodifiableSet(EnumSet.of(Material.WOOD_PICKAXE,
    45. Material.STONE_PICKAXE,
    46. Material.GOLD_PICKAXE,
    47. Material.IRON_PICKAXE));
    48. //cache, rather than making pickaxe itemstacks on every block break
    49. private static final ItemStack diamondPick = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    50.  
    51. @EventHandler
    52. public void onBreak(BlockBreakEvent event) {
    53. ItemStack i = event.getPlayer().getItemInHand();
    54. if (i != null && pickaxes.contains(i.getType())) {
    55. //Proceed to drop your own items as if your pickaxe was diamond
    56. event.getBlock().getDrops(diamondPick).forEach(item
    57. -> event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), item));
    58. event.getBlock().setType(Material.AIR); //Clear the normal drops
    59. }
    60. }
    61.  
    62. }
    63.  

     
    Last edited: Jan 9, 2015
  9. Offline

    Hex_27

    @1Rogue Oh. Well... then is it possible for me to break ores with any item? Including fist? Also, I'm dropping custom items. Do I just like.... clear drops and drop a custom item?

    This is what I'm using currently for most of the ores.

    Code:
                 if(event.getBlock().getType() == Material.GOLD_ORE){
                            event.getBlock().setType(Material.AIR);
                           
                            loadingblocks.put(event.getBlock().getLocation(), Material.GOLD_ORE);
                            ItemStack copper = new ItemStack(Material.SPECKLED_MELON);
                            ItemMeta meta = copper.getItemMeta();
                            meta.setDisplayName("Copper");
                            copper.setItemMeta(meta);
                            copper.setAmount(1);
                           
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), copper);
                            if(!building.contains(event.getPlayer().getUniqueId())){
    
                                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.GOLD_ORE);
                                   }
                                }, 4800);
                           
                         }
                        }
     
  10. Offline

    1Rogue

    Well, what my code will do is make the result of you breaking the block the same as if you had mined it with a diamond pickaxe (if you use a pickaxe). Keep in mind, I did not account for fortune, but if you wanted to that would be easily included by synchronizing the method and then just adding (or removing) a fortune enchantment on the "diamondPick" variable based on the pickaxe the player is currently using.
     
  11. Offline

    Hex_27

    @1Rogue based on what I'm doing, I wouldn't need fortune as it would be impossible to get it in my gamemode. Your code would break the block and drop its natural items, but I can only apply that to the iron ore. The rest drop custom items
     
  12. Offline

    1Rogue

    So then add a check to make sure the block is iron ore.
     
  13. Offline

    Hex_27

    @1Rogue Yes, I've done that. Now the problem is for the other ores to drop custom stuff.
     
  14. Offline

    1Rogue

    If you keep a map of the materials to their drops, you can just do another check in the method:

    Code:java
    1. Map<Material, List<ItemStack>> myDrops


    Code:java
    1. if (myDrops.containsKey(event.getBlock().getType()) {
    2. //get the list of drops from the map and drop them the same way
    3. }


    Be careful to do that before the block is changed to air.
     
  15. Offline

    Hex_27

    @1Rogue if I'm dropping custom items, what does getting the block drops have anything to do with it?
     
  16. Offline

    1Rogue

    You don't get the block drops, you map the relevant block material (e.g. DIAMOND_ORE) to the list of drops for the block. When I say "get the list of drops from the map", I don't mean the minecraft map, I mean:

    Code:java
    1. Map<Material, List<ItemStack>> myDrops
     
  17. Offline

    Hex_27

    @1Rogue I still don't understand
     
  18. Offline

    1Rogue

    You have custom drops, right?

    Code:java
    1. List<ItemStack> example = /* your custom diamond ore drops */
    2. myDrops.put(Material.DIAMOND_ORE, example);


    Then do as stated above.
     
  19. Offline

    Hex_27

    @1Rogue so I put this in mydrops, then I drop it later on? IS there like, another way to trick the system into thinking that the player is using a diamond pickaxe?
     
  20. Offline

    1Rogue

    I'm literally flat out telling you that you do a check in the same onBreak method for the block type, and drop the drops for it accordingly. (As I have done for the ores). The Map is there so that you can store what Blocks will drop what.

    http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
     
  21. Offline

    Hex_27

    @1Rogue I just tried your explanation, but the event was somehow cancelled, so the whole thing still didn't work
     
  22. Offline

    1Rogue

    Test your plugin first on a server without other plugins. Also perhaps try a higher event priority.
     
  23. Offline

    Hex_27

    @1Rogue but it still works with wood pick on coal, but not wood pick on anything else.
     
  24. Offline

    1Rogue

    What is your full method?
     
  25. Offline

    Hex_27

    Pardon me its a very long method...
    As you can see, I only cancel the event all the way at the end, so when wood picks break gold ore or something like that, it simply doesn't break. When I put a debugger under the ores to see if the event was even called, the debug message didn't come out.
    Code:
             @SuppressWarnings("deprecation")
        @EventHandler
        public void onBreak(BlockBreakEvent event){
            if(    isValidWorld(event.getPlayer().getWorld())){
                if(!event.isCancelled()){
                if(isBreakable(event.getBlock())){
                   
                    if(event.getBlock().getType() == Material.SMOOTH_BRICK){
                        if(event.getBlock().getData() != (byte) 3){
                            if(!building.contains(event.getPlayer().getUniqueId())){
                            event.setCancelled(true);
                        }
                    }
                    }
                   
                    if(isProtected(event.getBlock())){
                        String s = locationToString(event.getBlock().getLocation(), event.getPlayer());
                        List<String> blocklist = blocks.getStringList("blocks");
                        if(blocklist.contains(s)){
                           
                            blocklist.remove(s);
                            blocks.set("blocks", blocklist);
                            saveBlocks();
                        }else{
                            if(!building.contains(event.getPlayer().getUniqueId())){
                            event.setCancelled(true);
                            if(event.getBlock().getType() == Material.SMOOTH_BRICK){
                                if(event.getBlock().getData() != (byte) 3){
                                   
                            }else{
                                event.getPlayer().sendMessage(ChatColor.RED + "This block was not placed by you!");
                            }
                            }else{
                            event.getPlayer().sendMessage(ChatColor.RED + "This block was not placed by you!");
                            }
                            }
                    }
                    }
                   
                }else{
                   
                   
                   
                     if(event.getBlock().getType() == Material.LOG){
                            event.getBlock().setType(Material.AIR);
                            int amount = getConfig().getInt("logsPerLogBreak");
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.LOG, amount));
                            if(!building.contains(event.getPlayer().getUniqueId())){
                               
                                loadingblocks.put(event.getBlock().getLocation(), Material.LOG);
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.LOG);
                                   }
                                }, 2400);
                           
                         }
                        }
                    
                     if(event.getBlock().getType() == Material.GOLD_ORE){
                            event.getBlock().setType(Material.AIR);
                           
                            loadingblocks.put(event.getBlock().getLocation(), Material.GOLD_ORE);
                            ItemStack copper = new ItemStack(Material.SPECKLED_MELON);
                            ItemMeta meta = copper.getItemMeta();
                            meta.setDisplayName("Copper");
                            copper.setItemMeta(meta);
                            copper.setAmount(1);
                           
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), copper);
                            if(!building.contains(event.getPlayer().getUniqueId())){
    
                                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.GOLD_ORE);
                                   }
                                }, 4800);
                           
                         }
                        }
    
                     if(event.getBlock().getType() == Material.REDSTONE_ORE ||
                                event.getBlock().getType() == Material.GLOWING_REDSTONE_ORE){
                            event.getBlock().setType(Material.AIR);
                            loadingblocks.put(event.getBlock().getLocation(), Material.REDSTONE_ORE);
                              ItemStack silveringot = new ItemStack(Material.REDSTONE_ORE);
                              ItemMeta meta = silveringot.getItemMeta();
                              meta.setDisplayName("Silver Ore");
                              silveringot.setItemMeta(meta);
                             
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), silveringot);
                            if(!building.contains(event.getPlayer().getUniqueId())){
                               
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.REDSTONE_ORE);
                                   }
                                }, 6400);
                           
                         }
                        }
    
                     if(event.getBlock().getType() == Material.IRON_ORE){
                            event.getBlock().setType(Material.AIR);
                            loadingblocks.put(event.getBlock().getLocation(), Material.IRON_ORE);
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.IRON_ORE, 1));
                            if(!building.contains(event.getPlayer().getUniqueId())){
                               
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.IRON_ORE);
                                   }
                                }, 6400);
                           
                         }
                        }
    
                     if(event.getBlock().getType() == Material.DIAMOND_ORE){
                            event.getBlock().setType(Material.AIR);
                            loadingblocks.put(event.getBlock().getLocation(), Material.DIAMOND_ORE);
                              ItemStack gem = new ItemStack(Material.DIAMOND);
                              ItemMeta meta = gem.getItemMeta();
                              meta.setDisplayName("Gem");
                              gem.setItemMeta(meta);
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), gem);
                            if(!building.contains(event.getPlayer().getUniqueId())){
                               
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.DIAMOND_ORE);
                                   }
                                }, 7800);
                           
                         }
                        }
    
                     if(event.getBlock().getType() == Material.COAL_ORE){
                            event.getBlock().setType(Material.AIR);
                            loadingblocks.put(event.getBlock().getLocation(), Material.COAL_ORE);
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.COAL, 1));
                            if(!building.contains(event.getPlayer().getUniqueId())){
                               
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.COAL_ORE);
                                   }
                                }, 4800);
                           
                         }
                        }
    
                     if(event.getBlock().getType() == Material.LEAVES){
                           
                            Random random = new Random();
                            if(random.nextInt(100) < 2){
                               
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.APPLE, 1));
                            }
                        }
    
                     if(event.getBlock().getType() == Material.LONG_GRASS){
                            event.getBlock().setType(Material.AIR);
                            loadingblocks.put(event.getBlock().getLocation(), Material.LONG_GRASS);
                            Random random = new Random();
                            if(random.nextInt(100) < 2){
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.SEEDS, 1));
                            }
                            if(!building.contains(event.getPlayer().getUniqueId())){
                               
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.LONG_GRASS);
                                   }
                                }, 2400);
                           
                         }
                        }
    
                     if(event.getBlock().getType() == Material.IRON_FENCE){
                            if(event.getPlayer().getItemInHand() != null &&
                                    event.getPlayer().getItemInHand().getType() == Material.STONE_PICKAXE){
                            event.getBlock().setType(Material.AIR);
                            loadingblocks.put(event.getBlock().getLocation(), Material.IRON_FENCE);
                            Random random = new Random();
                            if(random.nextInt(100) < 10){
                            event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.IRON_INGOT, 1));
                            }
                            if(!building.contains(event.getPlayer().getUniqueId())){
                               
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                  public void run() {
                                   event.getBlock().setType(Material.IRON_FENCE);
                                   }
                                }, 2400);
                           
                         }
                        }else{
                            event.getPlayer().sendMessage(ChatColor.RED + "Iron gates can only be destroyed by sledge hammers!");
                        }
                        }
                     event.setCancelled(true);
                   
                   
                }
            }
        }
        }
     
  26. Offline

    1Rogue

    Try starting fresh with the method I provided, and working in the logic from your previous method. About 80% of that method is no longer needed.
     
  27. Offline

    Hex_27

    @1Rogue just asking, how do I drop the itemstack after I put it in the map?
     
  28. Offline

    Skionz

  29. Offline

    1Rogue

    Retrieve the itemstack from the map (You should've learned this by now from the tutorial link I gave you for maps), and follow my example as well :) (note where I dropped the items).
     
  30. Offline

    Hex_27

    @1Rogue damn its hard to talk over like this... I mean how do I extract the drops, if I save the Material mined?
    I'm saving it like this: (Material.REDSTONE_ORE, ItemStack(bla bla bla)) So what am I supposed to do with it? I'd prefer a small snipplet of code, because I'm jus getting confused over this. I can get the redstone ore, I can get the itemstack, but what does it have to do with anything? I can't even save the block as BlockBreakEvent isn't even called when I mine a block with a pickaxe.

    Ok nevermind... turns out something is stopping only NON-OPS from using wood picks to break gold ores....But I can't tell from where. Does nocheat try to stop this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page