Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates EffEquip, CondIsWearing, and adds CondIsSaddled #6849

Merged
merged 24 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
72e1898
Add wolf and update horse support
cheeezburga Jul 2, 2024
03a0cdd
Fixed star imports
cheeezburga Jul 2, 2024
bb25458
Merge branch 'dev/feature' into update-eff-equip
cheeezburga Aug 30, 2024
d14fdcb
Some more updates to EffEquip and it's test
cheeezburga Aug 30, 2024
430a0ab
Merge branch 'dev/feature' into update-eff-equip
cheeezburga Sep 2, 2024
e50ec03
Adds saddle condition and changes some small EffEquip stuff
cheeezburga Sep 2, 2024
94af02a
Adds saddle test and changes CondIsSaddled to avoid potential conflict
cheeezburga Sep 3, 2024
9e42efe
Just some tiny changes to CondIsWearing
cheeezburga Sep 3, 2024
d59ea69
Updates EffEquip and its test
cheeezburga Sep 3, 2024
b601a72
Merge branch 'dev/feature' into update-eff-equip
cheeezburga Sep 3, 2024
f90975e
Fixes wolf armor test failing
cheeezburga Sep 3, 2024
0ebb2ff
Merge remote-tracking branch 'origin/update-eff-equip' into update-ef…
cheeezburga Sep 3, 2024
880556d
Apply suggestions from code review
cheeezburga Sep 3, 2024
04a8d4e
Suggestions and stuff
cheeezburga Sep 3, 2024
90094ef
Suggestions
cheeezburga Sep 5, 2024
2c2c284
Suggestions (Nullable annotation)
cheeezburga Sep 6, 2024
f8ab358
Fixes entity equipment being checked per type instead of per entity
cheeezburga Sep 6, 2024
76c5d9c
Suggestion
cheeezburga Sep 14, 2024
8b772c2
Merge branch 'dev/feature' into update-eff-equip
cheeezburga Sep 14, 2024
33719f0
Small change to description
cheeezburga Sep 14, 2024
0827751
Merge branch 'dev/feature' into update-eff-equip
cheeezburga Oct 2, 2024
83a0c6c
Merge branch 'dev/feature' into update-eff-equip
cheeezburga Oct 5, 2024
5058a00
Merge branch 'dev/feature' into update-eff-equip
sovdeeth Oct 13, 2024
fc6f4e0
Merge branch 'dev/feature' into update-eff-equip
sovdeeth Dec 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsSaddled.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ch.njol.skript.conditions;

import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.Material;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Steerable;
import org.bukkit.inventory.ItemStack;

@Name("Is Saddled")
@Description({
"Checks whether a given entity (horse or steerable) is saddled.",
"If 'properly' is used, this will only return true if the entity is wearing specifically a saddle item."
})
@Examples("send whether {_horse} is saddled")
@Since("INSERT VERSION")
public class CondIsSaddled extends PropertyCondition<LivingEntity> {

static {
register(CondIsSaddled.class, "[:properly] saddled", "livingentities");
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean properly;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
properly = parseResult.hasTag("properly");
return super.init(exprs, matchedPattern, isDelayed, parseResult);
}

@Override
public boolean check(LivingEntity entity) {
if (entity instanceof Steerable steerable) {
return steerable.hasSaddle();
} else if (entity instanceof AbstractHorse horse) {
ItemStack saddle = horse.getInventory().getSaddle();
return properly ? (saddle != null && saddle.equals(new ItemStack(Material.SADDLE))) : (saddle != null);
}
return false;
}

@Override
protected String getPropertyName() {
return properly ? "properly saddled" : "saddled";
}

}
93 changes: 42 additions & 51 deletions src/main/java/ch/njol/skript/conditions/CondIsWearing.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.conditions;

import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType;
Expand All @@ -34,57 +10,72 @@
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;

/**
* @author Peter Güttinger
*/
@Name("Is Wearing")
@Description("Checks whether a player is wearing some armour.")
@Examples({"player is wearing an iron chestplate and iron leggings",
"player is wearing all diamond armour"})
@Description("Checks whether an entity is wearing some items (usually armor).")
@Examples({
"player is wearing an iron chestplate and iron leggings",
"player is wearing all diamond armour",
"target is wearing wolf armor"
})
@Since("1.0")
public class CondIsWearing extends Condition {

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

@SuppressWarnings("null")
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<LivingEntity> entities;
@SuppressWarnings("null")
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<ItemType> types;

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
public boolean init(Expression<?>[] vars, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
entities = (Expression<LivingEntity>) vars[0];
types = (Expression<ItemType>) vars[1];
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(final Event e) {
return entities.check(e,
en -> types.check(e,
t -> {
EntityEquipment equip = en.getEquipment();
if (equip == null)
return false; // No equipment -> not wearing anything
for (final ItemStack is : equip.getArmorContents()) {
if (t.isOfType(is) ^ t.isAll())
return !t.isAll();
}
return t.isAll();
}),
isNegated());
public boolean check(Event event) {
ItemType[] cachedTypes = types.getAll(event);

return entities.check(event, entity -> {
EntityEquipment equipment = entity.getEquipment();
if (equipment == null)
return false; // spigot nullability, no identifier as to why this occurs

ItemStack[] contents = Arrays.stream(EquipmentSlot.values())
.map(equipment::getItem)
.toArray(ItemStack[]::new);

return SimpleExpression.check(cachedTypes, type -> {
for (ItemStack content : contents) {
if (type.isOfType(content) ^ type.isAll())
return !type.isAll();
}
return type.isAll();
}, false, false);
}, isNegated());
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return PropertyCondition.toString(this, PropertyType.BE, e, debug, entities,
"wearing " + types.toString(e, debug));
public String toString(@Nullable Event event, boolean debug) {
return PropertyCondition.toString(this, PropertyType.BE, event, debug, entities,
"wearing " + types.toString(event, debug));
}

}
Loading
Loading