22
33import org .bukkit .Bukkit ;
44import org .bukkit .ChatColor ;
5+ import org .bukkit .configuration .file .FileConfiguration ;
56import org .bukkit .entity .Player ;
67import org .bukkit .event .EventHandler ;
8+ import org .bukkit .event .EventPriority ;
79import org .bukkit .event .Listener ;
810import org .bukkit .event .player .PlayerCommandPreprocessEvent ;
911import org .bukkit .plugin .java .JavaPlugin ;
12+ import org .bukkit .scheduler .BukkitRunnable ;
1013
1114public class ClickableTPA extends JavaPlugin implements Listener {
1215
13- public void onEnable () {
14- // Register the listener
15- getServer ().getPluginManager ().registerEvents (this , this );
16- // Save the default config
17- saveDefaultConfig ();
18- }
16+ public void onEnable () {
17+ // Register the listener
18+ getServer ().getPluginManager ().registerEvents (this , this );
19+ // Save the default config
20+ saveDefaultConfig ();
21+ }
1922
20- @ EventHandler
21- public void onCommandRun (PlayerCommandPreprocessEvent event ) {
22- String command = event .getMessage ().substring (1 );
23- if (command .split (" " )[0 ].equalsIgnoreCase ("tpa" )) {
24- // Command run is "/tpa"
25- if (event .getPlayer ().hasPermission (getConfig ().getString ("tpa-command-permission" ))) {
26- // Sender has permission
27- if (command .split (" " ).length == 2 ) {
28- // Has player as argument
29- if (Bukkit .getPlayer (command .split (" " )[1 ]) != null ) {
30- // Valid player (and online)
31- sendMessages (Bukkit .getPlayer (command .split (" " )[1 ]));
32- }
33- }
34- }
35- }
36- }
37-
38- private JSONMessage getFirstLine (Player player ) {
39- boolean useDisplayName = getConfig ().getBoolean ("message.use-display-name" );
40- JSONMessage message = JSONMessage .create ();
41- if (getConfig ().getString ("message.line-1" ) != "" ) {
42- String [] words = getConfig ().getString ("message.line-1" ).split (" " );
43- for (int i = 0 ; i < words .length ; i ++) {
44- String word = trans (words [i ].replace ("%player%" , useDisplayName ? player .getDisplayName () : player .getName ()));
45- if (word .equalsIgnoreCase ("%accept%" )) {
46- message .then (word ).runCommand ("tpaccept" );
47- } else if (word .equalsIgnoreCase ("%deny%" )) {
48- message .then (word ).runCommand ("tpdeny" );
49- } else {
50- message .then (word );
51- }
52- message .then (" " );
53- }
54- }
55- return message ;
56- }
57-
58- private JSONMessage getSecondLine (Player player ) {
59- boolean useDisplayName = getConfig ().getBoolean ("message.use-display-name" );
60- JSONMessage message = JSONMessage .create ();
61- if (getConfig ().getString ("message.line-2" ) != "" ) {
62- String [] words = getConfig ().getString ("message.line-2" ).split (" " );
63- for (int i = 0 ; i < words .length ; i ++) {
64- String word = trans (words [i ].replace ("%player%" , useDisplayName ? player .getDisplayName () : player .getName ()));
65- if (word .equalsIgnoreCase ("%accept%" )) {
66- message .then (word ).runCommand ("tpaccept" );
67- } else if (word .equalsIgnoreCase ("%deny%" )) {
68- message .then (word ).runCommand ("tpdeny" );
69- } else {
70- message .then (word );
71- }
72- message .then (" " );
73- }
74- }
75- return message ;
76- }
77-
78- private void sendMessages (Player player ) {
79- getFirstLine (player ).send (player );
80- getSecondLine (player ).send (player );
81- }
82-
83- private String trans (String string ) {
84- return ChatColor .translateAlternateColorCodes ('&' , string );
85- }
23+ @ EventHandler (priority = EventPriority .LOWEST )
24+ public void onCommandRun (PlayerCommandPreprocessEvent event ) {
25+ if (event .isCancelled ()) {
26+ return ;
27+ }
28+
29+ String command = event .getMessage ().substring (1 );
30+ String [] split = command .split (" " );
31+ if (split [0 ].equalsIgnoreCase ("tpa" )) {
32+ // Command run is "/tpa"
33+ handleCommand (event .getPlayer (), split , "tpa" );
34+ } else if (split [0 ].equalsIgnoreCase ("tpahere" )) {
35+ // Command run is "/tpahere"
36+ handleCommand (event .getPlayer (), split , "tpahere" );
37+ }
38+ }
39+
40+ private void handleCommand (Player player , String [] command , String permission ) {
41+ if (player .hasPermission (getConfig ().getString ("permission." + permission ))) {
42+ // Sender has permission
43+ if (command .length == 2 ) {
44+ // Has player as argument
45+ if (Bukkit .getPlayer (command [1 ]) != null ) {
46+ // Valid player (and online)
47+ new BukkitRunnable () {
48+ @ Override
49+ public void run () {
50+ sendMessages (Bukkit .getPlayer (command [1 ]));
51+ }
52+ }.runTaskLater (this , 1 );
53+ }
54+ }
55+ }
56+ }
57+
58+ private JSONMessage getLine (Player player , String configPath ) {
59+ FileConfiguration config = getConfig ();
60+ boolean useDisplayName = config .getBoolean ("message.use-display-name" );
61+ JSONMessage message = JSONMessage .create ();
62+ if (config .getString ("message." + configPath ) != "" ) {
63+ String [] words = config .getString ("message." + configPath ).split (" " );
64+ for (int i = 0 ; i < words .length ; i ++) {
65+ String word = trans (words [i ].replace ("%player%" , useDisplayName ? player .getDisplayName () : player .getName ()));
66+ if (word .equalsIgnoreCase ("%accept%" )) {
67+ message .then (trans (config .getString ("message.accept" ))).runCommand ("/tpaccept" );
68+ } else if (word .equalsIgnoreCase ("%deny%" )) {
69+ message .then (trans (config .getString ("message.deny" ))).runCommand ("/tpdeny" );
70+ } else {
71+ message .then (word ).color (ChatColor .GOLD );
72+ }
73+ message .then (" " );
74+ }
75+ }
76+ return message ;
77+ }
78+
79+ private void sendMessages (Player player ) {
80+ String spacer = getConfig ().getString ("message.spacer" );
81+ sendSpacer (player , spacer );
82+ getLine (player , "line" ).send (player );
83+ sendSpacer (player , spacer );
84+ }
85+
86+ private void sendSpacer (Player player , String spacer ) {
87+ if (spacer .equals ("" )) {
88+ return ;
89+ } else if (spacer .equals ("bar" )) {
90+ JSONMessage .create ().bar ().send (player );
91+ } else {
92+ player .sendMessage (trans (spacer ));
93+ }
94+ }
95+
96+ private String trans (String string ) {
97+ return ChatColor .translateAlternateColorCodes ('&' , string );
98+ }
8699
87100}
0 commit comments