Skip to content

Commit

Permalink
Merge branch 'dev/patch' into patch/date-serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus authored Feb 1, 2025
2 parents 96f6a85 + 382f883 commit 1af0677
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
21 changes: 20 additions & 1 deletion src/main/java/ch/njol/skript/conditions/CondIsWearing.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.njol.skript.conditions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType;
Expand All @@ -12,7 +13,7 @@
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.entity.*;
import org.bukkit.event.Event;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
Expand All @@ -30,6 +31,9 @@
})
@Since("1.0")
public class CondIsWearing extends Condition {

private static final boolean HAS_CAN_USE_SLOT_METHOD = Skript.methodExists(LivingEntity.class, "canUseEquipmentSlot", EquipmentSlot.class);
private static final boolean HAS_BODY_SLOT = Skript.fieldExists(EquipmentSlot.class, "BODY");

static {
PropertyCondition.register(CondIsWearing.class, "wearing %itemtypes%", "livingentities");
Expand Down Expand Up @@ -59,6 +63,21 @@ public boolean check(Event event) {
return false; // spigot nullability, no identifier as to why this occurs

ItemStack[] contents = Arrays.stream(EquipmentSlot.values())
.filter(slot -> {
// this method was added in 1.20.6
if (HAS_CAN_USE_SLOT_METHOD)
return entity.canUseEquipmentSlot(slot);

// body slot was added in 1.20.5
if (HAS_BODY_SLOT && slot == EquipmentSlot.BODY)
// this may change in the future, but for now this is the only way to figure out
// if the entity can use the body slot
return entity instanceof Horse
|| entity instanceof Wolf
|| entity instanceof Llama;

return true;
})
.map(equipment::getItem)
.toArray(ItemStack[]::new);

Expand Down
21 changes: 5 additions & 16 deletions src/main/java/ch/njol/skript/expressions/ExprNewBannerPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,23 @@
"remove {_pattern} from banner patterns of {_banneritem}",
"set the 1st banner pattern of block at location(0,0,0) to {_pattern}",
"clear the 1st banner pattern of block at location(0,0,0)",
"",
"set {_pattern} to a red mojang banner pattern"
})
@Since("2.10")
public class ExprNewBannerPattern extends SimpleExpression<Pattern> {

static {
Skript.registerExpression(ExprNewBannerPattern.class, Pattern.class, ExpressionType.PATTERN_MATCHES_EVERYTHING,
"[a] %bannerpatterntype% colo[u]red %color%",
"[a] %*color% %bannerpatterntype%");
Skript.registerExpression(ExprNewBannerPattern.class, Pattern.class, ExpressionType.COMBINED,
"[a] %bannerpatterntype% colo[u]red %color%");
}

private Expression<PatternType> selectedPattern;
private Expression<Color> selectedColor;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (matchedPattern == 0) {
//noinspection unchecked
selectedPattern = (Expression<PatternType>) exprs[0];
//noinspection unchecked
selectedColor = (Expression<Color>) exprs[1];
} else {
//noinspection unchecked
selectedPattern = (Expression<PatternType>) exprs[1];
//noinspection unchecked
selectedColor = (Expression<Color>) exprs[0];
}
selectedPattern = (Expression<PatternType>) exprs[0];
selectedColor = (Expression<Color>) exprs[1];
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ public boolean check(Event event) {
return elements.check(event, element -> {
boolean isAny = (element instanceof ItemType itemType && !itemType.isAll());
Keyed[] values = TagModule.getKeyed(element);
if (values == null)
if (values == null || values.length == 0)
return false;

Class<? extends Keyed> valueClass = values[0].getClass();

for (Tag<Keyed> tag : tags) {
// cursed check to ensure the tag is the same type as the values
if (!tag.getValues().iterator().next().getClass().isAssignableFrom(valueClass))
return false;
if (isTagged(tag, values, !isAny)) {
if (!and)
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.skriptlang.skript.test.tests.regression;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.util.ContextlessEvent;
import ch.njol.skript.test.runner.SkriptJUnitTest;
import ch.njol.skript.variables.Variables;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class MissingCheckIfEntityCanUseSlot7524Test extends SkriptJUnitTest {

private static final boolean HAS_CAN_USE_SLOT_METHOD = Skript.methodExists(LivingEntity.class, "canUseEquipmentSlot", EquipmentSlot.class);

private Player player;
private EntityEquipment equipment;
private Condition isWearingCondition;

@Before
public void setup() {
player = EasyMock.niceMock(Player.class);
equipment = EasyMock.niceMock(EntityEquipment.class);

isWearingCondition = Condition.parse("{_player} is wearing diamond chestplate", null);
if (isWearingCondition == null)
throw new IllegalStateException();
}

@Test
public void test() {
ContextlessEvent event = ContextlessEvent.get();
Variables.setVariable("player", player, event, true);

EasyMock.expect(player.isValid()).andStubReturn(true);
EasyMock.expect(player.getEquipment()).andReturn(equipment);

if (HAS_CAN_USE_SLOT_METHOD) {
EasyMock.expect(player.canUseEquipmentSlot(EquipmentSlot.CHEST)).andReturn(true);
EasyMock.expect(player.canUseEquipmentSlot(EquipmentSlot.LEGS)).andReturn(true);
EasyMock.expect(player.canUseEquipmentSlot(EquipmentSlot.FEET)).andReturn(true);
EasyMock.expect(player.canUseEquipmentSlot(EquipmentSlot.HEAD)).andReturn(true);
EasyMock.expect(player.canUseEquipmentSlot(EquipmentSlot.HAND)).andReturn(true);
EasyMock.expect(player.canUseEquipmentSlot(EquipmentSlot.OFF_HAND)).andReturn(true);
EasyMock.expect(player.canUseEquipmentSlot(EquipmentSlot.BODY)).andReturn(false);
}

EasyMock.expect(equipment.getItem(EquipmentSlot.CHEST)).andReturn(new ItemStack(Material.DIAMOND_CHESTPLATE));

EasyMock.replay(player, equipment);

assert isWearingCondition.check(event);

EasyMock.verify(player, equipment);
}

}
3 changes: 3 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondIsTagged.sk
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ test "CondIsTagged":

spawn a skeleton at spawn of world "world":
assert entity is tagged as entity tag "minecraft:skeletons" with "skeleton is not a skeleton"
assert entity is not tagged as item tag "doors" with "failed to handle checking entity for item tag"
delete entity

assert stone sword is not tagged as entity tag "skeletons" with "failed to handle checking item for entity tag"

0 comments on commit 1af0677

Please sign in to comment.