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

ExprArmorSlot - Body Armor #7061

Open
wants to merge 20 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
18 changes: 15 additions & 3 deletions src/main/java/ch/njol/skript/expressions/ExprArmorSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import ch.njol.skript.util.slot.EquipmentSlot.EquipSlot;
import ch.njol.skript.util.slot.Slot;
import ch.njol.util.Kleenean;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Wolf;
import org.bukkit.event.Event;
import org.bukkit.inventory.EntityEquipment;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -50,19 +52,22 @@
public class ExprArmorSlot extends PropertyExpression<LivingEntity, Slot> {

static {
register(ExprArmorSlot.class, Slot.class, "((boots:(boots|shoes)|leggings:leg[ging]s|chestplate:chestplate[s]|helmet:helmet[s]) [(item|:slot)]|armour:armo[u]r[s])", "livingentities");
register(ExprArmorSlot.class, Slot.class, "((boots:(boots|shoes)|leggings:leg[ging]s|chestplate:chestplate[s]|helmet:helmet[s]) [(item|:slot)]|armour:armo[u]r[s]|bodyarmor:body armo[u]r)", "livingentities");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Nullable
private EquipSlot slot;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
private boolean explicitSlot;
private boolean isArmor;

private boolean isBody;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
isBody = parseResult.hasTag("bodyarmor");
isArmor = parseResult.hasTag("armour");
slot = isArmor ? null : EquipSlot.valueOf(parseResult.tags.get(0).toUpperCase(Locale.ENGLISH));
slot = (isArmor || isBody) ? null : EquipSlot.valueOf(parseResult.tags.get(0).toUpperCase(Locale.ENGLISH));
explicitSlot = parseResult.hasTag("slot"); // User explicitly asked for SLOT, not item
setExpr((Expression<? extends LivingEntity>) exprs[0]);
return true;
Expand All @@ -76,6 +81,13 @@ protected Slot[] get(Event event, LivingEntity[] source) {
.flatMap(equipment -> {
if (equipment == null)
return null;
if (isBody) {
if (!(equipment.getHolder() instanceof Wolf) && !(equipment.getHolder() instanceof AbstractHorse))
return null;
return Stream.of(
new EquipmentSlot(equipment, EquipSlot.BODY, explicitSlot)
);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
return Stream.of(
new EquipmentSlot(equipment, EquipSlot.HELMET, explicitSlot),
new EquipmentSlot(equipment, EquipSlot.CHESTPLATE, explicitSlot),
Expand All @@ -96,7 +108,7 @@ protected Slot[] get(Event event, LivingEntity[] source) {

@Override
public boolean isSingle() {
return !isArmor && super.isSingle();
return isBody || (!isArmor && super.isSingle());
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ public ItemStack get(final EntityEquipment e) {
public void set(final EntityEquipment e, final @Nullable ItemStack item) {
e.setBoots(item);
}
},

BODY() {
@Override
@Nullable
public ItemStack get(final EntityEquipment e) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return e.getItem(org.bukkit.inventory.EquipmentSlot.BODY);
}

@Override
public void set(final EntityEquipment e, final @Nullable ItemStack item) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
e.setItem(org.bukkit.inventory.EquipmentSlot.BODY, item);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
};

public final int slotNumber;
Expand Down
10 changes: 10 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprArmorSlot.sk
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ test "armour slot":
clear armour of event-entity
assert armour of event-entity does not contain dirt block, diamond chestplate, iron leggings and gold boots with "Failed to clear EquipmentSlots"
delete event-entity

test "body armor wolf" when running minecraft "1.20.5":
spawn wolf at spawn of world "world":
set {_entity} to event-entity
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

set body armor of {_entity} to wolf armor
assert body armor of {_entity} is wolf armor with "Body armor of entity is not wolf armor"
clear body armor of {_entity}
assert body armor of {_entity} is air with "Body armor of entity did not get cleared"
clear entity within {_entity}