Skip to content
Open
Changes from 3 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
62 changes: 30 additions & 32 deletions src/main/java/ch/njol/skript/expressions/ExprTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ch.njol.skript.util.Time;
import ch.njol.skript.util.Timeperiod;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Timespan.TimePeriod;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.World;
Expand All @@ -35,12 +36,14 @@
public class ExprTime extends PropertyExpression<World, Time> {

// 18000 is the offset to allow for using "add 2:00" without going to a new day
// and causing unexpected behaviour
// and causing unexpected behavior
private static final int TIME_TO_TIMESPAN_OFFSET = 18000;

static {
Skript.registerExpression(ExprTime.class, Time.class, ExpressionType.PROPERTY,
"[the] time[s] [([with]in|of) %worlds%]", "%worlds%'[s] time[s]");
"[the] time[s] [([with]in|of) %worlds%]",
"%worlds%'[s] time[s]"
);
}

@SuppressWarnings("unchecked")
Expand All @@ -57,54 +60,49 @@ protected Time[] get(Event event, World[] worlds) {

@Override
@Nullable
Comment thread
erenkarakal marked this conversation as resolved.
Outdated
public Class<?>[] acceptChange(final ChangeMode mode) {
switch (mode) {
case ADD:
case REMOVE:
public Class<?>[] acceptChange(ChangeMode mode) {
Comment thread
erenkarakal marked this conversation as resolved.
Outdated
return switch (mode) {
case ADD, REMOVE ->
// allow time to avoid conversion to timespan, which causes all sorts of headaches
return CollectionUtils.array(Time.class, Timespan.class);
case SET:
return CollectionUtils.array(Time.class, Timeperiod.class);
default:
return null;
}
CollectionUtils.array(Time.class, Timespan.class);
case SET -> CollectionUtils.array(Time.class, Timeperiod.class);
default -> null;
};
}

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
if (delta == null)
return;

Object time = delta[0];
if (time == null)
Object t = delta[0];
if (t == null)
return;

World[] worlds = getExpr().getArray(event);

long ticks = 0;
Comment thread
erenkarakal marked this conversation as resolved.
Outdated
if (time instanceof Time) {
if (mode != ChangeMode.SET) {
ticks = ((Time) time).getTicks() - TIME_TO_TIMESPAN_OFFSET;
} else {
ticks = ((Time) time).getTicks();
switch (t) {
case Time time -> {
if (mode != ChangeMode.SET) {
ticks = time.getTicks() - TIME_TO_TIMESPAN_OFFSET;
} else {
ticks = time.getTicks();
}
}
} else if (time instanceof Timespan) {
ticks = ((Timespan) time).getAs(Timespan.TimePeriod.TICK);
} else if (time instanceof Timeperiod) {
ticks = ((Timeperiod) time).start;
case Timespan timespan -> ticks = timespan.getAs(TimePeriod.TICK);
case Timeperiod timeperiod -> ticks = timeperiod.start;
default -> {}
}

for (World world : worlds) {
if (world.getFullTime() == 0)
continue; // the world doesn't have a world clock, can't modify time

switch (mode) {
case ADD:
world.setTime(world.getTime() + ticks);
break;
case REMOVE:
world.setTime(world.getTime() - ticks);
break;
case SET:
world.setTime(ticks);
break;
case ADD -> world.setTime(world.getTime() + ticks);
case REMOVE -> world.setTime(world.getTime() - ticks);
case SET -> world.setTime(ticks);
}
}
}
Expand Down
Loading