BlockBreakEvent not dropping items properly

Discussion in 'Plugin Help/Development/Requests' started by mnoco8, Apr 14, 2015.

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

    mnoco8

    Hey there...this is really weird as this is a pretty simple plugin I've done many times before..but now it just doesn't want to work!

    I set it so when crops (or wheat) are destroyed then wheat is dropped if a certain tool is used. This isn't working right now for some weird reason. Heres the code. Thanks for the help!

    Code:
    @EventHandler
        public void WheatBreak(BlockBreakEvent Break){
            Player player = Break.getPlayer();
            Block broken = Break.getBlock();
            Location loc = player.getLocation();
           
            ItemStack hand = player.getItemInHand();
            ItemStack drop = new ItemStack(Material.WHEAT);
           
            Map<Enchantment, Integer> Enchants = hand.getEnchantments();
           
            int Level = Enchants.get(Enchantment.LOOT_BONUS_BLOCKS);
           
            if(broken.equals(Material.CROPS) && hand.equals(Material.WOOD_HOE)||equals(Material.STONE_HOE)){
                player.getWorld().dropItem(loc, drop);
               
                if(Level == 1){player.getWorld().dropItem(loc, drop);
                }
                else
                { if(Level == 2){player.getWorld().dropItem(loc, drop);
                }
            }
            }
        }
     
  2. your code formatting is really weird O.O
     
  3. Offline

    nverdier

    I think it's unintentional. If it is, @mnoco8, I'd recommend learning Java. Read about that here.
     
  4. Offline

    mnoco8

    No not intentional..just came out that way!
     
  5. Offline

    TheEntropy

    @mnoco8 Some more info would be nice, too. Are you sure the event is registered and running properly? Have you done any debugging so far?
     
  6. Offline

    nverdier

    @mnoco8 Well the logic is really screwed up with the syntax you're using.
     
  7. Offline

    TheEntropy

    @mnoco8 If you just look for Material.CROPS, that will only drop wheat if the crops aren't fully grown. I used this little snippet of code to drop a piece of wheat at the player's location if they broke wheat (in any growth stage) and were holding a wooden hoe:

    Code:
    	Player p = e.getPlayer();
    	Block broken = e.getBlock();
    		
    	if (broken.getType() == Material.CROPS || broken.getType() == Material.WHEAT && p.getItemInHand().getType() == Material.WOOD_HOE) {
    		
    		p.getWorld().dropItem(broken.getLocation(), new ItemStack(Material.WHEAT));
    			
    	}
    
     
  8. Offline

    Zombie_Striker

    Please change Break to anything else. The term break is commonly used for loops, so don't use it as a name. Your if statement should have more brackets (e.g. (1 && 2) || 3 or 1 && (2 || 3))

    You have two if statements that don't do anything different from each other. Did you want to use numb >= 1 or 2, or can you merge those two statements.
     
  9. Offline

    mnoco8

    How so? I agree it is really weird looking but the code is readable (to a point) sorry for it being so messed up :(

    Will do on the name change. Brackets wise I didn't copy them all accidentally but no worries all are there! Sorry I just realized that the statements are the same..I think I cut something out accidentally but Ill fix it!

    Sorry for the double post but this is important...

    So I got the event itself working (I actually forgot my listener -.-)

    But Heres the error I got when I ran it and destroyed the wheat:
    Code:
    
    
    
    [19:37:51 ERROR]: Could not pass event BlockBreakEvent to MainCrops v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:297) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerInteractManager.breakBlock(PlayerI
    nteractManager.java:285) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerInteractManager.a(PlayerInteractMa
    nager.java:189) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :573) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInBlockDig.a(SourceFile:40) [c
    raftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInBlockDig.a(SourceFile:10) [c
    raftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [cra
    ftbukkit.jar:git-Bukkit-33d5de3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_25]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_25]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    56) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:2
    84) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    09) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :517) [craftbukkit.jar:git-Bukkit-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.NullPointerException
            at crops.MainCrops.Wheat(MainCrops.java:36) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:295) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            ... 16 more
    >
    Heres is my code so far(prepare for wonky formatting):

    Code:
    public void Wheat(BlockBreakEvent Broken){
            Player player = Broken.getPlayer();
            Block broken = Broken.getBlock();
            Location loc = player.getLocation();
           
            ItemStack hand = player.getItemInHand();
            ItemStack drop = new ItemStack(Material.APPLE);
           
            Map<Enchantment, Integer> Enchants = hand.getEnchantments();
           
            //this is line 36// int Level = Enchants.get(Enchantment.LOOT_BONUS_BLOCKS);
           
             if (broken.getType() == Material.CROPS || broken.getType() == Material.WHEAT && player.getItemInHand().getType() == Material.WOOD_HOE) {
                  
                    player.getWorld().dropItem(broken.getLocation(), new ItemStack(Material.WHEAT));
                      
                }
                if(Level == 1){player.getWorld().dropItem(loc, drop);
                }
                else
                { if(Level == 2){player.getWorld().dropItem(loc, drop);
                }
            }
            }
        }
    
     
    Last edited by a moderator: Apr 14, 2015
Thread Status:
Not open for further replies.

Share This Page