Skip to content
Merged
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
2 changes: 1 addition & 1 deletion SpongeAPI
15 changes: 15 additions & 0 deletions src/main/java/org/spongepowered/common/util/SpongeTicks.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ public static int toSaturatedIntOrInfinite(final Ticks ticks, final int infinite
return (int) ticksValue;
}

public static long toSaturatedLongOrInfinite(final Ticks ticks) {
return SpongeTicks.toSaturatedLongOrInfinite(ticks, Constants.TickConversions.INFINITE_TICKS);
}

public static long toSaturatedLongOrInfinite(final Ticks ticks, final long infiniteTicksMarker) {
if (ticks.isInfinite()) {
return infiniteTicksMarker;
}
long ticksValue = ticks.ticks();
if (infiniteTicksMarker > 0 && ticksValue >= infiniteTicksMarker) {
ticksValue = infiniteTicksMarker - 1;
}
return ticksValue;
}

public static Ticks ticksOrInfinite(final long ticks) {
return SpongeTicks.ticksOrInfinite(ticks, Constants.TickConversions.INFINITE_TICKS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
*/
package org.spongepowered.common.world.border;

import org.spongepowered.api.util.Ticks;
import org.spongepowered.api.world.border.WorldBorder;
import org.spongepowered.common.accessor.world.level.border.WorldBorder_SettingsAccessor;
import org.spongepowered.common.bridge.world.level.border.WorldBorderBridge;
import org.spongepowered.common.util.SpongeTicks;
import org.spongepowered.math.vector.Vector2d;

import java.time.Duration;

public final class SpongeWorldBorderBuilder implements WorldBorder.Builder {

private double diameter = -1;
private double initialDiameter = -1;
private Duration time = Duration.ZERO;
private Ticks time = Ticks.zero();
private Vector2d center = Vector2d.ZERO; //use a default value otherwise null is used
private Duration warningTime = Duration.ZERO;
private Ticks warningTime = Ticks.zero();
private double warningDistance;
private double safeZone;
private double damagePerBlock;
Expand Down Expand Up @@ -77,11 +77,8 @@ public WorldBorder.Builder targetDiameter(final double diameter) {
}

@Override
public WorldBorder.Builder timeToTargetDiameter(final Duration time) {
if (time.isNegative()) {
throw new IllegalArgumentException("time cannot be negative");
}
this.time = time;
public WorldBorder.Builder timeToTargetDiameter(final Ticks ticks) {
this.time = ticks;
return this;
}

Expand Down Expand Up @@ -122,10 +119,7 @@ public WorldBorder.Builder damagePerBlock(final double damagePerBlock) {
}

@Override
public WorldBorder.Builder warningTime(final Duration warningTime) {
if (warningTime.isNegative()) {
throw new IllegalArgumentException("warning time cannot be negative");
}
public WorldBorder.Builder warningTime(final Ticks warningTime) {
this.warningTime = warningTime;
return this;
}
Expand All @@ -151,9 +145,9 @@ public WorldBorder build() throws IllegalStateException {
this.damagePerBlock,
this.safeZone,
(int) this.warningDistance,
(int) this.warningTime.getSeconds(),
SpongeTicks.toSaturatedIntOrInfinite(this.warningTime),
this.initialDiameter == -1 ? this.diameter : this.initialDiameter,
this.time.toMillis(),
SpongeTicks.toSaturatedLongOrInfinite(this.time),
this.diameter
);
}
Expand All @@ -165,9 +159,9 @@ public WorldBorder.Builder reset() {
this.safeZone = 0;
this.diameter = -1;
this.initialDiameter = -1;
this.time = Duration.ZERO;
this.time = Ticks.zero();
this.warningDistance = 0;
this.warningTime = Duration.ZERO;
this.warningTime = Ticks.zero();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
package org.spongepowered.common.mixin.api.minecraft.world.level.border;

import net.minecraft.world.level.border.WorldBorder;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.util.SpongeTicks;
import org.spongepowered.math.vector.Vector2d;

import java.time.Duration;

@Mixin(WorldBorder.Settings.class)
@Implements(@Interface(iface = org.spongepowered.api.world.border.WorldBorder.class, prefix = "api$"))
Expand Down Expand Up @@ -61,8 +62,8 @@ public double targetDiameter() {
}

@Override
public Duration timeUntilTargetDiameter() {
return Duration.ofMillis(this.shadow$lerpTime());
public Ticks timeUntilTargetDiameter() {
return SpongeTicks.ticksOrInfinite(this.shadow$lerpTime());
}

@Override
Expand All @@ -81,8 +82,8 @@ public double diameter() {
}

@Intrinsic
public Duration api$warningTime() {
return Duration.ofMillis(this.shadow$warningTime());
public Ticks api$warningTime() {
return SpongeTicks.ticksOrInfinite(this.shadow$warningTime());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.bridge.world.level.border.WorldBorderBridge;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.util.SpongeTicks;
import org.spongepowered.common.world.border.SpongeWorldBorderBuilder;

import java.time.Duration;
import java.util.Optional;
import java.util.function.Supplier;

Expand Down Expand Up @@ -83,13 +83,13 @@ public abstract class WorldBorderMixin implements WorldBorderBridge {
}

@Inject(method = "lerpSizeBetween", at = @At(value = "HEAD"), cancellable = true)
private void impl$onLerping(final double initial, final double target, final long milliseconds, final long delay, final CallbackInfo ci) {
private void impl$onLerping(final double initial, final double target, final long ticks, final long delay, final CallbackInfo ci) {
if (this.impl$fireEvent) {
final Supplier<org.spongepowered.api.world.border.WorldBorder> proposed =
() -> new SpongeWorldBorderBuilder().from(this)
.initialDiameter(initial)
.targetDiameter(target)
.timeToTargetDiameter(Duration.ofMillis(milliseconds))
.timeToTargetDiameter(SpongeTicks.ticksOrInfinite(ticks))
.build();
if (this.impl$suppressOriginalAction(proposed)) {
ci.cancel();
Expand Down Expand Up @@ -141,7 +141,7 @@ public abstract class WorldBorderMixin implements WorldBorderBridge {
if (this.impl$fireEvent) {
final Supplier<org.spongepowered.api.world.border.WorldBorder> proposed =
() -> new SpongeWorldBorderBuilder().from(this)
.warningTime(Duration.ofSeconds(warningTime))
.warningTime(SpongeTicks.ticksOrInfinite(warningTime))
.build();
if (this.impl$suppressOriginalAction(proposed)) {
ci.cancel();
Expand Down Expand Up @@ -225,7 +225,7 @@ public abstract class WorldBorderMixin implements WorldBorderBridge {
// TODO - figure out how to get the appropriate game time
((WorldBorder) (Object) this).lerpSizeBetween(worldBorder.size(), worldBorder.lerpTarget(), worldBorder.lerpTime(), SpongeCommon.server().overworld().getGameTime());
} else {
((WorldBorder) (Object) this).setSize(worldBorder.size());
((WorldBorder) (Object) this).setSize(worldBorder.lerpTime() == -1 ? worldBorder.size() : worldBorder.lerpTarget());
}
}

Expand Down
Loading