/
Functions.java
161 lines (128 loc) · 4.46 KB
/
Functions.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package me.pseudoknight.chplaceholderapi;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.api;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.Static;
import com.laytonsmith.core.constructs.*;
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.exceptions.CRE.CREFormatException;
import com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException;
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import com.laytonsmith.core.functions.AbstractFunction;
import com.laytonsmith.core.natives.interfaces.Mixed;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
public class Functions {
public static String docs() {
return "Functions for the PlaceholderAPI plugin.";
}
@api
public static class set_placeholders extends AbstractFunction {
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREPlayerOfflineException.class};
}
public boolean isRestricted() {
return true;
}
public Boolean runAsync() {
return false;
}
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
OfflinePlayer p = null;
if(args.length == 2 && !(args[0] instanceof CNull) && !args[0].val().equals(Static.getConsoleName())) {
p = (OfflinePlayer) Static.GetUser(args[0], t).getHandle();
}
String message = args[args.length - 1].val();
message = ChatColor.translateAlternateColorCodes('&', message);
return new CString(PlaceholderAPI.setPlaceholders(p, message), t);
}
public String getName() {
return "set_placeholders";
}
public Integer[] numArgs() {
return new Integer[]{1, 2};
}
public String docs() {
return "string {[player], string} Replaces all placeholders in the given string."
+ " Player can be null or absent if player context is not necessary for the given placeholders."
+ " Offline players are supported by some placeholders."
+ " Automatically \"colorizes\" the returned string.";
}
public Version since() {
return MSVersion.V3_3_2;
}
}
@api
public static class register_placeholder_hook extends AbstractFunction {
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREFormatException.class};
}
public boolean isRestricted() {
return true;
}
public Boolean runAsync() {
return false;
}
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
String id = args[0].val();
CClosure closure;
if(args[1] instanceof CClosure) {
closure = (CClosure) args[1];
} else {
throw new CREFormatException("This must be a closure.", t);
}
try {
return CBoolean.get(new Placeholders(id, closure).register());
} catch(IllegalArgumentException ex){
throw new CREFormatException("Invalid identifier.", t);
}
}
public String getName() {
return "register_placeholder_hook";
}
public Integer[] numArgs() {
return new Integer[]{2};
}
public String docs() {
return "boolean {identifier, closure} Registers a PlaceholderAPI identifier. When the identifier is used in a"
+ " placeholder, it executes the given closure. The closure will be passed the player name"
+ " (or null) and the particular placeholder name that follows the identifier"
+ " (eg. \"%id_placeholder_name%\") as variables. Use return() in the"
+ " closure to specify the output for each placeholder name you're checking for."
+ " Returns true if the placeholder was successfully registered.";
}
public Version since() {
return MSVersion.V3_3_2;
}
}
@api
public static class unregister_placeholder_hook extends AbstractFunction {
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{};
}
public boolean isRestricted() {
return true;
}
public Boolean runAsync() {
return false;
}
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
return CBoolean.get(Placeholders.unregister(args[0].val()));
}
public String getName() {
return "unregister_placeholder_hook";
}
public Integer[] numArgs() {
return new Integer[]{1};
}
public String docs() {
return "boolean {identifier} Unregisters a PlaceholderAPI identifier."
+ " Returns true if a placeholder by that id existed.";
}
public Version since() {
return MSVersion.V3_3_2;
}
}
}