Skip to content

Commit

Permalink
Fix WearBlackAllDay#72. Time ticking is not thread-safe and cause pis…
Browse files Browse the repository at this point in the history
…ton problems on worlds that does not tick the time
  • Loading branch information
CaveNightingale committed Oct 15, 2022
1 parent 66a519c commit ff3c9a9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void onMoveToWorld(ServerWorld destination, CallbackInfoReturnable<Entity
this.uncompletedTeleportTargetForMoveToWorld = null;
this.nbtCachedForMoveToWorld = null;
this.world.spawnEntity((Entity) (Object) this);// if the teleporting failed, we need to add it back to the world
LOGGER.debug("Failed to teleport {}, return it to it's previous world", this);
LOGGER.debug("Failed to teleport {}, return it to its previous world", this);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import wearblackallday.dimthread.DimThread;
import wearblackallday.dimthread.util.CrashInfo;
import wearblackallday.dimthread.util.ServerWorldAccessor;
import wearblackallday.dimthread.util.ThreadPool;

import java.util.Collections;
Expand Down Expand Up @@ -72,6 +73,7 @@ public void tickWorlds(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {
});

pool.awaitCompletion();
getWorlds().forEach(world -> ((ServerWorldAccessor) world).dimthread_tickTime()); // Time ticking is not thread-safe, fix https://github.com/WearBlackAllDay/DimensionalThreading/issues/72

if(crash.get() != null) {
crash.get().crash("Exception ticking world");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package wearblackallday.dimthread.mixin;

import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.util.registry.RegistryEntry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.MutableWorldProperties;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import wearblackallday.dimthread.DimThread;
import wearblackallday.dimthread.util.ServerWorldAccessor;

import java.util.function.Supplier;

@Mixin(ServerWorld.class)
public abstract class ServerWorldMixin extends World implements ServerWorldAccessor {
protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey<World> registryRef, RegistryEntry<DimensionType> registryEntry, Supplier<Profiler> profiler, boolean isClient, boolean debugWorld, long seed) {
super(properties, registryRef, registryEntry, profiler, isClient, debugWorld, seed);
}

@Shadow protected abstract void tickTime();

boolean onMainThread = false;
boolean timeTickedOnWorldThread = false;

/**
* Time ticking is not thread-safe. We cancel time ticking from the world thread. However, DimThread will tick time on the main thread
*/
@Inject(method = "tickTime", at = @At("HEAD"), cancellable = true)
private void preventTimeTicking(CallbackInfo ci) {
if (DimThread.MANAGER.isActive(getServer()) && !onMainThread) {
timeTickedOnWorldThread = true;
ci.cancel();
}
}

@Override
public void dimthread_tickTime() {
if (timeTickedOnWorldThread) {
onMainThread = true;
tickTime();
onMainThread = false;
timeTickedOnWorldThread = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package wearblackallday.dimthread.util;

/**
* Create this calass wo that we can call tickTime from ServerWorldMixin
*/
public interface ServerWorldAccessor {
void dimthread_tickTime();
}
1 change: 1 addition & 0 deletions src/main/resources/dimthread.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"MinecraftServerMixin",
"RedstoneWireBlockMixin",
"ServerChunkManagerMixin",
"ServerWorldMixin",
"WorldMixin"
],
"client": [
Expand Down

0 comments on commit ff3c9a9

Please sign in to comment.