Skip to content

Commit ab30f82

Browse files
Implement ExprPlugin (#49)
Co-authored-by: TPGamesNL <[email protected]>
1 parent 4d5d1fb commit ab30f82

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

docs/basics/utilities.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,15 @@ Checks whether objects are instances of the given java types.
175175
Returns a reference to the class from the given java type. Returns an object of type `java.lang.Class`.
176176
This expression also supports primitive types, which doesn't require an import.
177177

178+
## Plugin instance
179+
180+
{% code-tabs %}
181+
{% code-tabs-item title="Syntax" %}
182+
```text
183+
[(an|the)] instance of [the] plugin %javatype/string%
184+
```
185+
{% endcode-tabs-item %}
186+
{% endcode-tabs %}
187+
188+
Returns the instance of the given plugin (either the name as a string, or the plugin class).
189+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.btk5h.skriptmirror.skript;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
5+
import ch.njol.skript.lang.Expression;
6+
import ch.njol.skript.lang.ExpressionType;
7+
import ch.njol.skript.lang.SkriptParser;
8+
import ch.njol.util.Kleenean;
9+
import com.btk5h.skriptmirror.JavaType;
10+
import com.btk5h.skriptmirror.ObjectWrapper;
11+
import com.btk5h.skriptmirror.skript.custom.CustomImport;
12+
import org.bukkit.Bukkit;
13+
import org.bukkit.plugin.Plugin;
14+
import org.bukkit.plugin.java.JavaPlugin;
15+
import org.eclipse.jdt.annotation.NonNull;
16+
17+
public class ExprPlugin extends SimplePropertyExpression<Object, ObjectWrapper> {
18+
19+
static {
20+
Skript.registerExpression(ExprPlugin.class, ObjectWrapper.class, ExpressionType.PROPERTY, "[(an|the)] instance of [the] plugin %javatype/string%");
21+
}
22+
23+
@Override
24+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
25+
super.init(exprs, matchedPattern, isDelayed, parseResult);
26+
27+
if (exprs[0] instanceof CustomImport.ImportHandler) {
28+
JavaType javaType = ((CustomImport.ImportHandler) exprs[0]).getJavaType();
29+
Class<?> clazz = javaType.getJavaClass();
30+
31+
if (!JavaPlugin.class.isAssignableFrom(clazz) || JavaPlugin.class.equals(clazz)) {
32+
Skript.error("The class " + clazz.getSimpleName() + " is not a plugin class");
33+
return false;
34+
}
35+
}
36+
37+
return true;
38+
}
39+
40+
@Override
41+
public ObjectWrapper convert(Object plugin) {
42+
if (plugin instanceof String) {
43+
String pluginName = (String) plugin;
44+
for (Plugin pluginInstance : Bukkit.getPluginManager().getPlugins()) {
45+
if (pluginInstance.getName().equalsIgnoreCase(pluginName)) {
46+
return ObjectWrapper.create(pluginInstance);
47+
}
48+
}
49+
50+
return null;
51+
} else {
52+
Class<?> clazz = ((JavaType) plugin).getJavaClass();
53+
54+
if (!JavaPlugin.class.isAssignableFrom(clazz) || JavaPlugin.class.equals(clazz)) {
55+
return null;
56+
}
57+
58+
return ObjectWrapper.create(JavaPlugin.getPlugin(clazz.asSubclass(JavaPlugin.class)));
59+
}
60+
}
61+
62+
@Override
63+
@NonNull
64+
public Class<? extends ObjectWrapper> getReturnType() {
65+
return ObjectWrapper.class;
66+
}
67+
68+
@Override
69+
@NonNull
70+
protected String getPropertyName() {
71+
return "plugin instance";
72+
}
73+
74+
}

0 commit comments

Comments
 (0)