Skip to content

Commit e48aa53

Browse files
committed
Should be functional...
1 parent 84eb108 commit e48aa53

2 files changed

Lines changed: 94 additions & 78 deletions

File tree

src/main/java/com/perceivedev/clicktpa/ClickableTPA.java

Lines changed: 85 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,99 @@
22

33
import org.bukkit.Bukkit;
44
import org.bukkit.ChatColor;
5+
import org.bukkit.configuration.file.FileConfiguration;
56
import org.bukkit.entity.Player;
67
import org.bukkit.event.EventHandler;
8+
import org.bukkit.event.EventPriority;
79
import org.bukkit.event.Listener;
810
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
911
import org.bukkit.plugin.java.JavaPlugin;
12+
import org.bukkit.scheduler.BukkitRunnable;
1013

1114
public 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
}

src/main/resources/config.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
tpa-command-permission: "essentials.tpa" # The permission for running the tpa command
1+
permission:
2+
tpa: "essentials.tpa" # The permission for running the tpa command
3+
tpahere: "essentials.tpahere" # The permission for running the tpahere command
4+
25
## Placeholders
36
# %player% = The player that ran the /tpa command
47
# %accept% = The clickable text to accept the request
58
# %deny% = The clickable text to deny the request
69
message:
7-
line-1: "&2%player% &6has requested to teleport to you!" # Line one of the message to send to the player
8-
line-2: "&6Click %accept% or %deny%" # Line two of the message to send to the player (Leave empty for no second line, i.e "")
9-
accept: "&2Accept" # The text of the %accept% placeholder
10-
deny: "&cDeny" # The text of the %deny% placeholder
11-
use-display-name: false # If true use the display name rather than the real name for the %player% placeholder
10+
spacer: "bar" # If this is 'bar' then it uses a long horizontal bar, if not then it uses the value as the spacer. Leave empty for no spacer.
11+
line: "&6Click %accept% &6or %deny%" # The message to send to the player
12+
accept: "&aAccept" # The text of the %accept% placeholder
13+
deny: "&cDeny" # The text of the %deny% placeholder
14+
use-display-name: true # If true use the display name rather than the real name for the %player% placeholder

0 commit comments

Comments
 (0)