Do...Loop help

Discussion in 'Plugin Development' started by HaCkTiC_HaWkZ, Jun 30, 2012.

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

    HaCkTiC_HaWkZ

    Hey, I had this brilliant idea of using a do loop to make a little fun system for a player/s and this is what I have so far.

    Code:
        
    int dirt_block = 0; // < -- setting the value to 0;
        int sand_block = 0;
        int cobble_block = 0;
       
            public void onBlockDestroy(BlockBreakEvent bbe){  // < --- I made a function so that everytime a dirt block gets destroyed it adds 1 onto the value of dirt_block
           
            Material type = bbe.getBlock().getType();
                if(type == Material.DIRT){
                    dirt_block++;  // < -- add 1 to dirt_block int
                }else{   
                    bbe.getPlayer().sendMessage(ChatColor.GOLD + "[SystemNPC] " + "You have " + dirt_block + "Dirt blocks left"); 
                }
               
           
            do{
                    bbe.getPlayer().sendMessage(ChatColor.GOLD + "[SystemNPC] " + ChatColor.WHITE + "You have to get " + dirt_block +"!");
            }while(dirt_block <=10); // < -- This was a test so I put 10
        } 
    
    The problem is when I run my server and destroy 10 blocks of dirt I dont get a message, and I dont get a message stating how many left I have to do, I a guessing its a problem in my OnBlockDestroy but Ive tried a few things so far and they just lead to the same thing xD so I would appriciate it alot if someone helped me out :)

    Thanks
    Lewis / Hawkz
     
  2. Your event doesn't trigger, missing @EventHandler and make sure you use registerEvents() on that listener class :)

    Also, that loop will be infinite.
    And also, you don't need a do-while for that, a simple while would've been just fine as well.
     
  3. Offline

    HaCkTiC_HaWkZ

    xD omg i forgot it ahhahahahahahhaha i aint been bed for a day now :L but yes i see now thank you :D and the listener is registered :) xD ahahahahahh i feel so silly now i hope it works when i add the event handler
    thank you :)

    xD and now its only stuck in a loop for the last code and everytime i break a dirt block it does not add one onto the dirt_block int i think i have to do

    int dirt_block = +1; just to make sure

    i also see now that there is a problem with my function,..... because its all good at first but when i start hitting blocks and destroying 1 it starts the do loop then doesnt add 1 onto my dirt_block..... any suggestions?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. You need to change the dirt_block value inside the loop so it doesn't always return true for the while() condition...

    And ofc it doesn't add because it's in an infinite loop xD
     
  5. Offline

    HaCkTiC_HaWkZ

    xD well i guess i gotta watch some more new boston cause my idea failed :') i failed Bucky ahahhahaha,

    also something like this? or am i just failing xD ahahaha

    Code:
    int dirt_block = 0;
        int sand_block = 0;
        int cobble_block = 0;
        int dirt= 0;
        dirt = dirt_block;
       
            public void onBlockDestroy(BlockBreakEvent bbe){
           
            Material type = bbe.getBlock().getType();
                if(type == Material.DIRT){
                    dirt_block = ++;
                }else{   
                    bbe.getPlayer().sendMessage(ChatColor.GOLD + "[SystemNPC] " + "You have " + dirt_block + "Dirt blocks left");
                }
               
           
            do{
                    bbe.getPlayer().sendMessage(ChatColor.GOLD + "[SystemNPC] " + ChatColor.WHITE + "You have to get " + dirt_block +"!");
            }while(dirt <=10);
        }
     
  6. I am unsure that you understand what do-while() does... the current code first executes the code inside the do code block and then checks if "dirt" is less or equal than 10, if it is true then it repeats the code from the do code block... and again and again and again until the condition from the while() returns false... I don't think that's what you want, message spam.

    So, what exacly do you need to achieve ?
     
  7. Offline

    HaCkTiC_HaWkZ

    oh the way the video explained it was different..... :L well what i wanted to achieve was,

    Tell the player everytime they destroy a dirt block how many blocks left they have to destroy till like for example " 10 blocks = 10 exp" then it moves onto another code that makes it 25 blocks for 15 exp if you know what i mean :) xD and do this for all kinds of ores and things, at first i was thinking hash map but.....they are my weakness :( i dont really undertand how they work.

    Also is there something like drawText() onto the screen? sorry i dont really know if theres a java bukkit option for it, the other thing was to put in the corner a score of how much blocks the person has destroyed :D
     
  8. Hmm, I just noticed you have an invalid syntax here: "dirt_block = ++;", that's supposed to be "dirt_block++;".

    Yes but you're not being specific enough, is this a quest system or something ? Because you're making it pretty specific and vague at the same time =)
    Also, you want this for each player ? Because you only defined an integer for each block it seems, that will be shared between everybody... you'll have to use a HashMap to store stuff for each player.
    A simple example (not really usable for flexibility tough):
    Code:
    private HashMap<String, Integer> destroyed = new HashMap<String, Integer>();
    
    // in your event:
    
    Player player = event.getPlayer();
    String playerName = player.getName();
    
    if(event.getBlock().getType() == Material.DIRT)
    {
        Integer blocks = destroyed.get(playerName);
    
        if(blocks == null) // entry doesn't exist, let's add it
        {
             blocks = 0; // start at 0 because we'll increment it afterwards
             destroyed.put([playerName, blocks);
        }
    
        blocks++; // increment value
    
        if(blocks < 10)
        {
             player.sendMessage("You have " + (10 - blocks) + " dirt blocks left to destroy...");
        }
        else
        {
             // rewards or whatever
        }
    }
    But this example only works for one block type and until 10 blocks only... for something more flexible you'll need more flexible code... first you'll have to lay out the entire plan so you can design it right.

    No, for that you'll need a client mod to comunicate with the server... like Spout with SpoutCraft.
     
  9. Offline

    HaCkTiC_HaWkZ

    Thank you for your time :D and i shall look into hash maps and try to make my system work, and its like quests but not quests but this is really helpful i really appreciate your time :) and i see xD well i guess ill just stick with finding a new different way to say what is left then :), thanks again for all your help :D
     
Thread Status:
Not open for further replies.

Share This Page