Skip to content

Commit 841cdce

Browse files
Fix #1
1 parent 6e35f6a commit 841cdce

3 files changed

Lines changed: 39 additions & 16 deletions

File tree

src/main/java/wearblackallday/dimthread/mixin/EntityMixin.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,25 @@ public void onMoveToWorld(ServerWorld destination, CallbackInfoReturnable<Entity
104104
return;
105105

106106
if (DimThread.owns(Thread.currentThread())) {
107-
nbtCachedForMoveToWorld = writeNbt(new NbtCompound());
108-
uncompletedTeleportTargetForMoveToWorld = createTeleportTargetUncompleted(destination);
109-
destination.getServer().execute(
110-
() -> {
111-
Entity entity = this.moveToWorld(destination);
112-
if(entity == null) {
113-
this.unsetRemoved();
114-
nbtCachedForMoveToWorld.putInt("PortalCooldown", this.netherPortalCooldown);
115-
this.readNbt(nbtCachedForMoveToWorld);
116-
this.uncompletedTeleportTargetForMoveToWorld = null;
117-
this.nbtCachedForMoveToWorld = null;
118-
this.world.spawnEntity((Entity) (Object) this);// if the teleporting failed, we need to add it back to the world
119-
LOGGER.debug("Failed to teleport {}, return it to its previous world", this);
107+
if (nbtCachedForMoveToWorld == null) { // Some entity may invoke moveToWorld many times, which may cause https://github.com/CaveNightingale/DimensionalThreading/issues/1
108+
nbtCachedForMoveToWorld = writeNbt(new NbtCompound());
109+
uncompletedTeleportTargetForMoveToWorld = createTeleportTargetUncompleted(destination);
110+
destination.getServer().execute(
111+
() -> {
112+
Entity entity = this.moveToWorld(destination);
113+
if (entity == null) {
114+
this.unsetRemoved();
115+
nbtCachedForMoveToWorld.putInt("PortalCooldown", this.netherPortalCooldown);
116+
this.readNbt(nbtCachedForMoveToWorld);
117+
this.uncompletedTeleportTargetForMoveToWorld = null;
118+
this.nbtCachedForMoveToWorld = null;
119+
this.world.spawnEntity((Entity) (Object) this);// if the teleporting failed, we need to add it back to the world
120+
LOGGER.debug("Failed to teleport {}, return it to its previous world", this);
121+
}
120122
}
121-
}
122-
);
123-
this.removeFromDimension();
123+
);
124+
this.removeFromDimension();
125+
}
124126
ci.setReturnValue(null);
125127
}
126128
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package wearblackallday.dimthread.mixin;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import net.minecraft.server.command.ServerCommandSource;
5+
import net.minecraft.server.dedicated.command.PerfCommand;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
11+
@Mixin(PerfCommand.class)
12+
public abstract class PerfCommandMixin {
13+
/**
14+
* Remove /perf because it's not thread-safe
15+
*/
16+
@Inject(method = "register", at = @At("HEAD"), cancellable = true)
17+
private static void removePerfCommand(CommandDispatcher<ServerCommandSource> dispatcher, CallbackInfo ci) {
18+
ci.cancel();
19+
}
20+
}

src/main/resources/dimthread.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"EntityMixin",
88
"MinecraftDedicatedServerMixin",
99
"MinecraftServerMixin",
10+
"PerfCommandMixin",
1011
"RedstoneWireBlockMixin",
1112
"ServerChunkManagerMixin",
1213
"ServerWorldMixin",

0 commit comments

Comments
 (0)