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? Opps, wrong section EDIT by Moderator: merged posts, please use the edit button instead of double posting.
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]; } }
.. 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; } }
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; } }
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
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; } }
@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(); }
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!
What's your email XD 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.