[HELP]: Need help on code

Discussion in 'Plugin Development' started by TheMCBukkitTut, May 23, 2014.

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

    TheMCBukkitTut

    im trying to make a plugin where it won't drop the blocks but it will put it in there inventory stright away

    This is my code and it will not work at all:

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onBlockBreak(BlockBreakEvent event){
    4. Block block = event.getBlock();
    5. if(event.getPlayer().getItemInHand().equals(Material.DIAMOND_PICKAXE)){
    6. if(block.getType().equals(Material.STONE)){
    7. block.getDrops().clear();
    8. event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE));
    9. event.getPlayer().updateInventory();
    10. }
    11. }
    12. }
    13. }


    if you can do any code examples that would be great

    Thanks - Jacob
     
  2. Offline

    NathanWolf

    Two things I can see:

    1. You're comparing an ItemStack and a Material. You want getItemInHand().getType().equals(...).
    2. You're clearing drops from the Block, I think you want to clear the Event's drops instead
     
  3. Offline

    TheMCBukkitTut

    NathanWolf

    Thanks it works, do you know how to get all the picks/blocks?
     
  4. Offline

    NathanWolf

    No, not really. Do you need the Block check, or could this just work on anything? Is your intention for it to only work on ores, for instance?

    What I do for these sorts of cases is make a Set<Material> in advance (like in a Singleton object, or just static). I generally load these from a config for forward-compatibility reasons.

    Then you can just do specialBlocks.contains(block.getType()) instead of having this nasty-long cascade of if/else's.

    If you want to get fancy, you can condense this sort of thing to one line:

    Code:
    private final static Set<Material> blocks = new HashSet<Material>(Arrays.asList(new Material[] {
                Material.STONE, Material.DIAMOND_ORE, etc, etc
        }));
     
  5. Offline

    fireblast709

    NathanWolf real fancy code would've used EnumSet.of() ;)
     
    NathanWolf likes this.
  6. Offline

    NathanWolf

    +1 ! :)

    I copy+pasted this from my own code that uses Strings ... but it generally feels like there should be a cleaner way to make a hard-coded static Set.
     
  7. Offline

    TheMCBukkitTut

    I tried to use what you said with the:

    Code:java
    1. private final static Set<Material> PICKS = new HashSet<Material>(Arrays.asList(new Material[] {
    2. Material.WOOD_PICKAXE, Material.STONE_PICKAXE, Material.IRON_PICKAXE, Material.GOLD_PICKAXE, Material.DIAMOND_PICKAXE
    3. }));


    and

    Code:java
    1. if(event.getPlayer().getItemInHand().getType().equals(PICKS)){


    But it does not work?? please help
     
  8. Offline

    NathanWolf

    If you want to use the first part (Sets), the second part needs to change to PICKS.contains(itemInHand.getType());
     
  9. This works very well for me :D
    Code:java
    1.  
    2. @EventHandler
    3. public void onBlockBreak(BlockBreakEvent e) {
    4. Player p = e.getPlayer();
    5. Collection<ItemStack> drops = e.getBlock().getDrops();
    6. if (p.getGameMode() != GameMode.CREATIVE && !e.isCancelled() && p.getItemInHand.getType()==Material.DIAMOND_PICKAXE) {
    7. for (ItemStack item : drops) {
    8. p.getInventory().addItem(item);
    9. }
    10. e.getBlock().getDrops().clear();
    11. e.setCancelled(true);
    12. e.getBlock().setType(Material.AIR);
    13. }
    14. }
    15.  
     
  10. I understand this is solved but couldn't he just of had cancelled the event since he specified what he was dropping? So like if he wanted to drop the diamond pickaxe it would just put it back in some slot since the event was cancelled?
     
  11. Offline

    TheMCBukkitTut

    all i want is something that will work for all picks not just diamond pick, can anyone help?
     
  12. Offline

    NathanWolf

    That's what I gave you. Make a Set of all the types you care about, use Set.contains(Material) to check an item type.

    You could simplify the Set creation with that EnumSet trick fireblast mentioned, but it looks like you had that part working. I answered your question about why the check wasn't working (use contains not equals) - what else is wrong?
     
  13. Offline

    mrgreen33gamer

    Here yeah go:

    Code:java
    1.  
    2.  
    3. @SuppressWarnings("deprecation")
    4. @EventHandler
    5. public void onBlockBreak(BlockBreakEvent event){
    6. Block block = event.getBlock();
    7. if(event.getPlayer().getItemInHand().equals(Material.DIAMOND_PICKAXE) || event.getPlayer().getItemInHand().equals(Material.IRON_PICKAXE) || event.getPlayer().getItemInHand().equals(Material.GOLD_PICKAXE) || event.getPlayer().getItemInHand().equals(Material.STONE_PICKAXE) || event.getPlayer().getItemInHand().equals(Material.WOOD_PICKAXE)){
    8. if(block.getType().equals(Material.STONE)){
    9. block.getDrops().clear();
    10. event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE));
    11. event.getPlayer().updateInventory();
    12. }
    13. }
    14. }
    15.  
    16.  
     
Thread Status:
Not open for further replies.

Share This Page