Skip to content

Commit

Permalink
Merge branch 'dev/feature' into fix/block-data-clone
Browse files Browse the repository at this point in the history
  • Loading branch information
Pikachu920 authored Jun 30, 2024
2 parents 69d2aed + df620e4 commit 2d3b5f1
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import ch.njol.skript.util.slot.InventorySlot;
import ch.njol.skript.util.slot.Slot;
import com.destroystokyo.paper.event.block.AnvilDamagedEvent;
import com.destroystokyo.paper.event.entity.EndermanAttackPlayerEvent;
import com.destroystokyo.paper.event.entity.ProjectileCollideEvent;
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
import io.papermc.paper.event.entity.EntityMoveEvent;
Expand Down Expand Up @@ -691,6 +692,15 @@ public Entity get(LightningStrikeEvent event) {
return event.getLightning();
}
}, 0);
// EndermanAttackPlayerEvent
if (Skript.classExists("com.destroystokyo.paper.event.entity.EndermanAttackPlayerEvent")) {
EventValues.registerEventValue(EndermanAttackPlayerEvent.class, Player.class, new Getter<Player, EndermanAttackPlayerEvent>() {
@Override
public Player get(EndermanAttackPlayerEvent event) {
return event.getPlayer();
}
}, EventValues.TIME_NOW);
}

