|
| 1 | +/** |
| 2 | + * This file is part of Skript. |
| 3 | + * |
| 4 | + * Skript is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Skript is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Skript. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * |
| 18 | + * Copyright Peter Güttinger, SkriptLang team and contributors |
| 19 | + */ |
| 20 | +package ch.njol.skript.conditions; |
| 21 | + |
| 22 | +import ch.njol.skript.Skript; |
| 23 | +import ch.njol.skript.doc.Description; |
| 24 | +import ch.njol.skript.doc.Examples; |
| 25 | +import ch.njol.skript.doc.Name; |
| 26 | +import ch.njol.skript.doc.Since; |
| 27 | +import ch.njol.skript.lang.Condition; |
| 28 | +import ch.njol.skript.lang.Expression; |
| 29 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 30 | +import ch.njol.util.Kleenean; |
| 31 | +import org.bukkit.Bukkit; |
| 32 | +import org.bukkit.event.Event; |
| 33 | +import org.bukkit.plugin.Plugin; |
| 34 | +import org.eclipse.jdt.annotation.Nullable; |
| 35 | + |
| 36 | +@Name("Is Plugin Enabled") |
| 37 | +@Description({"Check if a plugin is enabled/disabled on the server.", |
| 38 | + "Plugin names can be found in the plugin's 'plugin.yml' file or by using the '/plugins' command, they are NOT the name of the plugin's jar file.", |
| 39 | + "When checking if a plugin is not enabled, this will return true if the plugin is either disabled or not on the server. ", |
| 40 | + "When checking if a plugin is disabled, this will return true if the plugin is on the server and is disabled."}) |
| 41 | +@Examples({"if plugin \"Vault\" is enabled:", |
| 42 | + "if plugin \"WorldGuard\" is not enabled:", |
| 43 | + "if plugins \"Essentials\" and \"Vault\" are enabled:", |
| 44 | + "if plugin \"MyBrokenPlugin\" is disabled:"}) |
| 45 | +@Since("INSERT VERSION") |
| 46 | +public class CondIsPluginEnabled extends Condition { |
| 47 | + |
| 48 | + static { |
| 49 | + Skript.registerCondition(CondIsPluginEnabled.class, |
| 50 | + "plugin[s] %strings% (is|are) enabled", |
| 51 | + "plugin[s] %strings% (is|are)(n't| not) enabled", |
| 52 | + "plugin[s] %strings% (is|are) disabled"); |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("null") |
| 56 | + private Expression<String> plugins; |
| 57 | + private int pattern; |
| 58 | + |
| 59 | + @SuppressWarnings({"unchecked", "null"}) |
| 60 | + @Override |
| 61 | + public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { |
| 62 | + plugins = (Expression<String>) exprs[0]; |
| 63 | + pattern = matchedPattern; |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean check(Event e) { |
| 69 | + return plugins.check(e, plugin -> { |
| 70 | + Plugin p = Bukkit.getPluginManager().getPlugin(plugin); |
| 71 | + switch (pattern) { |
| 72 | + case 1: |
| 73 | + return p == null || !p.isEnabled(); |
| 74 | + case 2: |
| 75 | + return p != null && !p.isEnabled(); |
| 76 | + default: |
| 77 | + return p != null && p.isEnabled(); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String toString(@Nullable Event e, boolean debug) { |
| 84 | + String plugin = plugins.isSingle() ? "plugin " : "plugins "; |
| 85 | + String plural = plugins.isSingle() ? " is" : " are"; |
| 86 | + String pattern = this.pattern == 0 ? " enabled" : this.pattern == 1 ? " not enabled" : " disabled"; |
| 87 | + return plugin + plugins.toString(e, debug) + plural + pattern; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments