Count number of blocks mined

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

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

    slater96

    Hi, I'm trying to get how many blocks a player has mined while they are in Jail and if they have then it should send them the message test, however it doesn't seem to be working. Can anyone help please?
    Code:
    @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player p = event.getPlayer();
            if (plugin.playersJailed.contains(p.getName())) {
                plugin.blocksMined.put(p.getName(), plugin.blocksMined.get(p.getName() + 1));
                int blocksToMine = plugin.getConfig().getInt("CrimeLevel.Low.AmountToMine");
                if (plugin.blocksMined.values().equals(blocksToMine)) {
                    p.sendMessage("test");
                }
            }
        }
     
  2. Offline

    sd5

    Aahm... blocksToMine is an int and plugin.blocksMined.values() returns returns a collection
    int != Collection
     
  3. if (plugin.blocksMined.values().equals(blocksToMine)) {
    values() won't return an int, so you can't compare it to blocksToMine. :)

    //EDIT: Ninjad by sd5
     
  4. Offline

    rjVapes

    plugin.blocksMined.values().equals(blocksToMine) should probably be something like:
    Code:
    plugin.blocksMined.get(p.getName()) >= blocksToMine
     
  5. Offline

    slater96

    That came up with an error saying >= is undefined for arguments. Anyone know how to make it work?
     
  6. Offline

    sd5

    what a map do you use? I recommend Map<String playerName, Integer blocks>
     
  7. Offline

    slater96

    I'm using a hashmap atm
    public HashMap<String, Integer> blocksMined = new HashMap<String, Integer>();
     
  8. Offline

    rjVapes

    slater96 Maybe it just doesn't like to compare Integer to int, kinda weird though. Cast one to the other and try.
     
  9. Offline

    Major

    You'll need to declare the time as an Integer rather than an int as if there is no value for the key (the object you use to get the value) then it'll return null - and primitive types (int, long, double, float, etc) cannot be null.

    There's your other problem :)
     
  10. Offline

    slater96

    I've been trying to use a for loop which I think will work, but atm it sends that message for all numbers up to the amount. Code:
    Code:
                      for (int i = 1; i < blocksToMine; i++) {
                            p.sendMessage("You have mined " + i + " block/s out of " + blocksToMine);
                            if (i == blocksToMine) {
                                p.sendMessage("test");
                            }
                        }
    Like it'll go
    You have mined 1 block/s out of 5
    You have mined 2 block/s out of 5
    You have mined 3 block/s out of 5
    You have mined 4 block/s out of 5
    You have mined 5 block/s out of 5
    test
    All at once when someone mines one block, but I want it to increase on every block mined, not all at once. Thanks

    Any help please?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  11. Offline

    Major

    Ok, what will happen is that you'll just iterate (loop) through everything - nothing will happen when the player mines. What you should do is this:

    Code:
    @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            String name = player.getName();
     
            if (plugin.playersJailed.contains(name)) {
                plugin.blocksMined.put(name, plugin.blocksMined.get(name) + 1);
                int blocksToMine = plugin.getConfig().getInt("CrimeLevel.Low.AmountToMine");
                if (plugin.blocksMined.get(name) == blocksToMine) {
                    player.sendMessage("test");
                }
            }
        }
    Also a couple of tips:
    'blocksToMine' is a constant value - it will not change, so you should declare it somewhere in the class rather than reading the value from the config each time a block is broken.
    Because it is a constant, it should be declared like:

    Code:
    public static final int BLOCKS_TO_MINE = plugin.getConfig().getInt("CrimeLevel.Low.AmountToMine");
    Also you should get your maps using accessor (get) methods, e.g.

    Code:
    private HashMap<String, Integer> blocksMined = new HashMap<String, Integer>();
     
    public HashMap<String, Integer> getBlocksMined(){
        return blocksMined;
    }
    Then you'd get it like:

    Code:
    plugin.getBlocksMined();
    Also you're reading from your own config file so there's no need to prefix it all with 'CrimeLevel' :p.
     
  12. Offline

    slater96

    Sorry, but it's still not working :(
    I didn't get any test message when I mined the block and also the constant gave me this error when I tried to use it.
    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:153)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:222)
    at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:565)
    at org.bukkit.Bukkit.reload(Bukkit.java:183)
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:21)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    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.ExceptionInInitializerError
    at me.slater96.SimpleJail.SimpleJail.<init>(SimpleJail.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:149)
    ... 21 more
    Caused by: java.lang.NullPointerException
    at me.slater96.SimpleJail.Listeners.BlockEvent.<clinit>(BlockEvent.java:13)
    ... 27 more
    
     
  13. Offline

    Major

    Remove the 'final' modifier from the constant.
     
  14. Offline

    slater96

    Still gives me this
    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:153)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:222)
    at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:565)
    at org.bukkit.Bukkit.reload(Bukkit.java:183)
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:21)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    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.ExceptionInInitializerError
    at me.slater96.SimpleJail.SimpleJail.<init>(SimpleJail.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:149)
    ... 21 more
    Caused by: java.lang.NullPointerException
    at me.slater96.SimpleJail.Listeners.BlockEvent.<clinit>(BlockEvent.java:13)
    ... 27 more
     
  15. Offline

    Major

    Paste all your classes please (p.s. packages should be lowercase).
     
Thread Status:
Not open for further replies.

Share This Page