From 7ab1ca33719862485d3e37fca5da37d0a512c9f8 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Wed, 15 Jan 2025 04:22:12 -0500 Subject: [PATCH] Initial Commit --- .../skript/expressions/ExprPlayerWeather.java | 69 ----------- .../njol/skript/expressions/ExprWeather.java | 110 +++++++++++------- .../tests/syntaxes/expressions/ExprWeather.sk | 7 ++ 3 files changed, 72 insertions(+), 114 deletions(-) delete mode 100644 src/main/java/ch/njol/skript/expressions/ExprPlayerWeather.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprWeather.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprPlayerWeather.java b/src/main/java/ch/njol/skript/expressions/ExprPlayerWeather.java deleted file mode 100644 index 5cb17f56877..00000000000 --- a/src/main/java/ch/njol/skript/expressions/ExprPlayerWeather.java +++ /dev/null @@ -1,69 +0,0 @@ -package ch.njol.skript.expressions; - -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.PropertyExpression; -import ch.njol.skript.expressions.base.SimplePropertyExpression; -import ch.njol.skript.util.WeatherType; -import ch.njol.util.coll.CollectionUtils; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.jetbrains.annotations.Nullable; - -/** - * @author Nicofisi - * @author Peter Güttinger - */ -@Name("Player Weather") -@Description("The weather for a player.") -@Examples({"set weather of arg-player to rainy", - "reset player's weather", - "if arg-player's weather is rainy"}) -@Since("2.2-dev34") -public class ExprPlayerWeather extends SimplePropertyExpression { - - static { - register(ExprPlayerWeather.class, WeatherType.class, "[(client|custom)] weather", "players"); - } - - @Override - protected String getPropertyName() { - return "weather"; - } - - @Override - @Nullable - public WeatherType convert(Player player) { - return WeatherType.fromPlayer(player); - } - - @Override - public Class getReturnType() { - return WeatherType.class; - } - - @Override - @Nullable - public Class[] acceptChange(final ChangeMode mode) { - if (mode == ChangeMode.RESET || mode == ChangeMode.SET) - return CollectionUtils.array(WeatherType.class); - return null; - } - - @SuppressWarnings("null") - @Override - public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) { - if (delta == null) { - for (Player p : getExpr().getArray(e)) - p.resetPlayerWeather(); - } else { - WeatherType type = (WeatherType) delta[0]; - for (Player p : getExpr().getArray(e)) - type.setWeather(p); - } - } - -} \ No newline at end of file diff --git a/src/main/java/ch/njol/skript/expressions/ExprWeather.java b/src/main/java/ch/njol/skript/expressions/ExprWeather.java index e56d2b722b2..96cce9d239d 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprWeather.java +++ b/src/main/java/ch/njol/skript/expressions/ExprWeather.java @@ -12,76 +12,92 @@ import ch.njol.util.Kleenean; import ch.njol.util.coll.CollectionUtils; import org.bukkit.World; +import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.weather.ThunderChangeEvent; import org.bukkit.event.weather.WeatherChangeEvent; import org.bukkit.event.weather.WeatherEvent; import org.jetbrains.annotations.Nullable; -/** - * @author Peter Güttinger - */ @Name("Weather") -@Description("The weather in the given or the current world.") -@Examples({"set weather to clear", - "weather in \"world\" is rainy"}) +@Description({ + "The weather of a world or player.", + "Clearing or resetting the weather of a player will make the player's weather match the weather of the world.", + "Clearing or resetting the weather of a world will make the weather clear." +}) +@Examples({ + "set weather to clear", + "weather in \"world\" is rainy", + "reset custom weather of player", + "set weather of player to clear" +}) @Since("1.0") @Events("weather change") -public class ExprWeather extends PropertyExpression { +public class ExprWeather extends PropertyExpression { + static { - Skript.registerExpression(ExprWeather.class, WeatherType.class, ExpressionType.PROPERTY, "[the] weather [(in|of) %worlds%]", "%worlds%'[s] weather"); + Skript.registerExpression(ExprWeather.class, WeatherType.class, ExpressionType.PROPERTY, + "[the] [custom|client] weather [(in|of) %worlds/players%]", + "%worlds/players%'[s] [custom|client] weather"); } - @SuppressWarnings({"unchecked", "null"}) @Override - public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) { - setExpr((Expression) exprs[0]); + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { + setExpr(exprs[0]); return true; } @Override - protected WeatherType[] get(Event event, World[] source) { - return get(source, world -> { - if (getTime() >= 0 && event instanceof WeatherEvent weatherEvent - && world.equals(weatherEvent.getWorld()) && !Delay.isDelayed(event)) - return WeatherType.fromEvent(weatherEvent); - else + protected WeatherType @Nullable [] get(Event event, Object[] source) { + World eventWorld = event instanceof WeatherEvent weatherEvent ? weatherEvent.getWorld() : null; + return get(source, object -> { + if (object instanceof Player player) { + return WeatherType.fromPlayer(player); + } else if (object instanceof World world) { + if (eventWorld != null && world.equals(eventWorld) && getTime() >= 0 && !Delay.isDelayed(event)) + return WeatherType.fromEvent((WeatherEvent) event); return WeatherType.fromWorld(world); + } + return null; }); } @Override - public String toString(final @Nullable Event e, final boolean debug) { - return "the weather in " + getExpr().toString(e, debug); - } - - @Override - @Nullable - public Class[] acceptChange(final ChangeMode mode) { - if (mode == ChangeMode.DELETE || mode == ChangeMode.SET) + public Class @Nullable [] acceptChange(ChangeMode mode) { + if (mode == ChangeMode.DELETE || mode == ChangeMode.SET || mode == ChangeMode.RESET) return CollectionUtils.array(WeatherType.class); return null; } @Override - public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) { - final WeatherType t = delta == null ? WeatherType.CLEAR : (WeatherType) delta[0]; - for (final World w : getExpr().getArray(e)) { - assert w != null : getExpr(); - if (getTime() >= 0 && e instanceof WeatherEvent && w.equals(((WeatherEvent) e).getWorld()) && !Delay.isDelayed(e)) { - if (e instanceof WeatherChangeEvent) { - if (((WeatherChangeEvent) e).toWeatherState() && t == WeatherType.CLEAR) - ((WeatherChangeEvent) e).setCancelled(true); - if (((WeatherChangeEvent) e).getWorld().isThundering() != (t == WeatherType.THUNDER)) - ((WeatherChangeEvent) e).getWorld().setThundering(t == WeatherType.THUNDER); - } else if (e instanceof ThunderChangeEvent) { - if (((ThunderChangeEvent) e).toThunderState() && t != WeatherType.THUNDER) - ((ThunderChangeEvent) e).setCancelled(true); - if (((ThunderChangeEvent) e).getWorld().hasStorm() == (t == WeatherType.CLEAR)) - ((ThunderChangeEvent) e).getWorld().setStorm(t != WeatherType.CLEAR); + public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { + WeatherType playerWeather = delta != null ? (WeatherType) delta[0] : null; + WeatherType worldWeather = playerWeather != null ? playerWeather : WeatherType.CLEAR; + + World eventWorld = event instanceof WeatherEvent weatherEvent ? weatherEvent.getWorld() : null; + for (Object object : getExpr().getArray(event)) { + if (object instanceof Player player) { + if (playerWeather != null) { + playerWeather.setWeather(player); + } else { + player.resetPlayerWeather(); + } + } else if (object instanceof World world) { + if (eventWorld != null && world.equals(eventWorld) && getTime() >= 0 && !Delay.isDelayed(event)) { + if (event instanceof WeatherChangeEvent weatherChangeEvent) { + if (weatherChangeEvent.toWeatherState() && worldWeather == WeatherType.CLEAR) + weatherChangeEvent.setCancelled(true); + if (eventWorld.isThundering() != (worldWeather == WeatherType.THUNDER)) + eventWorld.setThundering(worldWeather == WeatherType.THUNDER); + } else if (event instanceof ThunderChangeEvent thunderChangeEvent) { + if (thunderChangeEvent.toThunderState() && worldWeather != WeatherType.THUNDER) + thunderChangeEvent.setCancelled(true); + if (eventWorld.hasStorm() == (worldWeather == WeatherType.CLEAR)) + eventWorld.setStorm(worldWeather != WeatherType.CLEAR); + } + } else { + worldWeather.setWeather(world); } - } else { - t.setWeather(w); } } } @@ -90,10 +106,14 @@ public void change(final Event e, final @Nullable Object[] delta, final ChangeMo public Class getReturnType() { return WeatherType.class; } - - @SuppressWarnings("unchecked") + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "the weather of " + getExpr().toString(event, debug); + } + @Override - public boolean setTime(final int time) { + public boolean setTime(int time) { return super.setTime(time, getExpr(), WeatherChangeEvent.class, ThunderChangeEvent.class); } diff --git a/src/test/skript/tests/syntaxes/expressions/ExprWeather.sk b/src/test/skript/tests/syntaxes/expressions/ExprWeather.sk new file mode 100644 index 00000000000..690f2c07c71 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprWeather.sk @@ -0,0 +1,7 @@ +test "world weather": + set weather of world "world" to rainy + assert the weather of world "world" is rainy with "World weather should be rainy" + set weather of world "world" to thunder + assert the weather of world "world" is thunder with "World weather should be thunder" + clear the weather of world "world" + assert the weather of world "world" is clear with "World weather was not cleared"