Skip to content

Commit

Permalink
Fixes to ScheduledUpdate API
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Nov 2, 2024
1 parent ccfca26 commit f131f23
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@
*/
package org.spongepowered.common.bridge.world.ticks;

import java.util.function.LongSupplier;
import net.minecraft.server.level.ServerLevel;

public interface LevelTicksBridge<T> {

LongSupplier bridge$getGameTime();

void bridge$setGameTimeSupplier(LongSupplier supplier);

LongSupplier bridge$getNextSubTickCountSupplier();

void bridge$setNextSubTickCountSupplier(LongSupplier supplier);
void bridge$level(ServerLevel level);

ServerLevel bridge$level();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package org.spongepowered.common.bridge.world.ticks;

import net.minecraft.world.level.Level;
import net.minecraft.world.ticks.LevelTicks;
import org.spongepowered.api.scheduler.ScheduledUpdate;
import org.spongepowered.api.world.server.ServerLocation;
Expand All @@ -35,8 +34,6 @@ public interface TickNextTickDataBridge<T> {

void bridge$createdByList(LevelTicks<T> tickList);

void bridge$setWorld(Level world);

ServerLocation bridge$getLocation();

ScheduledUpdate.State bridge$internalState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ public ScheduledUpdate<T> schedule(
throw new IllegalArgumentException("Delay cannot be infinite!");
}
final var blockPos = new BlockPos(x, y, z);
final var gameTime = ((LevelTicksBridge<T>) this).bridge$getGameTime().getAsLong();
final var subCount = ((LevelTicksBridge<T>) this).bridge$getNextSubTickCountSupplier().getAsLong();
final var level = ((LevelTicksBridge<T>) this).bridge$level();
final var gameTime = level.getLevelData().getGameTime();
final var subCount = level.nextSubTickCount();
final var scheduledUpdate = new ScheduledTick<>(
target, blockPos, tickDelay.ticks() + gameTime, (TickPriority) (Object) priority, subCount);
if (!this.tickCheck.test(ChunkPos.asLong(blockPos))) {
if (this.tickCheck.test(ChunkPos.asLong(blockPos))) {
this.shadow$schedule(scheduledUpdate);
}
return (ScheduledUpdate<T>) (Object) scheduledUpdate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public abstract class ServerLevelMixin extends LevelMixin implements ServerLevel
this.impl$levelSave = $$2;
this.impl$chunkStatusListener = $$6;
this.impl$prevWeather = ((ServerWorld) this).weather();
((LevelTicksBridge<?>) this.blockTicks).bridge$setGameTimeSupplier(this.levelData::getGameTime);
((LevelTicksBridge<?>) this.fluidTicks).bridge$setGameTimeSupplier(this.levelData::getGameTime);
((LevelTicksBridge<?>) this.blockTicks).bridge$level((ServerLevel) (Object) this);
((LevelTicksBridge<?>) this.fluidTicks).bridge$level((ServerLevel) (Object) this);

final Boolean createDragonFight = ((DimensionTypeBridge) (Object) this.shadow$dimensionType()).bridge$createDragonFight();
if (createDragonFight != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@
import net.minecraft.nbt.ListTag;
import net.minecraft.world.ticks.LevelChunkTicks;
import net.minecraft.world.ticks.LevelTicks;
import net.minecraft.world.ticks.SavedTick;
import net.minecraft.world.ticks.ScheduledTick;
import org.spongepowered.api.scheduler.ScheduledUpdate;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.bridge.world.ticks.LevelChunkTicksBridge;
import org.spongepowered.common.bridge.world.ticks.TickNextTickDataBridge;

import java.util.function.Function;

@Mixin(LevelChunkTicks.class)
public abstract class LevelChunkTicksMixin<T> implements LevelChunkTicksBridge<T> {

Expand All @@ -56,12 +58,12 @@ public abstract class LevelChunkTicksMixin<T> implements LevelChunkTicksBridge<T
}

@SuppressWarnings("unchecked")
@ModifyArg(method = "save(JLjava/util/function/Function;)Lnet/minecraft/nbt/ListTag;",
@Redirect(method = "save(JLjava/util/function/Function;)Lnet/minecraft/nbt/ListTag;",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/ticks/SavedTick;saveTick(Lnet/minecraft/world/ticks/ScheduledTick;Ljava/util/function/Function;J)Lnet/minecraft/nbt/CompoundTag;"))
private ScheduledTick<T> impl$onSaveSkipCancelled(final ScheduledTick<T> $$0) {
private CompoundTag impl$onSaveSkipCancelled(final ScheduledTick<T> $$0, final Function<T, String> $$1, final long $$2) {
final ScheduledUpdate.State state = ((TickNextTickDataBridge<T>) (Object) $$0).bridge$internalState();
if (state != ScheduledUpdate.State.CANCELLED) {
return $$0;
return SavedTick.saveTick($$0, $$1, $$2);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
package org.spongepowered.common.mixin.core.world.ticks;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.ticks.LevelChunkTicks;
import net.minecraft.world.ticks.LevelTicks;
import net.minecraft.world.ticks.ScheduledTick;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.spongepowered.api.scheduler.ScheduledUpdate;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -40,16 +42,14 @@
import org.spongepowered.common.bridge.world.ticks.LevelTicksBridge;
import org.spongepowered.common.bridge.world.ticks.TickNextTickDataBridge;

import java.util.Objects;
import java.util.Queue;
import java.util.function.BiConsumer;
import java.util.function.LongSupplier;

@Mixin(LevelTicks.class)
public abstract class LevelTicksMixin<T> implements LevelTicksBridge<T> {

// @formatter:off
private LongSupplier impl$gameTimeSupplier = () -> 0;
@MonotonicNonNull private ServerLevel impl$level;
// @formatter:on

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -95,19 +95,19 @@ public abstract class LevelTicksMixin<T> implements LevelTicksBridge<T> {
return queue.add((ScheduledTick<T>) data);
}

@Override
public LongSupplier bridge$getGameTime() {
return this.impl$gameTimeSupplier;
@SuppressWarnings("unchecked")
@Inject(method = "addContainer", at = @At("HEAD"))
private void impl$onAddContainer(final ChunkPos $$0, final LevelChunkTicks<T> $$1, final CallbackInfo ci) {
((LevelChunkTicksBridge<T>) $$1).bridge$setTickList((LevelTicks<T>) (Object) this);
}

@Override
public void bridge$setGameTimeSupplier(LongSupplier supplier) {
this.impl$gameTimeSupplier = Objects.requireNonNull(supplier, "gametime supplier cannot be null");
public void bridge$level(final ServerLevel level) {
this.impl$level = level;
}

@SuppressWarnings("unchecked")
@Inject(method = "addContainer", at = @At("HEAD"))
private void impl$onAddContainer(final ChunkPos $$0, final LevelChunkTicks<T> $$1, final CallbackInfo ci) {
((LevelChunkTicksBridge<T>) $$1).bridge$setTickList((LevelTicks<T>) (Object) this);
@Override
public ServerLevel bridge$level() {
return this.impl$level;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import net.kyori.adventure.util.Ticks;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.ticks.LevelTicks;
import net.minecraft.world.ticks.ScheduledTick;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
Expand Down Expand Up @@ -57,15 +57,10 @@ public abstract class ScheduledTickMixin<T> implements TickNextTickDataBridge<T>
@SuppressWarnings("unchecked")
@Override
public void bridge$createdByList(final LevelTicks<T> tickList) {
final ServerLevel level = ((LevelTicksBridge<T>) tickList).bridge$level();
this.impl$parentTickList = tickList;
this.impl$scheduledTime = ((LevelTicksBridge<T>) tickList).bridge$getGameTime().getAsLong();
}

@Override
public void bridge$setWorld(final Level world) {
Preconditions.checkState(this.impl$location == null, "World already known");
final BlockPos position = this.pos;
this.impl$location = ServerLocation.of((ServerWorld) world, position.getX(), position.getY(), position.getZ());
this.impl$scheduledTime = level.getLevelData().getGameTime();
this.impl$location = ServerLocation.of((ServerWorld) level, this.pos.getX(), this.pos.getY(), this.pos.getZ());
}

@Override
Expand Down

0 comments on commit f131f23

Please sign in to comment.