Best way to do this?

Discussion in 'Plugin Development' started by ZodiacTheories, Aug 1, 2014.

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


    Hi, so I am making a plugin so when a block is broken players with the permission get a message saying that someone just broke a block. That works. The only problem is, I need to find out the amount of blocks they broke. I came up with one way of doing this; when the block is broken, I check if any other blocks of the same type are around it? I don't think that way is very efficient. So, if you have a better way, please say!

  2. Offline


    Temporary store how many blocks they have broken?
        Map<String, Integer> blocksBroken = new HashMap<String, Integer>();
        public void onBlockBreak(BlockBreakEvent event){
                blocksBroken.put(event.getPlayer().getName(), blocksBroken.get(event.getPlayer().getName())+1);
  3. Offline


    ZodiacTheories An idea that may work better, though not sure how efficient it would be, is to compare the time inbetween breaking of a certain block (Map-based-Timestamps or a Table (credit to Comphenix for that idea)), combined with a scheduler to preform self-cleanup and notification.

    Something like:
    Player A breaks Diamonds, timestamp is stored.
    A repeating task/task timer that runs every x seconds (one task that handles the whole map could save on resources) will use the time-stamps to determined when the Player has "stopped mining" by comparing the last time stamp to the current time and checking if it is x ms old, then if it is, notify admins that the Player found x diamonds and clear all relevant data from that "mining session". A class for handling this may be easier then having multiple maps (multiple being timestamp and ore counting), such as a "Session" class where it holds reference to the Player, what he has mined, and the timestamping, making this used in the scheduler.
    The idea for the scheduler is that it'll iterate over the stored data, and notify the admins, then clear it and prepare for the next session.
  4. Offline


    xTigerRebornx, ZodiacTheories
    Why are you guys making this so complicated?
    1. int blocksbroken = 0;
    3. @EventHandler
    4. public void onBlockBreak(BlockBreakEvent e){
    5. blocksbroken++;
    6. e.getPlayer().sendMessage("Blocks Broken: "+blocksbroken);
    7. }
  5. Offline


    MoeMix He gave a vague question, his solution made me interpret it differently then you and provide a different solution. Didn't realize there was only ever one way to see a question. That, and your solution doesn't work for multiple people.
  6. Offline


    Because then if I break a block it will add it to your total?

    EDIT: Ahh, I see how you read the question.
  7. Offline


    xTigerRebornx, I just went off of what he explicitly asked. Didn't want to make any suggestions. And yes, thanks for catching that I'll edit it.

    I'll edit the code

    ZodiacTheories, Nevermind, if I were to edit then I would have wrote exactly what RenegadeEagle already wrote. But yeah, hashmaps are only temporary so might want to store it in a config.

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


Thread Status:
Not open for further replies.

Share This Page