Multiple Questions.

Discussion in 'Plugin Development' started by Stantastic, Nov 13, 2011.

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

    Stantastic

    Im searching for an explanation on how to use "Register"... especially how to add money by command/event. I allready searched the forum but havent found anything. Im also interested in a timer/countdown which triggers an event and i need to know how to get the playercount!

    Thanks for any help on this!

    Stan
     
  2. Register:
    Add the register jar as an external jar to your project.
    Code:
    if(Methods.hasMethod()) { //checks if register has selected an economy plugin
                        if(Methods.getMethod().hasAccount("playername")) { //checks if player has an account on said plugin
                            Methods.getMethod().getAccount("playername").add(100.0); //adds 100 to that player's acount
                        }
                    }
    My plugin MoneyDrop uses Register. The source is included in the jar if you want some more examples.

    As for the countdown, check out yourplugin.getServer().getScheduler().scheduleAsyncDelayedTask(yourplugin, new Runnable(), delay);
    This will run the Runnable after the delay has passed. Use repeatingTask if you actually want a timer.

    And the playercount, yourplugin.getServer().getOnlinePlayers().length

    Hope this helped!
     
    Stantastic likes this.
  3. Never use Async tasks if you plan to interact with the bukkit API...
     
  4. Offline

    Stantastic

    @V10lator what you mean with "Never use Async tasks if you plan to interact with the bukkit API..." what should i do instead then?
     
  5. Use the yourplugin.getServer().getScheduler().scheduleSyncDelayedTask(yourplugin, new Runnable(), delay) method, instead of the Async one.
    Async doesn't stress the server as much, but may cause synchronization conflicts if you're interacting with bukkit.
     
    Stantastic likes this.
  6. Offline

    Stantastic

    @DrakeSpirit so i do
    PHP:
    yourplugin.getServer().getScheduler().scheduleSyncDelayedTask(yourplugin, new Runnable(), delay){ //nowdostuffhere}
    ? is the delay in seconds?
     
  7. @Stantastic
    No, you have to make a Runnable class yourself, then use that one instead of the new Runnable() part.
    The easiest way to do that is make a new class, then type
    public class YourClass implements Runnable {
    Eclipse or Netbeans will then suggest you override the run method.
    The code in your run method will be executed by the scheduler.
    Look up some info on java threads if you want to learn more about it.

    The delay is in server ticks, that means 1 second = 20 ticks.
     
    Stantastic likes this.
  8. Offline

    Stantastic

    allright i got it! now back to
    Code:
    if(Methods.hasMethod()) { //checks if register has selected an economy plugin
                        if(Methods.getMethod().hasAccount("playername")) { //checks if player has an account on said plugin
                            Methods.getMethod().getAccount("playername").add(100.0); //adds 100 to that player's acount
                        }
                    }
    again.

    how do add money to all online players accounts? do i need a loop for this?
     
  9. Offline

    ZNickq

    Yeah, you need a loop:
    Player[] allplrs = this.getServer().getOnlinePlayer();
    for(Player curplr:allplrs)
    {
    //add money to curplr
    }
     
    Stantastic likes this.
  10. Offline

    Stantastic

    @ZNickq help me! =) please

    Code:
    int runint = getConfig().getInt("interval");
            tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    
                public void run() {
    
                    if(Methods.hasMethod()) {
    
                        String playername = "Stantastic";
    
                        if(Methods.getMethod().hasAccount(playername)) {
                            Methods.getMethod().getAccount(playername).add( getConfig().getInt("interest") * getServer().getOnlinePlayers().length * getConfig().getInt("multiplier") );
    
                             int inter = getConfig().getInt("interest") * getServer().getOnlinePlayers().length * getConfig().getInt("multiplier");
                             int plcount = getServer().getOnlinePlayers().length;
    
                            Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "[SPB-Network] " + ChatColor.GOLD +"You gained " + ChatColor.BLUE + inter + ChatColor.GOLD + " Interest! Because there are " + ChatColor.BLUE + plcount + ChatColor.GOLD + " Players online!"  );
                        }
                    }
    
                }
    
            }, 0, runint * 20);
        }
    
     
  11. Oh, come on @Stantastic , you know how to write loops :p

    Here's the slightly longer version of the for loop if that's clearer for you.
    Code:
    String playername = "";
    Player[] players = getServer().getOnlinePlayers();
    for(int i = 0; i < players.length; i++) {
        playername = players[i].getName();
        //if(Methods.getMethod().hasAccount(playername)) { etc
    }
     
    Stantastic likes this.
  12. Offline

    Stantastic

    well im not that pro =D wasted around 1 day with java now so im just new =D
     
  13. Offline

    Xandaros

    The "normal" for-loop works like this:
    You have one instruction that is executed before the loop is run.
    You have one instruction that returns whether to continue to loop or not.
    You have one instruction that gets executes every run.
    And you have one instruction that also gets executed every run, but is the "content" of the loop. I have no idea why we separate them, though...

    So your for loop looks about like this:

    Code:
    for (first; continue; every) every;
    
    let's try to make that a usable example:

    Code:
    for (int i = 0; i < 10; i++) System.out.println(i);
    
    This will print the digits 0 to 9 in the console.
    Read it as follows: Initialize Integer variable i with a value of 0, repeat the the loop while i is smaller than 10, increase i every run, print i every run.

    If you want to have more than two statements every run, you have to put the "content" into curly brackets(these: {}).
    so, if we wanted to print the number and then do some other stuff, we'd do it like this:

    Code:
    for (int i = 0; i < 10; i++)
    {
            System.out.println(i);
            //do some other stuff
    }
    
    I hope that clarifies things for you.
     
  14. Offline

    Stantastic

    got the plugin allready working on my server but thanks =)

    also thanks to all the others whoi helped me with this!
     
Thread Status:
Not open for further replies.

Share This Page