-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathSkriptAddon.java
More file actions
171 lines (143 loc) · 5.6 KB
/
SkriptAddon.java
File metadata and controls
171 lines (143 loc) · 5.6 KB
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
162
163
164
165
166
167
168
169
170
171
package ch.njol.skript;
import java.io.File;
import java.io.IOException;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;
import ch.njol.skript.util.Utils;
import ch.njol.skript.util.Version;
import ch.njol.skript.variables.VariableStorage;
import ch.njol.skript.variables.Variables;
import org.jetbrains.annotations.ApiStatus;
import org.skriptlang.skript.localization.Localizer;
import org.skriptlang.skript.registration.SyntaxRegistry;
import org.skriptlang.skript.util.Registry;
/**
* Utility class for Skript addons. Use {@link Skript#registerAddon(JavaPlugin)} to create a SkriptAddon instance for your plugin.
* @deprecated Use {@link org.skriptlang.skript.addon.SkriptAddon} instead.
* Register using {@link org.skriptlang.skript.Skript#registerAddon(Class, String)}.
* Obtain a Skript instance with {@link Skript#instance()}.
*/
@Deprecated(since = "2.14", forRemoval = true)
public final class SkriptAddon implements org.skriptlang.skript.addon.SkriptAddon {
public final JavaPlugin plugin;
public final Version version;
private final String name;
private final org.skriptlang.skript.addon.SkriptAddon addon;
/**
* Package-private constructor. Use {@link Skript#registerAddon(JavaPlugin)} to get a SkriptAddon for your plugin.
*/
SkriptAddon(JavaPlugin plugin) {
this(plugin, Skript.instance().registerAddon(plugin.getClass(), plugin.getName()));
}
SkriptAddon(JavaPlugin plugin, org.skriptlang.skript.addon.SkriptAddon addon) {
this.addon = addon;
this.plugin = plugin;
this.name = plugin.getName();
Version version;
try {
version = new Version(plugin.getDescription().getVersion());
} catch (IllegalArgumentException e) {
final Matcher m = Pattern.compile("(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?").matcher(plugin.getDescription().getVersion());
if (!m.find())
throw new IllegalArgumentException("The version of the plugin " + name + " does not contain any numbers: " + plugin.getDescription().getVersion());
version = new Version(Utils.parseInt(m.group(1)), m.group(2) == null ? 0 : Utils.parseInt(m.group(2)), m.group(3) == null ? 0 : Utils.parseInt(m.group(3)));
Skript.warning("The plugin " + name + " uses a non-standard version syntax: '" + plugin.getDescription().getVersion() + "'. Skript will use " + version + " instead.");
}
this.version = version;
}
@Override
public final String toString() {
return getName();
}
public String getName() {
return name;
}
/**
* Loads classes of the plugin by package. Useful for registering many syntax elements like Skript does it.
*
* @param basePackage The base package to add to all sub packages, e.g. <tt>"ch.njol.skript"</tt>.
* @param subPackages Which subpackages of the base package should be loaded, e.g. <tt>"expressions", "conditions", "effects"</tt>. Subpackages of these packages will be loaded
* as well. Use an empty array to load all subpackages of the base package.
* @throws IOException If some error occurred attempting to read the plugin's jar file.
* @return This SkriptAddon
*/
public SkriptAddon loadClasses(String basePackage, String... subPackages) throws IOException {
Utils.getClasses(plugin, basePackage, subPackages);
return this;
}
/**
* Makes Skript load language files from the specified directory, e.g. "lang" or "skript lang" if you have a lang folder yourself. Localised files will be read from the
* plugin's jar and the plugin's data folder, but the default English file is only taken from the jar and <b>must</b> exist!
*
* @param directory Directory name
* @return This SkriptAddon
*/
public SkriptAddon setLanguageFileDirectory(String directory) {
localizer().setSourceDirectories(directory, plugin.getDataFolder().getAbsolutePath() + directory);
return this;
}
@Nullable
public String getLanguageFileDirectory() {
return localizer().languageFileDirectory();
}
@Nullable
private File file;
/**
* The first invocation of this method uses reflection to invoke the protected method {@link JavaPlugin#getFile()} to get the plugin's jar file.
* The file is then cached and returned upon subsequent calls to this method to reduce usage of reflection.
* Only nullable if there was an exception thrown.
*
* @return The jar file of the plugin.
*/
@Nullable
public File getFile() {
if (file == null)
file = Utils.getFile(plugin);
return file;
}
//
// Modern SkriptAddon Compatibility
//
static SkriptAddon fromModern(org.skriptlang.skript.addon.SkriptAddon addon) {
return new SkriptAddon(JavaPlugin.getProvidingPlugin(addon.source()), addon);
}
@Override
public Class<?> source() {
return addon.source();
}
@Override
public String name() {
return addon.name();
}
@Override
public <R extends Registry<?>> void storeRegistry(Class<R> registryClass, R registry) {
addon.storeRegistry(registryClass, registry);
}
@Override
public void removeRegistry(Class<? extends Registry<?>> registryClass) {
addon.removeRegistry(registryClass);
}
@Override
public boolean hasRegistry(Class<? extends Registry<?>> registryClass) {
return addon.hasRegistry(registryClass);
}
@Override
public <R extends Registry<?>> R registry(Class<R> registryClass) {
return addon.registry(registryClass);
}
@Override
public <R extends Registry<?>> R registry(Class<R> registryClass, Supplier<R> putIfAbsent) {
return addon.registry(registryClass, putIfAbsent);
}
@Override
public SyntaxRegistry syntaxRegistry() {
return addon.syntaxRegistry();
}
@Override
public Localizer localizer() {
return addon.localizer();
}
}