Programming a Guide
Programming a Guide
Programming a Guide
Prepering the Segments
A guide runs different Segments.
A Segment requires some parameters:
- ID
:
int - The ID of the Segment: 0, 1, 2, ...
- Text
:
String - The text to send to the player.
- exitBySkip
:
boolean - If the guide exits, when the segment should skipped.
- spaceable
:
boolean - If the answer of the player can contain spaces.
- backSeg
:
int - The ID of the Segment to go to, when the player uses "back".
- skipSeg
:
int - The ID of the Segment to go to, when the player uses "skip" or "next".
Segments can look like this:
Segment seg1 = new Segment(0, "Whats your name?", false, true, -1, 1); Segment seg2 = new Segment(1, "Do you want to play survival or creative?", false, false, 0, -1); Segment seg3 = new Segment(2, "Do friends of you already playing on this server?", true, false, 1, 2);
Notice:
- Using "back" at the first segment will exit the guide. "Skip" will continue with the second segment.
- Typing "cre a tive" at the second segment will be corrected to "creative", because spaces aren't allowed here.
- Using "back" at the second segment will go back to the first one. "Skip" is not allowed here.
- Using "skip" at the third segment will exit the guide.
- Make sure the skipSeg is 2, and nothing other!
The array of segments could look like this:
Segment[] segs = {seg1, seg2, seg3};
Easy, isn't it?
Prepareing the Receiver
Every time the player type in a message, the Guide will scan it.
There are some "commands" / words, which will have an effect on the Guide:
String | Effect |
---|---|
"exit" / "stop" | Exits the Guide. |
"back" | Calls the backSeg . |
"skip" | Calls the skipSeg . |
Any other message from the player will be send to the receiver!
Create a class, which implements the GuideReceiver interface.
package mypackage; import org.bukkit.command.CommandSender; import de.gcmclan.team.guides.Guide; import de.gcmclan.team.guides.GuideReceiver; import de.gcmclan.team.guides.Segment; public class MyGuide implements GuideReceiver { public MyGuide(){} @Override public int receive(Guide guide, Segment seg, String msg) { // Do something with the msg. return nextSeg; // The segment, which should now be called. /* * The return value can also be: * -1 To exit the Guide without any announcement. * -2 The finish the Guide. This will send the player a message: "You finished the Guide!" (Guide will be replace by the guideSyn) * -3 Exits the Guide, telling the player, that the Guide was exited by the plugin. */ } @Override public void finish(CommandSender s) { //Save data, if you wish to. } @Override public boolean usesId(String id) { //Does this Guide use a ID of an element? (ID of segments are not meant.) //If yes, is it the given String id equal to the ID in use? //If no, leave it blank. } }
Inside the receive(...) method, you should handle the message.
For our example it could look like this:
@Override public int receive(Guide guide, Segment seg, String msg) { switch(seg.getID()) { case 0: customPlayer.setName(msg); return 1; case 1: if(msg.equalsIgnoreCase("survial")) { MsgSender.sInfo(guide.getExecutor(), guide.getCmdSender(), "You'll play in survival mode!"); customPlayer.setGameMode(0); return 2; } else if(msg.equalsIgnoreCase("creative")) { MsgSender.sInfo(guide.getExecutor(), guide.getCmdSender(), "You'll play in creative mode!"); customPlayer.setGameMode(1); return 2; } else { MsgSender.sBug(guide.getExecutor(), guide.getCmdSender(), "You cannot play " + msg + "!"); return 1; } case 2: TrinaryAnswer answer = MethodPool.getAnswer(msg); if(answer == TrinaryAnswer.POSITIVE) { MsgSender.sInfo(guide.getExecutor(), guide.getCmdSender(), "Fine. :-)"); finish(guide.getCmdSender()); return -2; } else if(answer == TrinaryAnswer.NEGATIVE) { MsgSender.sInfo(guide.getExecutor(), guide.getCmdSender(), "Oh dear. :-("); finish(guide.getCmdSender()); return -2; } else { MsgSender.sBug(guide.getExecutor(), guide.getCmdSender(), "Your answer should be yes or no!"); MsgSender.sWarn(guide.getExecutor(), guide.getCmdSender(), "You typed: " + msg); return 2; } } return nextSeg; //The segment, which should now be called. }
Parameters of a Guide
A guide requires some parameters:
- GuideManager
:
GuideManger - You can use the internal GuideManger
- You can also create your own guide manger
- GuideReceiver
:
GuideReceiver - see below
- Executor
:
Plugin - The excecuting plugin -> Your plugin!
- GuideSyn
:
String - A word for "Guide", like "Setup", "Editor" or "Welcome Guide".
- Player / Console
:
CommandSender - The player or the console using the guide.
- Segments
:
Segment[] - An array of segments.
Putting all together
Once, you prepared the Segments and the Receiver, you just have to fit all together:
- Make sure you followed the "Hook into GuidesPlugin"-introduction.
public void runGuide(Plugin myPlugin, CommandSender sender) { GuidesPlugin gp = (GuidesPlugin) this.getServer().getPluginManager().getPlugin("GuidesPlugin"); Segment seg1 = new Segment(0, "Whats your name?", false, true, -1, 1); Segment seg2 = new Segment(1, "Do you want to play survival or creative?", false, false, 0, -1); Segment seg3 = new Segment(2, "Do friends of you already playing on this server?", true, false, 1, 2); Segment[] segs = {seg1, seg2, seg3}; GudieReceiver gr = new MyGuide(); Guide myGuide = new Guide(gp, myReceiver, myPlugin, "Welcome-Guide", sender, segs); gp.addGuide(myGuide, true); // True here, to deactivate possible running Guides of the player / sender. }
Now your're done!
Comments