api/Create your own variables (1.8+)
Attention: This is for the 1.8 update and does not work with earlier versions
In this small tutorial we will show you how to add your own variables to ServerCaster.
We will show you the code and then explain what every line of code does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.ArrayList; import me.servercaster.core.ServerCaster; import me.servercaster.core.event.CastListener; import me.servercaster.core.event.PreCastEvent; import me.servercaster.core.event.PreCastPlayerEvent; import org.bukkit.plugin.java.JavaPlugin; /** * * @author Patrick Beuks (killje) and Floris Huizinga (Flexo013) */ class VersionVariable implements CastListener { private final JavaPlugin instance; public VersionVariable(JavaPlugin instance) { this.instance = instance; ServerCaster.addMessageListener(instance, this); } @Override public void castHandler(PreCastEvent pce) { ArrayList<String> messages = pce.getMessages(); ArrayList<String> newMessages = new ArrayList<>(); for (String string : messages) { string = string.replaceAll("(?i)%VERSION%", instance.getServer().getVersion()); newMessages.add(string); } pce.setMessages(messages); } @Override public void castPlayerHandler(PreCastPlayerEvent pcpe) { } } |
On line 12 you see that this class implements from CastListener. By doing this you get to override castHandler(PreCastEvent pce) and castPlayerHandler(PreCastPlayerEvent pcpe) (lines 22 and 33).
On line 14 you see JavaPlugin. This is the class from which your plugin extends. When you create a new instance of this file, you can just give that object with the arguments.
On line 18 you can see that we add this new listener to the plugin. Every time that a message is sent castHandler(PreCastEvent pce) and castPlayerHandler(PreCastPlayerEvent pcpe) will be executed.
On line 23 we get the messages that would be sent out to players.
On line 24 we make a new list of messages that we want to send (we add all the messages one by one inside the for-loop (line 27) and set those messages (line 29)).
On line 26 we look for the variable %VERSION% and replace it with the server's version. (The replaceAll function accept regular expressions and with the "(?i)" we show that we don't care about capitalization so inside the messages of ServerCaster you can also use %version% or %VeRsiON%)
Comments