Getting amount of blocks a player has mined?

Discussion in 'Plugin Development' started by slater96, Aug 10, 2012.

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

    slater96

    Hi, I need to get the amount of a certain block that a player has mined. It's the amount part that I'm not sure how to do. I though about getDrops() but that wouldn't be accurate would it..
    Thanks
     
  2. Offline

    Firefly

    Add 1 to a HashMap keying the player's name to the block they've mined whenever they mine that block. You might want to save the HashMap so it's persistent through reloads.
     
  3. Offline

    slater96

    Thanks, do you know what I'm doing wrong, it doesn't broadcast the message 'test' even when I've broke the correct amount of blocks.
    Code:
    @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player p = event.getPlayer();
            Block b = event.getBlock();
            if (plugin.playersJailed.containsKey(p.getName()) && plugin.playersJailed.containsValue(1) || plugin.playersJailed.containsValue(2) || plugin.playersJailed.containsValue(3) || plugin.playersJailed.containsValue(4) || plugin.playersJailed.containsValue(5)) {
                int lowCrimeLevel = plugin.getConfig().getInt("CrimeLevel.Low.BlockID");
                if (b.getTypeId() == lowCrimeLevel) {
                    plugin.blocksMined.put(p.getName(), plugin.blocksMined.get(p.getName() + 1));
                    int blocksToMine = plugin.getConfig().getInt("CrimeLevel.Low.BlocksToMine");
                    if (plugin.blocksMined.containsValue(blocksToMine)) {
                            p.sendMessage("test");
                    }
                }
            }
        }
     
  4. Offline

    Firefly

    This:

    if (plugin.blocksMined.containsValue(blocksToMine)) {

    Should be:

    if (plugin.blocksMined.get(p.getName()) == blockToMine) {

    Otherwise, you are checking if anyone has the write number of blocks mined.

    Add some more debug messages around your other if statements to see how far the code gets.
     
  5. Offline

    slater96

    The code is fine up until
    if (plugin.blocksMined.get(p.getName()) == blockToMine) {
    that i tried which then gives me an error on that line. Anyone know how to do this?
     
  6. Offline

    Phil2812

    slater96
    Did I get this right: You want to check if a player has mined a defined amount of a certain block?
    If yes you need to check what kind of block the player mined and if it is the right one increase your stored number of mined blocks for that player.
     
  7. Offline

    slater96

    Yeah, i can get the block, just having trouble increasing it.
    Code:
    @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player p = event.getPlayer();
            Block b = event.getBlock();
            if (plugin.playersJailed.containsKey(p.getName()) && plugin.playersJailed.containsValue(1) || plugin.playersJailed.containsValue(2) || plugin.playersJailed.containsValue(3) || plugin.playersJailed.containsValue(4) || plugin.playersJailed.containsValue(5)) {
                int lowCrimeLevel = plugin.getConfig().getInt("CrimeLevel.Low.BlockID");
                if (b.getTypeId() == lowCrimeLevel) {
                    plugin.blocksMined.put(p.getName(), plugin.blocksMined.get(p.getName() + 1));
                    int blocksToMine = plugin.getConfig().getInt("CrimeLevel.Low.AmountToMine");
                    if (plugin.blocksMined.get(p.getName()) == blocksToMine) {
                        p.sendMessage("test");
                    }
                } else {
                    p.sendMessage(ChatColor.RED + "You cannot break this block!");
                    event.setCancelled(true);
                }
            }
        }
     
  8. Offline

    chaseoes

    It's hard to help if you just say "it gives me an error" without giving us the error.
     
  9. Offline

    Phil2812

    slater96
    Okay try this:
    Code:
    plugin.blocksMined.put(p.getName(), plugin.blocksMined.get(p.getName()) + 1);
    (You set the braces wrong.)

    And out of curiosity:
    Code:
    plugin.playersJailed.containsValue(1) etc.
    What purpose do they have in the if clause?
    You might want to explain your plugin.playerJailed map and what exactly it saves.
     
  10. Offline

    slater96

    I said in a post above that the error is on this line
    if (plugin.blocksMined.get(p.getName()) == blockToMine) {
     
  11. Offline

    chaseoes

    I realize that, I quoted your post. Again, we need the error it gives you, not the line it gives you the error on. The line tells us nothing without the error.
     
  12. Offline

    Phil2812

    slater96
    Please answer the questions in my post above (or answer the questions in the comments in the code).
    Code:
        // your map that contains jailed players is plugin.playersJailed<String,Int> in this example
    // what does the int value in the jailedPlayers map stand for ?
        // your map that contains the number of blocks mined is plugin.blocksMined<String,Int>
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player p = event.getPlayer();
            Block b = event.getBlock();
            if (plugin.playersJailed.containsKey(p.getName()) && plugin.playersJailed.containsValue(1) || plugin.playersJailed.containsValue(2) || plugin.playersJailed.containsValue(3) || plugin.playersJailed.containsValue(4) || plugin.playersJailed.containsValue(5)) {
                int lowCrimeLevel = plugin.getConfig().getInt("CrimeLevel.Low.BlockID");
                if (b.getTypeId() == lowCrimeLevel) {
                    plugin.blocksMined.put(p.getName(), plugin.blocksMined.get(p.getName()) + 1);
                    int blocksToMine = plugin.getConfig().getInt("CrimeLevel.Low.BlocksToMine");
                    if (plugin.blocksMined.get(p.getName()) >= blocksToMine) {
                            // the player has mined blocksToMine blocks of the lowCrimeLevel type now
                            p.sendMessage("test");
                    }
                }
                else
                {
                    p.sendMessage(ChatColor.RED + "You cannot break this block!");
                    event.setCancelled(true);
                }
            }
        }
     
  13. Offline

    slater96

    It's for a jail plugin and there 5 cells so thats why i do 1,2,3,4,5 to stand for the cells. I didn't know how to do it an easier way.
     
  14. Offline

    Phil2812

    It is fine that you have a map mapping the Player to a Cell but why do you check that in the if clause?
    Also you just check if there is any player in any cell there.

    Did you get it to work now with the code I posted? If not what is the problem now?
     
  15. Offline

    slater96

    No I still get this error
    Code:
    18:59:41 [SEVERE] Could not pass event BlockBreakEvent to SimpleJail
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:332)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at net.minecraft.server.ItemInWorldManager.breakBlock(ItemInWorldManager
    .java:278)
            at net.minecraft.server.ItemInWorldManager.a(ItemInWorldManager.java:205
    )
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:549)
            at net.minecraft.server.Packet14BlockDig.handle(SourceFile:46)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
            at me.slater96.SimpleJail.Listeners.BlockEvent.onBlockBreak(BlockEvent.j
    ava:26)
            at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:330)
            ... 16 more
    This is line 26
    plugin.blocksMined.put(p.getName(), plugin.blocksMined.get(p.getName()) + 1);
     
  16. Offline

    Phil2812

    slater96
    I guess it is because the blocksMined map does not contain a player yet.
    Replace this line with that:
    Code:
    if (plugin.blocksMined.containsKey(p.getName()))
    {
      plugin.blocksMined.put(p.getName(), plugin.blocksMined.get(p.getName()) + 1);
    }
    else
    {
      plugin.blocksMined.put(p.getName(), 1);
    }
     
Thread Status:
Not open for further replies.

Share This Page