|
| 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