How?

Discussion in 'Plugin Development' started by nubpro, Jul 5, 2011.

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

    nubpro

    Is there are code which can works like this?
    Which could auto changed itself by typing /change?
    Eg:
    /change
    Show:
    Hello!
    Re-type again and Show:
    Bye!
    Which could show arranged the lists?
    :D

    Opps, wrong section

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  2. Offline

    krinsdeath

    Not sure what the function of this would be, aside from maybe a "random tips" type thing, but you could store a series of phrases in a data structure:

    Code:
    public class PhraseList {
    	public String[] phrases;
    	public int index;
    	
    	public PhraseList() {
    		index = 0;
    		// TODO: Load lines from a file or configuration
    		phrases[0] = "Hello!";
    		phrases[1] = "Bye!";
    	}
    	
    	public String next() {
    		index++;
    		if (index > phrases.length) { index = 0; }
    		return phrases[index];
    	}
    }
     
  3. Offline

    nubpro

    What if I have more than two phares?
     
  4. Offline

    krinsdeath

    .. well, it was a very simple representation of a static class. If you wanted something more verbose:

    Code:
    public class PhraseList {
    	public String[] phrases;
    	public int index;
    	
    	public PhraseList() {
    		index = 0;
    		// TODO: Load lines from a file or configuration
    		this.add("Hello!");
    		this.add("Hello v2.0!");
    		this.add("Insert next message here ->");
    		this.add("Phrases are fun!");
    		this.add("Bye!");
    	}
    	
    	public String next() {
    		index++;
    		if (index > phrases.length) { index = 0; }
    		return phrases[index];
    	}
    	
    	public void add(String msg) {
    		phrases[phrases.length + 1] = msg;
    	}
    }
     
  5. Offline

    nubpro

    Thank you so much, I'll try that later
    :D
     
  6. Offline

    nubpro

    Where should I put commands?
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
        if (cmd.getName().equalsIgnoreCase("test")){
    
    Do you think it's correct?
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
        if (cmd.getName().equalsIgnoreCase("chat")){    
            index = 0;
            String fullmessage= "";
            this.add("Hello!" + fullmessage);
            this.add("Hello v2.0!");
            this.add("Insert next message here ->");
            this.add("Phrases are fun!");
            this.add("Bye!"); }
        return false;
        }    
        
        public String next() {
            index++;
            if (index > phrases.length) { index = 0; }
            return phrases[index];
        }
        
        public void add(String msg) {
            phrases[phrases.length + 1] = msg;
        }
    }
    
    
    
     
  7. Offline

    krinsdeath

    Wow, this was a while ago. That class would just be a utility class that you access from within your normal plugin (some class that extends JavaPlugin).

    As for including commands, you'd do something like:

    Code:
    public class Test extends JavaPlugin {
      public void onEnable() {
        // do stuff
      }
    
      public void onDisable() {
        // do stuff?
      }
    
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("test")) {
          // 'test' command handler
        }
      }
    }
    
    then, in your plugin.yml

    Code:
    name: Test
    main: me.yourname.test.Test
    version: 0.1
    authors: [you]
    
    commands:
      test:
        description: use the /test command
        usage: |
          /<command>                             - show this message
    
     
  8. Offline

    nubpro

    @krinsdeath
    :D
    Thanks but I already knew the basics.
     
  9. Offline

    krinsdeath

    I recommend you read some tutorials on Java. You want that as a separate class, and you'd create another method inside of it:

    Code:
    import java.util.Random;
    public class PhraseList {
      // your other code for PhraseList is here
    
      public String random() {
        Random rand = new Random(System.currentTimeMillis());
        return phrases[rand.nextInt(phrases.length)];
      }
    }
    
    This will generate some random phrase for you. In your main class (onEnable()), you'd do something like:

    Code:
    public void onEnable() {
      PhraseList phrases = new PhraseList();
    }
    
    and in your onCommand...

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      if (label.equalsIgnoreCase("chat")) {
        sender.sendMessage(phrases.random());
        return true;
      } else {
        return false;
      }
    }
    
     
  10. Offline

    nubpro

    @krinsdeath
    Code:
    import java.util.Random; public class PhraseList { //Correct?
    
     this.add("Hello!" + fullmessage);
    this.add("Hello v2.0!");
    this.add("Insert next message here ->");
    this.add("Phrases are fun!");  this.add("Bye!"); }
    
    public String random() {
     Random rand = new Random(System.currentTimeMillis());
     return phrases[rand.nextInt(phrases.length)];   } }
    
    Where should I put this on my main extends JavaPlugin or new class with code phrase list?
    Code:
    public void onEnable() {   PhraseList phrases = new PhraseList(); }
    
     
  11. Offline

    krinsdeath

    Like I said, bud, you need to read some basic Java tutorials and get the hang of what classes actually are before I can explain this to you. I've already written the plugin for you, it's up to you to figure out how it pieces together.

    Keep at it!
     
  12. Offline

    nubpro

    What's your email XD
    :D
    I will try to figured out.
    Do you know I didn't read anything before I started coding?
    I'm not really a professional and well trained Plugin Developer, but I'm still good in making PHP and sites.
     
Thread Status:
Not open for further replies.

Share This Page