Skip to content

Commit

Permalink
Merge branch 'dev/patch' into fish-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus authored Feb 1, 2025
2 parents 90c98c4 + b4db4b1 commit dd26c72
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 25 deletions.
12 changes: 8 additions & 4 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,19 @@ public String toVariableNameString(final Timeperiod o) {
@Override
public Fields serialize(Date date) {
Fields fields = new Fields();
fields.putPrimitive("time", date.getTime());
fields.putPrimitive("timestamp", date.getTime());
return fields;
}


@Override
protected Date deserialize(Fields fields)
throws StreamCorruptedException {
long time = fields.getPrimitive("time", long.class);
protected Date deserialize(Fields fields) throws StreamCorruptedException {
long time;
if (fields.hasField("time")) { // compatibility for 2.10.0 (#7542)
time = fields.getPrimitive("time", long.class);
} else {
time = fields.getPrimitive("timestamp", long.class);
}
return new Date(time);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ private void refresh() {
String namespace = namespacedKey.getNamespace();
String key = namespacedKey.getKey();
String keyWithSpaces = key.replace("_", " ");
String languageKey = languageNode + "." + key;
String languageKey;

// Put the full namespaced key as a pattern
parseMap.put(namespacedKey.toString(), registryObject);

// If the object is a vanilla Minecraft object, we'll add the key with spaces as a pattern
if (namespace.equalsIgnoreCase(NamespacedKey.MINECRAFT)) {
parseMap.put(keyWithSpaces, registryObject);
languageKey = languageNode + "." + key;
} else {
languageKey = namespacedKey.toString();
}

String[] options = Language.getList(languageKey);
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class ExprExperienceCooldown extends SimplePropertyExpression<Player, Timespan> {

static {
register(ExprExperienceCooldown.class, Timespan.class, "[the] (experience|[e]xp) [pickup|collection] cooldown", "players");
register(ExprExperienceCooldown.class, Timespan.class, "(experience|[e]xp) [pickup|collection] cooldown", "players");
}

private static final int maxTicks = Integer.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class ExprExperienceCooldownChangeReason extends EventValueExpression<ChangeReason> {

static {
register(ExprExperienceCooldownChangeReason.class, ChangeReason.class, "[the] (experience|[e]xp) cooldown change (reason|cause|type)");
register(ExprExperienceCooldownChangeReason.class, ChangeReason.class, "(experience|[e]xp) cooldown change (reason|cause|type)");
}

public ExprExperienceCooldownChangeReason() {
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
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/test/platform/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ public TestResults runTests(Path runnerRoot, Path testsRoot, boolean devMode, bo
args.add("-Ddisable.watchdog=true");
if (debug)
args.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000");
args.add("-Duser.language=en");
args.add("-Duser.country=US");
args.addAll(jvmArgs);
args.addAll(Arrays.asList(commandLine));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.util.SimpleEvent;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.EventValues;
import ch.njol.yggdrasil.Fields;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
Expand All @@ -18,6 +20,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.StreamCorruptedException;

public class LootTableModule {

Expand Down Expand Up @@ -53,6 +56,42 @@ public String toVariableNameString(LootTable o) {
return "loot table:" + o.getKey();
}
})
.serializer(new Serializer<>() {
@Override
public Fields serialize(LootTable lootTable) {
Fields fields = new Fields();
fields.putObject("key", lootTable.getKey().toString());
return fields;
}

@Override
public void deserialize(LootTable lootTable, Fields fields) {
assert false;
}

@Override
protected LootTable deserialize(Fields fields) throws StreamCorruptedException {
String key = fields.getAndRemoveObject("key", String.class);
if (key == null)
throw new StreamCorruptedException();

NamespacedKey namespacedKey = NamespacedKey.fromString(key);
if (namespacedKey == null)
throw new StreamCorruptedException();

return Bukkit.getLootTable(namespacedKey);
}

@Override
public boolean mustSyncDeserialization() {
return true;
}

@Override
protected boolean canBeInstantiated() {
return false;
}
})
);

Classes.registerClass(new ClassInfo<>(LootContext.class, "lootcontext")
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 dd26c72

Please sign in to comment.