[Help]HiddenTreasure in block

Discussion in 'Plugin Development' started by McKiller5252, Dec 2, 2013.

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

    McKiller5252

    When someone is mining there is a very slim chance, say 1 in 100 of a block behind where you're mining will turn into a chest and spawn a random firework , with configurable items randomly placed into it.

    The basic idea would be in a config set the 'chance ratio'...and the items it can possibly have in it.

    Have it check the blocks around the one being mined, and if smoothstone is being mined, and it has say 6 smoothstone around it, one of them can become a chest, so you don't get weird random 'chest in mid air' or 'chest spawned into lava' on break.

    I attempted to create it, but my bukkit programming is a bit too limited for this.

    My basic line of logic was this:
    'on_break' of smoothstone, check chance to create chest
    check surrounding blocks, if 3+ smoothstone blocks are found in a 3x3x3 cube centered on on_break event block, replace one with chest and populate with items/spawn a firework to tell the player they found hidden treasure.

    I was wondering how would the code look like?
    Would anyone be willing to give me a example of the code if they could code it up for me so i could take a look at it.

    That would be awesome :D

    Thanks, Killer5252
     
  2. Offline

    Bart

    Please don't expect people to write code for you very often but I'm in a good mood this evening so I've written out the majority of the code (It's a bit inefficient but ultimately it's the best way to go about it)

    I haven't done the parts that you can google such as populating a chest with items and spawning fireworks but hopefully this should get you on the right track. I wrote this with an IDE so it is syntactically correct

    Code:java
    1. private Random r;
    2. @EventHandler
    3. public void onBlockBreak(BlockBreakEvent evt)
    4. {
    5. //initialise the random variable!
    6. if(r == null) r = new Random();
    7.  
    8. //block is stone
    9. if(evt.getBlock().getType() == Material.STONE)
    10. {
    11. int chance = 50; //50% chance of a block being a chest
    12. Block block = evt.getBlock();
    13.  
    14. //loop through all directions
    15. for(BlockFace bf : BlockFace.values())
    16. {
    17. //get the block in that direction
    18. Block block2check = block.getRelative(bf);
    19.  
    20. //if it's not stone then just exit because it means one of the blocks around it is not stone
    21. //it will also allow air only because there is no way to break a block where there is stone in every single direction of it
    22. if(block2check.getType() != Material.STONE || block2check.getType() != Material.AIR)
    23. {
    24. return;
    25. }
    26. }
    27.  
    28.  
    29. if(r.nextInt(100) >= chance)
    30. {
    31. //high enough chance! make it a chest
    32. //for now, just grab block above it and set it to a chest
    33. //TODO implement it so that a chest will spawn in any direction rather than just above it.
    34. Block chest = block.getRelative(BlockFace.UP);
    35. chest.setType(Material.CHEST);
    36.  
    37. //fireworks & fill chest
    38. }
    39. }
    40. }
    41.  
     
  3. Offline

    McKiller5252


    This is the only time i will be asking someone to write me code :)
    Wow... Thank you much Bart :)
     
Thread Status:
Not open for further replies.

Share This Page