From 5042f2e9201abc0907f1798d6878b27fb32d637a Mon Sep 17 00:00:00 2001 From: APickledWalrus Date: Sun, 5 Apr 2020 01:21:10 -0400 Subject: [PATCH 1/4] Add expressions and test --- .../skript/expressions/ExprLastDeath.java | 137 ++++++++++++++++++ .../skript/expressions/ExprTimeSince.java | 94 ++++++++++++ .../syntaxes/expressions/ExprTimeSince.sk | 10 ++ 3 files changed, 241 insertions(+) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprLastDeath.java create mode 100644 src/main/java/ch/njol/skript/expressions/ExprTimeSince.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprLastDeath.java b/src/main/java/ch/njol/skript/expressions/ExprLastDeath.java new file mode 100644 index 00000000000..9e161563941 --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprLastDeath.java @@ -0,0 +1,137 @@ +/** + * 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 . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.expressions; + +import org.bukkit.Statistic; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +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.expressions.base.SimplePropertyExpression; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.util.Date; +import ch.njol.skript.util.Timespan; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; + +@Name("Last Death") +@Description({"The time of the last death of a player.", + "A change to this value can't be negative, so it will be reset or remain unchanged."}) +@Examples("send \"Your last death was %last death of player%!\" to player") +@Since("INSERT VERSION") +public class ExprLastDeath extends SimplePropertyExpression { + + private final Statistic LAST_DEATH = Statistic.TIME_SINCE_DEATH; + + static { + Skript.registerExpression(ExprLastDeath.class, Date.class, ExpressionType.PROPERTY, + "[the] [time of [the]] last death of %players%", + "[the] [time of [the]] %players%'[s] last death" + ); + register(ExprLastDeath.class, Date.class, "[the] last death", "players"); + } + + @SuppressWarnings({"null", "unchecked"}) + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + setExpr((Expression) exprs[0]); + return true; + } + + @Nullable + @Override + public Date convert(Player player) { + Date date = new Date(); + date.subtract(Timespan.fromTicks_i(player.getStatistic(LAST_DEATH))); + return date; + } + + @Nullable + @Override + public Class[] acceptChange(ChangeMode mode) { + if (mode == ChangeMode.DELETE || mode == ChangeMode.REMOVE_ALL) + return null; + if (mode == ChangeMode.SET) + return CollectionUtils.array(Date.class); + return CollectionUtils.array(Timespan.class); + } + + @SuppressWarnings("null") + @Override + public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { + if (delta == null && mode != ChangeMode.RESET) + return; + Date now = new Date(); + for (Player player : getExpr().getArray(e)) { + if (mode == ChangeMode.ADD) { + + Date deathTime = convert(player); + long add = ((Timespan) delta[0]).getTicks_i(); + deathTime.add(Timespan.fromTicks_i(add)); + player.setStatistic(LAST_DEATH, (int) deathTime.difference(now).getTicks_i()); + + } else if (mode == ChangeMode.REMOVE) { + + Date deathTime = convert(player); + long remove = ((Timespan) delta[0]).getTicks_i(); + deathTime.subtract(Timespan.fromTicks_i(remove)); + player.setStatistic(LAST_DEATH, (int) deathTime.difference(now).getTicks_i()); + + } else if (mode == ChangeMode.SET) { + + /* + * Since the statistic is actually the time since the last death of the player, + * it needs to be set to the difference between now and the new date. + * For example, if we were setting the player's last death to 1 day ago, + * the statistic would need to be 24 hours, and that's what this would give us. + * + * If the new date is in the future, the statistic will remain unchanged. + */ + Date newDate = ((Date) delta[0]); + if (newDate.compareTo(now) < 1) + player.setStatistic(LAST_DEATH, (int) ((Date) delta[0]).difference(now).getTicks_i()); + + } else if (mode == ChangeMode.RESET) { + + player.setStatistic(LAST_DEATH, 0); + + } + } + } + + @Override + public Class getReturnType() { + return Date.class; + } + + @Override + protected String getPropertyName() { + return "last death"; + } + +} diff --git a/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java new file mode 100644 index 00000000000..a30fba70915 --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java @@ -0,0 +1,94 @@ +/** + * 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 . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.expressions; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Statistic; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +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.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.util.Date; +import ch.njol.skript.util.Timespan; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; + +@Name("Time Since") +@Description("The time that has passed since a date. This will return 0 seconds if the given date is in the future.") +@Examples("send \"You died %time since last death of player% ago!\" to player") +@Since("INSERT VERSION") +public class ExprTimeSince extends SimpleExpression { + + static { + Skript.registerExpression(ExprTimeSince.class, Timespan.class, ExpressionType.SIMPLE, "time since %dates%"); + } + + @SuppressWarnings("null") + private Expression dates; + + @SuppressWarnings({"unchecked", "null"}) + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + dates = (Expression) exprs[0]; + return true; + } + + @Override + @Nullable + protected Timespan[] get(Event e) { + List timespans = new ArrayList<>(); + Date now = new Date(); + for (Date date : dates.getArray(e)) + if (date.compareTo(now) > 0) { + timespans.add(new Timespan()); + } else { + timespans.add(date.difference(now)); + } + return timespans.toArray(new Timespan[0]); + } + + @Override + public boolean isSingle() { + return dates.isSingle(); + } + + @Override + public Class getReturnType() { + return Timespan.class; + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + return "time since " + dates.toString(e, debug); + } + +} diff --git a/src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk b/src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk new file mode 100644 index 00000000000..ed3cffa40e4 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk @@ -0,0 +1,10 @@ +test "time since": + + set {_time} to time since 1 hour ago + assert {_time} is 1 hour with "time since 1 hour ago failed | value: %{_time}%" + + set {_time} to time since 1 day ago + assert {_time} is 1 day with "time since 1 day ago failed | value: %{_time}%" + + set {_time} to time since 1 day later + assert {_time} is 0 seconds with "time since 1 day later failed | value: %{_time}%" From 6ba7107a3db2e0979508cdf573b99dd3643c9712 Mon Sep 17 00:00:00 2001 From: APickledWalrus Date: Fri, 17 Apr 2020 13:59:23 -0400 Subject: [PATCH 2/4] Fixes & Changes --- .../skript/expressions/ExprLastDeath.java | 137 ------------- .../skript/expressions/ExprLastDeathTime.java | 181 ++++++++++++++++++ .../skript/expressions/ExprTimeSince.java | 50 ++--- 3 files changed, 201 insertions(+), 167 deletions(-) delete mode 100644 src/main/java/ch/njol/skript/expressions/ExprLastDeath.java create mode 100644 src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java diff --git a/src/main/java/ch/njol/skript/expressions/ExprLastDeath.java b/src/main/java/ch/njol/skript/expressions/ExprLastDeath.java deleted file mode 100644 index 9e161563941..00000000000 --- a/src/main/java/ch/njol/skript/expressions/ExprLastDeath.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * 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 . - * - * - * Copyright 2011-2017 Peter Güttinger and contributors - */ -package ch.njol.skript.expressions; - -import org.bukkit.Statistic; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.eclipse.jdt.annotation.Nullable; - -import ch.njol.skript.Skript; -import ch.njol.skript.classes.Changer.ChangeMode; -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.expressions.base.SimplePropertyExpression; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.skript.util.Date; -import ch.njol.skript.util.Timespan; -import ch.njol.util.Kleenean; -import ch.njol.util.coll.CollectionUtils; - -@Name("Last Death") -@Description({"The time of the last death of a player.", - "A change to this value can't be negative, so it will be reset or remain unchanged."}) -@Examples("send \"Your last death was %last death of player%!\" to player") -@Since("INSERT VERSION") -public class ExprLastDeath extends SimplePropertyExpression { - - private final Statistic LAST_DEATH = Statistic.TIME_SINCE_DEATH; - - static { - Skript.registerExpression(ExprLastDeath.class, Date.class, ExpressionType.PROPERTY, - "[the] [time of [the]] last death of %players%", - "[the] [time of [the]] %players%'[s] last death" - ); - register(ExprLastDeath.class, Date.class, "[the] last death", "players"); - } - - @SuppressWarnings({"null", "unchecked"}) - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - setExpr((Expression) exprs[0]); - return true; - } - - @Nullable - @Override - public Date convert(Player player) { - Date date = new Date(); - date.subtract(Timespan.fromTicks_i(player.getStatistic(LAST_DEATH))); - return date; - } - - @Nullable - @Override - public Class[] acceptChange(ChangeMode mode) { - if (mode == ChangeMode.DELETE || mode == ChangeMode.REMOVE_ALL) - return null; - if (mode == ChangeMode.SET) - return CollectionUtils.array(Date.class); - return CollectionUtils.array(Timespan.class); - } - - @SuppressWarnings("null") - @Override - public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { - if (delta == null && mode != ChangeMode.RESET) - return; - Date now = new Date(); - for (Player player : getExpr().getArray(e)) { - if (mode == ChangeMode.ADD) { - - Date deathTime = convert(player); - long add = ((Timespan) delta[0]).getTicks_i(); - deathTime.add(Timespan.fromTicks_i(add)); - player.setStatistic(LAST_DEATH, (int) deathTime.difference(now).getTicks_i()); - - } else if (mode == ChangeMode.REMOVE) { - - Date deathTime = convert(player); - long remove = ((Timespan) delta[0]).getTicks_i(); - deathTime.subtract(Timespan.fromTicks_i(remove)); - player.setStatistic(LAST_DEATH, (int) deathTime.difference(now).getTicks_i()); - - } else if (mode == ChangeMode.SET) { - - /* - * Since the statistic is actually the time since the last death of the player, - * it needs to be set to the difference between now and the new date. - * For example, if we were setting the player's last death to 1 day ago, - * the statistic would need to be 24 hours, and that's what this would give us. - * - * If the new date is in the future, the statistic will remain unchanged. - */ - Date newDate = ((Date) delta[0]); - if (newDate.compareTo(now) < 1) - player.setStatistic(LAST_DEATH, (int) ((Date) delta[0]).difference(now).getTicks_i()); - - } else if (mode == ChangeMode.RESET) { - - player.setStatistic(LAST_DEATH, 0); - - } - } - } - - @Override - public Class getReturnType() { - return Date.class; - } - - @Override - protected String getPropertyName() { - return "last death"; - } - -} diff --git a/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java b/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java new file mode 100644 index 00000000000..35a3936277f --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java @@ -0,0 +1,181 @@ +/** + * 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 . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.expressions; + +import org.bukkit.Statistic; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +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.expressions.base.SimplePropertyExpression; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.util.Date; +import ch.njol.skript.util.Timespan; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; + +@Name("Last Death Time") +@Description({"The time of the last death of a player.", + "Any changes that would result in the death time being in the future are ignored.", + "This expression affects the 'TIME_SINCE_DEATH' statistic."}) +@Examples("send \"Your last death was %last death of player%!\" to player") +@Since("INSERT VERSION") +public class ExprLastDeathTime extends SimplePropertyExpression { + + static { + Skript.registerExpression(ExprLastDeathTime.class, Date.class, ExpressionType.PROPERTY, + "[the] time of [the] last death of %players%", + "[the] time of [the] %players%'[s] last death", + "%players%'[s] last death time", + "[the] last death time of %players%" + ); + } + + @SuppressWarnings({"null", "unchecked"}) + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + setExpr((Expression) exprs[0]); + return true; + } + + @Nullable + @Override + public Date convert(Player player) { + long ticksSinceDeath = player.getStatistic(Statistic.TIME_SINCE_DEATH); + if (ticksSinceDeath < 0) + return null; + Timespan timeSinceDeath = Timespan.fromTicks_i(ticksSinceDeath); + return Date.now().minus(timeSinceDeath); + } + + @Nullable + @Override + public Class[] acceptChange(ChangeMode mode) { + // Use a date for setting (e.g. 'set last death time of player to 1 hour ago') + if (mode == ChangeMode.SET) + return CollectionUtils.array(Date.class); + // Use a timespan for adding/remove (e.g 'add 10 seconds to the time of the player's last death') + if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) + return CollectionUtils.array(Timespan.class); + return null; + } + + /* + * Date#compare(date) + * This method returns whether the passed date is + * before, the same as, or after the date it's being compared to. + * A value less than 0 indicates that the passed date is AFTER the date it's being compared to. + * A value of 0 indicates that the passed date is the SAME the date it's being compared to. + * A value greater than 0 indicates that passed date is BEFORE the date it's being compared to. + * + * We use this method because we don't want the new death date to be in the future + */ + @SuppressWarnings("null") + @Override + public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { + + if (delta == null) + return; + + Date now = Date.now(); + + for (Player player : getExpr().getArray(e)) { + if (mode == ChangeMode.ADD) { + + // Get the current death time of the player. + Date deathTime = convert(player); + + deathTime.add((Timespan) delta[0]); + + if (deathTime.compareTo(now) < 1) { + // Get the timespan representing the new time since death (in ticks). + long newTimespanInTicks= deathTime.difference(now).getTicks_i(); + + /* + * Convert to int. + * If it is greater than the max value for an integer, + * the user probably wants to set it to the max value, right? + */ + int newTimeSinceDeath = newTimespanInTicks < Integer.MAX_VALUE ? (int) newTimespanInTicks : Integer.MAX_VALUE; + + player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); + } + + } else if (mode == ChangeMode.REMOVE) { + + // Get the current death time of the player. + Date deathTime = convert(player); + + deathTime.subtract((Timespan) delta[0]); + + // Get the timespan representing the new time since death (in ticks). + long newTimespanInTicks= deathTime.difference(now).getTicks_i(); + + /* + * Convert to int. + * If it is greater than the max value for an integer, + * the user probably wants to set it to the max value, right? + */ + int newTimeSinceDeath = newTimespanInTicks < Integer.MAX_VALUE ? (int) newTimespanInTicks : Integer.MAX_VALUE; + + player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); + + } else if (mode == ChangeMode.SET) { + + /* + * Since the statistic is actually the time since the last death of the player, + * it needs to be set to the difference between now and the new date. + * For example, if we were setting the player's last death to 1 day ago, + * the statistic would need to be 24 hours, and that's what this would give us. + * + * If the new date is in the future, the statistic will remain unchanged. + */ + + Date newDate = ((Date) delta[0]); + + if (newDate.compareTo(now) < 1) { + long newTimespanInTicks = ((Date) delta[0]).difference(now).getTicks_i(); + int newTimeSinceDeath = newTimespanInTicks < Integer.MAX_VALUE ? (int) newTimespanInTicks : Integer.MAX_VALUE; + player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); + } + + } + } + } + + @Override + public Class getReturnType() { + return Date.class; + } + + @Override + protected String getPropertyName() { + return "last death time"; + } + +} diff --git a/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java index a30fba70915..a715765349f 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java @@ -33,6 +33,7 @@ import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; +import ch.njol.skript.expressions.base.SimplePropertyExpression; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser.ParseResult; @@ -43,42 +44,31 @@ import ch.njol.util.coll.CollectionUtils; @Name("Time Since") -@Description("The time that has passed since a date. This will return 0 seconds if the given date is in the future.") -@Examples("send \"You died %time since last death of player% ago!\" to player") +@Description("The time that has passed since a date. If the given date is in the future, a value will not be returned.") +@Examples("send \"You died %time since % ago!\" to player") @Since("INSERT VERSION") -public class ExprTimeSince extends SimpleExpression { +public class ExprTimeSince extends SimplePropertyExpression { static { - Skript.registerExpression(ExprTimeSince.class, Timespan.class, ExpressionType.SIMPLE, "time since %dates%"); - } - - @SuppressWarnings("null") - private Expression dates; - - @SuppressWarnings({"unchecked", "null"}) - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - dates = (Expression) exprs[0]; - return true; + Skript.registerExpression(ExprTimeSince.class, Timespan.class, ExpressionType.PROPERTY, "time since %dates%"); } @Override @Nullable - protected Timespan[] get(Event e) { - List timespans = new ArrayList<>(); - Date now = new Date(); - for (Date date : dates.getArray(e)) - if (date.compareTo(now) > 0) { - timespans.add(new Timespan()); - } else { - timespans.add(date.difference(now)); - } - return timespans.toArray(new Timespan[0]); - } + public Timespan convert(Date date) { - @Override - public boolean isSingle() { - return dates.isSingle(); + Date now = Date.now(); + + /* + * This condition returns whether the date the player is using is + * before the current date, the same as the current date, or after the current date. + * A value less than 0 indicates that the new date is BEFORE the current date. + * A value of 0 indicates that the new date is the SAME as the current date. + * A value greater than 0 indicates that the new date is AFTER the current date. + */ + if (date.compareTo(now) < 1) + return date.difference(now); + return null; } @Override @@ -87,8 +77,8 @@ public Class getReturnType() { } @Override - public String toString(@Nullable Event e, boolean debug) { - return "time since " + dates.toString(e, debug); + protected String getPropertyName() { + return "time since"; } } From e469b90bb3ebd213f5a67b93a01902aaa2d746d8 Mon Sep 17 00:00:00 2001 From: APickledWalrus Date: Mon, 27 Apr 2020 08:28:11 -0400 Subject: [PATCH 3/4] Make Requested Changes --- .../ch/njol/skript/expressions/ExprLastDeathTime.java | 10 +++++----- .../java/ch/njol/skript/expressions/ExprTimeSince.java | 9 +++++++-- .../skript/tests/syntaxes/expressions/ExprTimeSince.sk | 3 ++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java b/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java index 35a3936277f..fb20007a94f 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java +++ b/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java @@ -43,7 +43,7 @@ @Description({"The time of the last death of a player.", "Any changes that would result in the death time being in the future are ignored.", "This expression affects the 'TIME_SINCE_DEATH' statistic."}) -@Examples("send \"Your last death was %last death of player%!\" to player") +@Examples("send \"Your last death was at %the last death time of the player%!\" to player") @Since("INSERT VERSION") public class ExprLastDeathTime extends SimplePropertyExpression { @@ -114,14 +114,14 @@ public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { if (deathTime.compareTo(now) < 1) { // Get the timespan representing the new time since death (in ticks). - long newTimespanInTicks= deathTime.difference(now).getTicks_i(); + long newTimespanInTicks = deathTime.difference(now).getTicks_i(); /* * Convert to int. * If it is greater than the max value for an integer, * the user probably wants to set it to the max value, right? */ - int newTimeSinceDeath = newTimespanInTicks < Integer.MAX_VALUE ? (int) newTimespanInTicks : Integer.MAX_VALUE; + int newTimeSinceDeath = Math.min((int) newTimespanInTicks, Integer.MAX_VALUE); player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); } @@ -141,7 +141,7 @@ public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { * If it is greater than the max value for an integer, * the user probably wants to set it to the max value, right? */ - int newTimeSinceDeath = newTimespanInTicks < Integer.MAX_VALUE ? (int) newTimespanInTicks : Integer.MAX_VALUE; + int newTimeSinceDeath = Math.min((int) newTimespanInTicks, Integer.MAX_VALUE); player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); @@ -160,7 +160,7 @@ public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { if (newDate.compareTo(now) < 1) { long newTimespanInTicks = ((Date) delta[0]).difference(now).getTicks_i(); - int newTimeSinceDeath = newTimespanInTicks < Integer.MAX_VALUE ? (int) newTimespanInTicks : Integer.MAX_VALUE; + int newTimeSinceDeath = Math.min((int) newTimespanInTicks, Integer.MAX_VALUE); player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); } diff --git a/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java index a715765349f..f1064936070 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java @@ -45,12 +45,12 @@ @Name("Time Since") @Description("The time that has passed since a date. If the given date is in the future, a value will not be returned.") -@Examples("send \"You died %time since % ago!\" to player") +@Examples("send \"You died %time since the last death time of the player% ago!\" to player") @Since("INSERT VERSION") public class ExprTimeSince extends SimplePropertyExpression { static { - Skript.registerExpression(ExprTimeSince.class, Timespan.class, ExpressionType.PROPERTY, "time since %dates%"); + Skript.registerExpression(ExprTimeSince.class, Timespan.class, ExpressionType.PROPERTY, "[the] time since %dates%"); } @Override @@ -81,4 +81,9 @@ protected String getPropertyName() { return "time since"; } + @Override + public String toString(final @Nullable Event e, final boolean debug) { + return "the time since " + getExpr().toString(e, debug); + } + } diff --git a/src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk b/src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk index ed3cffa40e4..fcb5e7652ac 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk @@ -7,4 +7,5 @@ test "time since": assert {_time} is 1 day with "time since 1 day ago failed | value: %{_time}%" set {_time} to time since 1 day later - assert {_time} is 0 seconds with "time since 1 day later failed | value: %{_time}%" + # {_time} won't update since "1 day later" returns null + assert {_time} is 1 day with "time since 1 day later failed | value: %{_time}%" From 3ec37e850f15e90116e7d4ce7157c616d83ef4fc Mon Sep 17 00:00:00 2001 From: APickledWalrus Date: Wed, 15 Jul 2020 20:58:06 -0400 Subject: [PATCH 4/4] Remove ExprLastDeathTime, clean up ExprTimeSince It was decided that ExprLastDeathTime should just be handled in a to-be-made ExprStatistic --- .../skript/expressions/ExprLastDeathTime.java | 181 ------------------ .../skript/expressions/ExprTimeSince.java | 14 +- 2 files changed, 1 insertion(+), 194 deletions(-) delete mode 100644 src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java diff --git a/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java b/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java deleted file mode 100644 index fb20007a94f..00000000000 --- a/src/main/java/ch/njol/skript/expressions/ExprLastDeathTime.java +++ /dev/null @@ -1,181 +0,0 @@ -/** - * 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 . - * - * - * Copyright 2011-2017 Peter Güttinger and contributors - */ -package ch.njol.skript.expressions; - -import org.bukkit.Statistic; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.eclipse.jdt.annotation.Nullable; - -import ch.njol.skript.Skript; -import ch.njol.skript.classes.Changer.ChangeMode; -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.expressions.base.SimplePropertyExpression; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.skript.util.Date; -import ch.njol.skript.util.Timespan; -import ch.njol.util.Kleenean; -import ch.njol.util.coll.CollectionUtils; - -@Name("Last Death Time") -@Description({"The time of the last death of a player.", - "Any changes that would result in the death time being in the future are ignored.", - "This expression affects the 'TIME_SINCE_DEATH' statistic."}) -@Examples("send \"Your last death was at %the last death time of the player%!\" to player") -@Since("INSERT VERSION") -public class ExprLastDeathTime extends SimplePropertyExpression { - - static { - Skript.registerExpression(ExprLastDeathTime.class, Date.class, ExpressionType.PROPERTY, - "[the] time of [the] last death of %players%", - "[the] time of [the] %players%'[s] last death", - "%players%'[s] last death time", - "[the] last death time of %players%" - ); - } - - @SuppressWarnings({"null", "unchecked"}) - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - setExpr((Expression) exprs[0]); - return true; - } - - @Nullable - @Override - public Date convert(Player player) { - long ticksSinceDeath = player.getStatistic(Statistic.TIME_SINCE_DEATH); - if (ticksSinceDeath < 0) - return null; - Timespan timeSinceDeath = Timespan.fromTicks_i(ticksSinceDeath); - return Date.now().minus(timeSinceDeath); - } - - @Nullable - @Override - public Class[] acceptChange(ChangeMode mode) { - // Use a date for setting (e.g. 'set last death time of player to 1 hour ago') - if (mode == ChangeMode.SET) - return CollectionUtils.array(Date.class); - // Use a timespan for adding/remove (e.g 'add 10 seconds to the time of the player's last death') - if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) - return CollectionUtils.array(Timespan.class); - return null; - } - - /* - * Date#compare(date) - * This method returns whether the passed date is - * before, the same as, or after the date it's being compared to. - * A value less than 0 indicates that the passed date is AFTER the date it's being compared to. - * A value of 0 indicates that the passed date is the SAME the date it's being compared to. - * A value greater than 0 indicates that passed date is BEFORE the date it's being compared to. - * - * We use this method because we don't want the new death date to be in the future - */ - @SuppressWarnings("null") - @Override - public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { - - if (delta == null) - return; - - Date now = Date.now(); - - for (Player player : getExpr().getArray(e)) { - if (mode == ChangeMode.ADD) { - - // Get the current death time of the player. - Date deathTime = convert(player); - - deathTime.add((Timespan) delta[0]); - - if (deathTime.compareTo(now) < 1) { - // Get the timespan representing the new time since death (in ticks). - long newTimespanInTicks = deathTime.difference(now).getTicks_i(); - - /* - * Convert to int. - * If it is greater than the max value for an integer, - * the user probably wants to set it to the max value, right? - */ - int newTimeSinceDeath = Math.min((int) newTimespanInTicks, Integer.MAX_VALUE); - - player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); - } - - } else if (mode == ChangeMode.REMOVE) { - - // Get the current death time of the player. - Date deathTime = convert(player); - - deathTime.subtract((Timespan) delta[0]); - - // Get the timespan representing the new time since death (in ticks). - long newTimespanInTicks= deathTime.difference(now).getTicks_i(); - - /* - * Convert to int. - * If it is greater than the max value for an integer, - * the user probably wants to set it to the max value, right? - */ - int newTimeSinceDeath = Math.min((int) newTimespanInTicks, Integer.MAX_VALUE); - - player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); - - } else if (mode == ChangeMode.SET) { - - /* - * Since the statistic is actually the time since the last death of the player, - * it needs to be set to the difference between now and the new date. - * For example, if we were setting the player's last death to 1 day ago, - * the statistic would need to be 24 hours, and that's what this would give us. - * - * If the new date is in the future, the statistic will remain unchanged. - */ - - Date newDate = ((Date) delta[0]); - - if (newDate.compareTo(now) < 1) { - long newTimespanInTicks = ((Date) delta[0]).difference(now).getTicks_i(); - int newTimeSinceDeath = Math.min((int) newTimespanInTicks, Integer.MAX_VALUE); - player.setStatistic(Statistic.TIME_SINCE_DEATH, newTimeSinceDeath); - } - - } - } - } - - @Override - public Class getReturnType() { - return Date.class; - } - - @Override - protected String getPropertyName() { - return "last death time"; - } - -} diff --git a/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java index f1064936070..691b06c6fd9 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTimeSince.java @@ -19,33 +19,22 @@ */ package ch.njol.skript.expressions; -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.Statistic; -import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.Skript; -import ch.njol.skript.classes.Changer.ChangeMode; 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.expressions.base.SimplePropertyExpression; -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.skript.util.Date; import ch.njol.skript.util.Timespan; -import ch.njol.util.Kleenean; -import ch.njol.util.coll.CollectionUtils; @Name("Time Since") @Description("The time that has passed since a date. If the given date is in the future, a value will not be returned.") -@Examples("send \"You died %time since the last death time of the player% ago!\" to player") +@Examples("send \"%time since 5 minecraft days ago% has passed since 5 minecraft days ago!\" to player") @Since("INSERT VERSION") public class ExprTimeSince extends SimplePropertyExpression { @@ -56,7 +45,6 @@ public class ExprTimeSince extends SimplePropertyExpression { @Override @Nullable public Timespan convert(Date date) { - Date now = Date.now(); /*