|
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 |
| - * Copyright Peter Güttinger, SkriptLang team and contributors |
18 |
| - */ |
19 | 1 | package ch.njol.skript.conditions;
|
20 | 2 |
|
21 |
| -import org.bukkit.entity.LivingEntity; |
22 |
| -import org.bukkit.event.Event; |
23 |
| -import org.bukkit.inventory.EntityEquipment; |
24 |
| -import org.bukkit.inventory.ItemStack; |
25 |
| -import org.jetbrains.annotations.Nullable; |
26 |
| - |
27 | 3 | import ch.njol.skript.aliases.ItemType;
|
28 | 4 | import ch.njol.skript.conditions.base.PropertyCondition;
|
29 | 5 | import ch.njol.skript.conditions.base.PropertyCondition.PropertyType;
|
|
34 | 10 | import ch.njol.skript.lang.Condition;
|
35 | 11 | import ch.njol.skript.lang.Expression;
|
36 | 12 | import ch.njol.skript.lang.SkriptParser.ParseResult;
|
| 13 | +import ch.njol.skript.lang.util.SimpleExpression; |
37 | 14 | import ch.njol.util.Kleenean;
|
| 15 | +import org.bukkit.entity.LivingEntity; |
| 16 | +import org.bukkit.event.Event; |
| 17 | +import org.bukkit.inventory.EntityEquipment; |
| 18 | +import org.bukkit.inventory.EquipmentSlot; |
| 19 | +import org.bukkit.inventory.ItemStack; |
| 20 | +import org.jetbrains.annotations.Nullable; |
| 21 | + |
| 22 | +import java.util.Arrays; |
38 | 23 |
|
39 |
| -/** |
40 |
| - * @author Peter Güttinger |
41 |
| - */ |
42 | 24 | @Name("Is Wearing")
|
43 |
| -@Description("Checks whether a player is wearing some armour.") |
44 |
| -@Examples({"player is wearing an iron chestplate and iron leggings", |
45 |
| - "player is wearing all diamond armour"}) |
| 25 | +@Description("Checks whether an entity is wearing some items (usually armor).") |
| 26 | +@Examples({ |
| 27 | + "player is wearing an iron chestplate and iron leggings", |
| 28 | + "player is wearing all diamond armour", |
| 29 | + "target is wearing wolf armor" |
| 30 | +}) |
46 | 31 | @Since("1.0")
|
47 | 32 | public class CondIsWearing extends Condition {
|
48 | 33 |
|
49 | 34 | static {
|
50 | 35 | PropertyCondition.register(CondIsWearing.class, "wearing %itemtypes%", "livingentities");
|
51 | 36 | }
|
52 | 37 |
|
53 |
| - @SuppressWarnings("null") |
| 38 | + @SuppressWarnings("NotNullFieldNotInitialized") |
54 | 39 | private Expression<LivingEntity> entities;
|
55 |
| - @SuppressWarnings("null") |
| 40 | + @SuppressWarnings("NotNullFieldNotInitialized") |
56 | 41 | private Expression<ItemType> types;
|
57 | 42 |
|
58 | 43 | @SuppressWarnings({"unchecked", "null"})
|
59 | 44 | @Override
|
60 |
| - public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { |
| 45 | + public boolean init(Expression<?>[] vars, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
61 | 46 | entities = (Expression<LivingEntity>) vars[0];
|
62 | 47 | types = (Expression<ItemType>) vars[1];
|
63 | 48 | setNegated(matchedPattern == 1);
|
64 | 49 | return true;
|
65 | 50 | }
|
66 | 51 |
|
67 | 52 | @Override
|
68 |
| - public boolean check(final Event e) { |
69 |
| - return entities.check(e, |
70 |
| - en -> types.check(e, |
71 |
| - t -> { |
72 |
| - EntityEquipment equip = en.getEquipment(); |
73 |
| - if (equip == null) |
74 |
| - return false; // No equipment -> not wearing anything |
75 |
| - for (final ItemStack is : equip.getArmorContents()) { |
76 |
| - if (t.isOfType(is) ^ t.isAll()) |
77 |
| - return !t.isAll(); |
78 |
| - } |
79 |
| - return t.isAll(); |
80 |
| - }), |
81 |
| - isNegated()); |
| 53 | + public boolean check(Event event) { |
| 54 | + ItemType[] cachedTypes = types.getAll(event); |
| 55 | + |
| 56 | + return entities.check(event, entity -> { |
| 57 | + EntityEquipment equipment = entity.getEquipment(); |
| 58 | + if (equipment == null) |
| 59 | + return false; // spigot nullability, no identifier as to why this occurs |
| 60 | + |
| 61 | + ItemStack[] contents = Arrays.stream(EquipmentSlot.values()) |
| 62 | + .map(equipment::getItem) |
| 63 | + .toArray(ItemStack[]::new); |
| 64 | + |
| 65 | + return SimpleExpression.check(cachedTypes, type -> { |
| 66 | + for (ItemStack content : contents) { |
| 67 | + if (type.isOfType(content) ^ type.isAll()) |
| 68 | + return !type.isAll(); |
| 69 | + } |
| 70 | + return type.isAll(); |
| 71 | + }, false, false); |
| 72 | + }, isNegated()); |
82 | 73 | }
|
83 |
| - |
| 74 | + |
84 | 75 | @Override
|
85 |
| - public String toString(final @Nullable Event e, final boolean debug) { |
86 |
| - return PropertyCondition.toString(this, PropertyType.BE, e, debug, entities, |
87 |
| - "wearing " + types.toString(e, debug)); |
| 76 | + public String toString(@Nullable Event event, boolean debug) { |
| 77 | + return PropertyCondition.toString(this, PropertyType.BE, event, debug, entities, |
| 78 | + "wearing " + types.toString(event, debug)); |
88 | 79 | }
|
89 | 80 |
|
90 | 81 | }
|
0 commit comments