// --- PlayerEvents ---
EventValues.registerEventValue(PlayerEvent.class, Player.class, new Getter<Player, PlayerEvent>() {
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/ch/njol/skript/classes/data/DefaultOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ch.njol.skript.util.Date;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Timespan.TimePeriod;
import ch.njol.skript.util.Utils;
import ch.njol.util.Math2;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -85,9 +86,9 @@ public class DefaultOperations {
});

// Timespan - Timespan
Arithmetics.registerOperation(Operator.ADDITION, Timespan.class, (left, right) -> new Timespan(Math2.addClamped(left.getMilliSeconds(), right.getMilliSeconds())));
Arithmetics.registerOperation(Operator.SUBTRACTION, Timespan.class, (left, right) -> new Timespan(Math.max(0, left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerDifference(Timespan.class, (left, right) -> new Timespan(Math.abs(left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerOperation(Operator.ADDITION, Timespan.class, (left, right) -> new Timespan(Math2.addClamped(left.getAs(TimePeriod.MILLISECOND), right.getAs(TimePeriod.MILLISECOND))));
Arithmetics.registerOperation(Operator.SUBTRACTION, Timespan.class, (left, right) -> new Timespan(Math.max(0, left.getAs(TimePeriod.MILLISECOND) - right.getAs(TimePeriod.MILLISECOND))));
Arithmetics.registerDifference(Timespan.class, (left, right) -> new Timespan(Math.abs(left.getAs(TimePeriod.MILLISECOND) - right.getAs(TimePeriod.MILLISECOND))));
Arithmetics.registerDefaultValue(Timespan.class, Timespan::new);

// Timespan - Number
Expand All @@ -96,20 +97,24 @@ public class DefaultOperations {
long scalar = right.longValue();
if (scalar < 0)
return null;
return new Timespan(Math2.multiplyClamped(left.getMilliSeconds(), scalar));
return new Timespan(Math2.multiplyClamped(left.getAs(TimePeriod.MILLISECOND), scalar));
}, (left, right) -> {
long scalar = left.longValue();
if (scalar < 0)
return null;
return new Timespan(scalar * right.getMilliSeconds());
return new Timespan(scalar * right.getAs(TimePeriod.MILLISECOND));
});
Arithmetics.registerOperation(Operator.DIVISION, Timespan.class, Number.class, (left, right) -> {
long scalar = right.longValue();
if (scalar <= 0)
return null;
return new Timespan(left.getMilliSeconds() / scalar);
return new Timespan(left.getAs(TimePeriod.MILLISECOND) / scalar);
});

// Timespan / Timespan = Number
Arithmetics.registerOperation(Operator.DIVISION, Timespan.class, Timespan.class, Number.class,
(left, right) -> left.getAs(TimePeriod.MILLISECOND) / (double) right.getAs(TimePeriod.MILLISECOND));

// Date - Timespan
Arithmetics.registerOperation(Operator.ADDITION, Date.class, Timespan.class, Date::plus);
Arithmetics.registerOperation(Operator.SUBTRACTION, Date.class, Timespan.class, Date::minus);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,22 @@ public class SimpleEvents {
.since("INSERT VERSION")
.requiredPlugins("Spigot 1.19.4+");
}

if (Skript.classExists("com.destroystokyo.paper.event.entity.EndermanAttackPlayerEvent")) {
Skript.registerEvent("Enderman Enrage", SimpleEvent.class, com.destroystokyo.paper.event.entity.EndermanAttackPlayerEvent.class, "enderman (enrage|anger)")
.description(
"Called when an enderman gets mad because a player looked at them.",
"Note: This does not stop enderman from targeting the player as a result of getting damaged."
)
.examples(
"# Stops endermen from getting angry players with the permission \"safeFrom.enderman\"",
"on enderman enrage:",
"\tif player has permission \"safeFrom.enderman\":",
"\t\tcancel event"
)
.since("INSERT VERSION")
.requiredPlugins("Paper");
}
}

}
6 changes: 4 additions & 2 deletions src/main/java/ch/njol/skript/expressions/ExprTernary.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ public boolean isSingle() {
}

@Override
public String toString(Event e, boolean debug) {
return ifTrue.toString(e, debug) + " if " + condition + " otherwise " + ifFalse.toString(e, debug);
public String toString(Event event, boolean debug) {
return ifTrue.toString(event, debug)
+ " if " + condition.toString(event, debug)
+ " otherwise " + ifFalse.toString(event, debug);
}

}
78 changes: 78 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprWhether.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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.expressions;

import ch.njol.skript.Skript;
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.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.UnknownNullability;

@Name("Whether")
@Description("A shorthand for returning the result of a condition (true or false). This is functionally identical to using `true if <condition> else false`.")
@Examples({
"set {fly} to whether player can fly",
"broadcast \"Flying: %whether player is flying%\""
})
@Since("INSERT VERSION")
public class ExprWhether extends SimpleExpression<Boolean> {

static {
Skript.registerExpression(ExprWhether.class, Boolean.class, ExpressionType.PATTERN_MATCHES_EVERYTHING,
"whether <.+>");
}

private @UnknownNullability Condition condition;

@Override
public boolean init(Expression<?>[] expressions, int pattern, Kleenean delayed, ParseResult result) {
String input = result.regexes.get(0).group();
this.condition = Condition.parse(input, "Can't understand this condition: " + input);
return condition != null;
}

@Override
protected Boolean[] get(Event event) {
return new Boolean[] {condition.check(event)};
}

@Override
public Class<? extends Boolean> getReturnType() {
return Boolean.class;
}

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

@Override
public String toString(Event event, boolean debug) {
return "whether " + condition.toString(event, debug);
}

}
1 change: 1 addition & 0 deletions src/main/java/ch/njol/skript/util/Timespan.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Timespan implements YggdrasilSerializable, Comparable<Timespan> { /

public enum TimePeriod {

MILLISECOND(1L),
TICK(50L),
SECOND(1000L),
MINUTE(SECOND.time * 60L),
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ tree types:

# -- Time --
time:
millisecond: millisecond¦s
tick: tick¦s
second: second¦s
minute: minute¦s
Expand Down
8 changes: 7 additions & 1 deletion src/test/skript/tests/syntaxes/expressions/ExprArithmetic.sk
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ test "timespan arithmetic":
assert (2 * {_t1}) is (2 seconds) with "2 * 1 second is not 2 seconds"
assert (2 / {_t1}) is not set with "number divided by timespan is set"

assert ({_t1} + 2) is not set with "timespan plus number is set"
assert ({_t1} + 2) is not set with "timespan plus number is set"

assert {_t1} / {_t2} is 0.5 with "timespan / timespan failed"
assert {_t1} / 1 tick is 20 with "timespan / timespan of different units failed"
assert 0 seconds / {_t2} is 0 with "0 timespan / timespan failed"
assert {_t1} / 0 seconds is infinity value with "timespan / 0 timespan failed"
assert isNaN(0 seconds / 0 ticks) is true with "0 timespan / 0 timespan failed", expected NaN value, got (0 seconds / 0 ticks)

test "date arithmetic":
set {_d1} to now
Expand Down
14 changes: 14 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprWhether.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test "whether":
set {_number} to 5
set {_okay} to whether {_number} is greater than 3
assert {_okay} is true with "Condition didn't evaluate correctly"
set {_okay} to whether {_number} is less than 6
assert {_okay} is true with "Condition didn't evaluate correctly"
set {_okay} to whether {_number} is 5
assert {_okay} is true with "Condition didn't evaluate correctly"
delete {_okay}
spawn a pig at spawn of "world":
set {_pig} to event-entity
assert (whether {_pig} is alive) is true with "Condition didn't evaluate correctly"
assert (whether health of {_pig} is greater than 0) is true with "Condition didn't evaluate correctly"
delete the last spawned pig

0 comments on commit 2d3b5f1

Please sign in to comment.