Solved BlockBreakEvent issue

Discussion in 'Plugin Development' started by bwfcwalshy, Jul 21, 2014.

Thread Status:
Not open for further replies.
  1. I break a block (lapis) which is in the config with its id but when i break it nothing drops.

    Error code:
    Code:
    [13:30:34 ERROR]: Could not pass event BlockBreakEvent to BlockFortune v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:320) ~[custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:486) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:471) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerInteractManager.breakBlock(PlayerI
    nteractManager.java:263) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerInteractManager.a(PlayerInteractMa
    nager.java:191) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :548) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockDig.a(SourceFile:53) [c
    ustom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockDig.handle(SourceFile:8
    ) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [cust
    om.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.IllegalArgumentException: No enum constant org.bukkit.Mater
    ial.22
            at java.lang.Enum.valueOf(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.Material.valueOf(Material.java:64) ~[custom.jar:git-Bukkit
    -1.7.2-R0.3-b3020jnks]
            at com.bwfcwalshy.blockfortune.FortuneBlock.onBreak(FortuneBlock.java:27
    ) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:318) ~[custom.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    code:
    Code:java
    1. @EventHandler
    2. public void onBreak(BlockBreakEvent e){
    3. Player player = e.getPlayer();
    4. @SuppressWarnings("deprecation")
    5. int blockid = e.getBlock().getTypeId();
    6. String sblockid = String.valueOf(blockid);
    7.  
    8. if (player.hasPermission("blockfortune.use")){
    9. if(player.getGameMode().equals(GameMode.SURVIVAL)) {
    10. if(Main.pl.getConfig().getIntegerList("BlockIds").contains(Integer.valueOf(blockid))){
    11. if(player.getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)){
    12. int fortune = player.getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
    13. e.getBlock().setType(Material.AIR);
    14. e.getBlock().getDrops().clear();
    15. Material bId = Material.valueOf(sblockid);
    16. ItemStack drops = new ItemStack(bId, fortune);
    17. player.getWorld().dropItem(e.getBlock().getLocation(), drops);
    18. }
    19. }
    20. }
    21. }
    22. }
    23. }
    24. }


    and yes i have registered the listener
     
  2. Offline

    MCMastery

    NEVER use IDs anymore. Use material names.
    It is saying there is no Material.22.
    There IS a Material.LAPIS_BLOCK
    So do Material.getMaterialById(22)
     
  3. Offline

    xmarinusx

    bwfcwalshy
    You are using Material.valueOf(); which needs the Material name, not the material ID.
     
  4. Ok so my new code is this:
    Code:java
    1. package com.bwfcwalshy.blockfortune;
    2.  
    3. import org.bukkit.GameMode;
    4. import org.bukkit.Material;
    5. import org.bukkit.enchantments.Enchantment;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.BlockBreakEvent;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. public class FortuneBlock implements Listener{
    13.  
    14. @EventHandler
    15. public void onBreak(BlockBreakEvent e){
    16. Player player = e.getPlayer();
    17. @SuppressWarnings("deprecation")
    18. int blockid = e.getBlock().getTypeId();
    19. String sblockid = String.valueOf(blockid);
    20.  
    21. if (player.hasPermission("blockfortune.use")){
    22. if(player.getGameMode().equals(GameMode.SURVIVAL)) {
    23. if(Main.pl.getConfig().getIntegerList("BlockIds").contains(Integer.valueOf(blockid))){
    24. if(player.getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)){
    25. int fortune = player.getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
    26. e.getBlock().setType(Material.AIR);
    27. e.getBlock().getDrops().clear();
    28. Material bId = Material.getMaterial(sblockid);
    29. ItemStack drops = new ItemStack(bId, fortune);
    30. player.getWorld().dropItem(e.getBlock().getLocation(), drops);
    31. }
    32. }
    33. }
    34. }
    35. }
    36. }


    But it still isn't working, do i need to change the config?

    xmarinusx MCMastery know how to fix it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    xmarinusx

    bwfcwalshy
    Yes, the problem is in the sblockid object. sblockid should be the name of the material.
     
  6. xmarinusx how would i fix it? what should the line be? Material needs a id and the id is blockid.
     
  7. Offline

    xmarinusx

    bwfcwalshy
    Just change 'Material bId = Material.getMaterial(sblockid);'
    to
    'Material bID = e.getBlock().getType();'
     
    MCMastery likes this.
  8. Offline

    MCMastery

    bwfcwalshy BTW the e.getBlock().setMaterial(Material.AIR) I don't think it does anything, since the event says it was broken anyway :p
     
  9. Offline

    xmarinusx

    It does do something though, it will remove the block before the server does actually count it as a block break, so no items will be dropped etc.
     
  10. Ok thats fixed now its just a issue of it wont set it to Air
    Line causing issue
    Code:java
    1. e.getBlock().setType(Material.AIR);


    MCMastery if i remove it both the fortune ones and the normal block drop as 2 seperate drops and bug out.

    xmarinusx any ideas how to fix it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  11. Bump

    So what is the issue now, well when i remove this line: e.setBlock().setType(Material.AIR); it drops 2 different item stacks it drops the fortune one and the one from the block originally problem is 1.it looks weird 2.it bugs out. When i add the line it doesn't drop anything. Need help!
     
  12. Offline

    xmarinusx

    It looks like you're setting the block to AIR before getting the drops. Just move the line all the way to the bottom of the event.
     
  13. xmarinusx Worked. Is there a way for it to randomly pick how many blocks?

    So for example Fortune 1: 2-5, Fortune 2: 3-7, Fortune 5-12
    Any way to do that and set it in the config?
     
  14. Offline

    xmarinusx

    bwfcwalshy
    You can create a random integer using:
    Code:java
    1.  
    2. Random rand = new Random();
    3. int i = rand.nextInt(max);
    4.  

    'max' will be the max value of the random integer, exclusive. So if max = 10, i will be max 9. I hope that makes sense :p
     
  15. xmarinusx ok is there a way to set minimum? and for different fortunes?
     
  16. Offline

    xmarinusx

    There's no method which uses minimum, but you can do it yourself. If you want a minimum of 3, just add 3 to the integer and make the max 3 less.

    Different fortunes can be achieved in different ways, depending on how you want it. You could e.g. use if checks, or you could add the fortune level to the min/max. I'm sure you can create an algorythm for it yourself.
     
    bwfcwalshy likes this.
Thread Status:
Not open for further replies.

Share This Page