Skip to content

Commit 342d7e9

Browse files
cheeezburgaModerockyFusezionsovdeeth
authored
Updates EffEquip, CondIsWearing, and adds CondIsSaddled (#6849)
* Add wolf and update horse support * Fixed star imports * Some more updates to EffEquip and it's test * Adds saddle condition and changes some small EffEquip stuff * Adds saddle test and changes CondIsSaddled to avoid potential conflict - Renames CondHasSaddle to CondIsSaddled * Just some tiny changes to CondIsWearing * Updates EffEquip and its test - The test now uses CondIsSaddled, and tries to test wolf armor as well - Removed the Steerable check in EffEquip, as 1.19.4+ (2.10) will always have it * Fixes wolf armor test failing - Due to CondIsWearing not accounting for the wolf armor in the BODY slot, which apparently isn't returned by EntityEquipment#getArmorContents() - Moves wolf test to separate structure * Apply suggestions from code review Co-authored-by: Moderocky <admin@moderocky.com> Co-authored-by: Fusezion <fusezionstream@gmail.com> * Suggestions and stuff * Suggestions - Adds horse armour tests - Moves annotations in-line - Only supports 'properly' in CondIsSaddled * Suggestions (Nullable annotation) Co-authored-by: Moderocky <admin@moderocky.com> * Fixes entity equipment being checked per type instead of per entity * Suggestion * Small change to description --------- Co-authored-by: Moderocky <admin@moderocky.com> Co-authored-by: Fusezion <fusezionstream@gmail.com> Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
1 parent a73d31e commit 342d7e9

File tree

5 files changed

+261
-145
lines changed

5 files changed

+261
-145
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ch.njol.skript.conditions;
2+
3+
import ch.njol.skript.conditions.base.PropertyCondition;
4+
import ch.njol.skript.doc.Description;
5+
import ch.njol.skript.doc.Examples;
6+
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.Since;
8+
import ch.njol.skript.lang.Expression;
9+
import ch.njol.skript.lang.SkriptParser.ParseResult;
10+
import ch.njol.util.Kleenean;
11+
import org.bukkit.Material;
12+
import org.bukkit.entity.AbstractHorse;
13+
import org.bukkit.entity.LivingEntity;
14+
import org.bukkit.entity.Steerable;
15+
import org.bukkit.inventory.ItemStack;
16+
17+
@Name("Is Saddled")
18+
@Description({
19+
"Checks whether a given entity (horse or steerable) is saddled.",
20+
"If 'properly' is used, this will only return true if the entity is wearing specifically a saddle item."
21+
})
22+
@Examples("send whether {_horse} is saddled")
23+
@Since("INSERT VERSION")
24+
public class CondIsSaddled extends PropertyCondition<LivingEntity> {
25+
26+
static {
27+
register(CondIsSaddled.class, "[:properly] saddled", "livingentities");
28+
}
29+
30+
private boolean properly;
31+
32+
@Override
33+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
34+
properly = parseResult.hasTag("properly");
35+
return super.init(exprs, matchedPattern, isDelayed, parseResult);
36+
}
37+
38+
@Override
39+
public boolean check(LivingEntity entity) {
40+
if (entity instanceof Steerable steerable) {
41+
return steerable.hasSaddle();
42+
} else if (entity instanceof AbstractHorse horse) {
43+
ItemStack saddle = horse.getInventory().getSaddle();
44+
return properly ? (saddle != null && saddle.equals(new ItemStack(Material.SADDLE))) : (saddle != null);
45+
}
46+
return false;
47+
}
48+
49+
@Override
50+
protected String getPropertyName() {
51+
return properly ? "properly saddled" : "saddled";
52+
}
53+
54+
}
Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
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-
*/
191
package ch.njol.skript.conditions;
202

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-
273
import ch.njol.skript.aliases.ItemType;
284
import ch.njol.skript.conditions.base.PropertyCondition;
295
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType;
@@ -34,57 +10,72 @@
3410
import ch.njol.skript.lang.Condition;
3511
import ch.njol.skript.lang.Expression;
3612
import ch.njol.skript.lang.SkriptParser.ParseResult;
13+
import ch.njol.skript.lang.util.SimpleExpression;
3714
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;
3823

39-
/**
40-
* @author Peter Güttinger
41-
*/
4224
@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+
})
4631
@Since("1.0")
4732
public class CondIsWearing extends Condition {
4833

4934
static {
5035
PropertyCondition.register(CondIsWearing.class, "wearing %itemtypes%", "livingentities");
5136
}
5237

53-
@SuppressWarnings("null")
38+
@SuppressWarnings("NotNullFieldNotInitialized")
5439
private Expression<LivingEntity> entities;
55-
@SuppressWarnings("null")
40+
@SuppressWarnings("NotNullFieldNotInitialized")
5641
private Expression<ItemType> types;
5742

5843
@SuppressWarnings({"unchecked", "null"})
5944
@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) {
6146
entities = (Expression<LivingEntity>) vars[0];
6247
types = (Expression<ItemType>) vars[1];
6348
setNegated(matchedPattern == 1);
6449
return true;
6550
}
6651

6752
@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());
8273
}
83-
74+
8475
@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));
8879
}
8980

9081
}

0 commit comments

Comments
 (0)