Method Help

Discussion in 'Plugin Development' started by roflcopter399, Jul 4, 2012.

  1. Offline

    roflcopter399

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I don't know how to setup randomNumber method so that it runs when I want it to under public boolean
    Code:
    package me.thedirtydan.AntiSwear;
     
    import java.util.Random;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
     
        Logger log;
        Random random = new Random();
       
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            Logger log = this.getServer().getLogger();
            log.info(pdfFile.getName() + " has been disabled");
        }
       
        public void onEnable(){
            getServer().getPluginManager().registerEvents(new MainListener(), this);
            PluginDescriptionFile pdfFile = this.getDescription();
            Logger log = this.getServer().getLogger();
            log.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
     
                  public void run() {
                      getServer().broadcastMessage(randomNumber);
                  }
                }, 60L, 200L);
     
           
            return true;
        }
       
       
        public void randomNumber(String t){
        String alphaNumerics = "qwertyuiopasdfghjklzxcvbnm1234567890";
        t = "";
        for (int i = 0; i < 8; i++) {
              t += alphaNumerics.charAt(random.nextInt()* alphaNumerics.length());
                }
        }
    }
  2. Offline

    Milkywayz BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    To call a void method in java you just use:
    EDIT: (this is just in your case its different all the time)
    Code:
    randomNumber(//a String here);
    Just put that in your onCommand i guess is where you where specifying. Hope i helped :D
    For other methods you need to call say you need to call
    Code:
    public int add(int x, int y, int z) {
    return x+y+z;
    }
    You would use :
    Code:
    add(7,4,6);
    
    And get you get 17, you can use it to compare as well for example:
    Code:
    if(15 > add(7,5,5) {
    //stuff
    }
    

    This post has been edited 1 time. It was last edited by Milkywayz Jul 4, 2012.
  3. Offline

    roflcopter399

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    so you are saying it would look like this?
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
          randomNumber(String t);
    }
    ?

    This post has been edited 1 time. It was last edited by roflcopter399 Jul 4, 2012.
  4. Offline

    Milkywayz BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Uh you wouldnt pass in String t, you would pass in a string variable or just a string like :
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
          randomNumber("stringy");
    }
  5. Offline

    roflcopter399

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    That still doesn't help, I am trying to apply that method so that the server broadcasts the randomly generated number that is created in the method.
  6. Offline

    Milkywayz BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Well than you need to make a method that makes a random number. Make that method return an Integer, then broadcast that integer.
  7. Offline

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    you never passing the number from the methode to the outside
  8. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    Bukkit.getServer().broadcastMessage(String message);
    Now assuming you have generated a number called "randomNumber", here's how to broadcast it:
    Code:
    Bukkit.getServer().broadcastMessage("The randomly generated number is: " + randomNumber);
    You will have to import org.bukkit.Bukkit
  9. Offline

    roflcopter399

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It gives me an error with the randomNumber under the randomNumber in the broadcast, and it gives me the option to create a constant, create field, etc. .
  10. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You also need to change your randomNumber method from accepting a String parameter to returning a String.
  11. Offline

    roflcopter399

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So like
    Code:
    public String randomNumber(String t){
        String alphaNumerics = "qwertyuiopasdfghjklzxcvbnm1234567890";
        t = "";
        for (int i = 0; i < 8; i++) {
              t += alphaNumerics.charAt(random.nextInt()* alphaNumerics.length());
                }
            return alphaNumerics;
        }

Share This Page