Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExprWeather + ExprPlayerWeather Merge #7442

Open
wants to merge 2 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions src/main/java/ch/njol/skript/expressions/ExprPlayerWeather.java

This file was deleted.

110 changes: 65 additions & 45 deletions src/main/java/ch/njol/skript/expressions/ExprWeather.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<World, WeatherType> {
public class ExprWeather extends PropertyExpression<Object, WeatherType> {

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");
Comment on lines +40 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client weather of %world% doesn't make sense. It might be better to make custom and client exclusive to player weathers.

TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
setExpr((Expression<World>) 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))
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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)) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}
Expand All @@ -90,10 +106,14 @@ public void change(final Event e, final @Nullable Object[] delta, final ChangeMo
public Class<WeatherType> getReturnType() {
return WeatherType.class;
}

@SuppressWarnings("unchecked")

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the weather of " + getExpr().toString(event, debug);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean setTime(final int time) {
public boolean setTime(int time) {
return super.setTime(time, getExpr(), WeatherChangeEvent.class, ThunderChangeEvent.class);
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprWeather.sk
Original file line number Diff line number Diff line change
@@ -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"
Loading