Skip to content

Commit b224b2c

Browse files
committed
Fix thread-safety issues with /rtp
1 parent f52bb5b commit b224b2c

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

src/main/java/com/fibermc/essentialcommands/commands/RandomTeleportCommand.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fibermc.essentialcommands.commands;
22

33
import java.util.Random;
4+
import java.util.concurrent.CompletableFuture;
45

56
import com.fibermc.essentialcommands.EssentialCommands;
67
import com.fibermc.essentialcommands.ManagerLocator;
@@ -19,6 +20,7 @@
1920
import net.minecraft.block.BlockState;
2021
import net.minecraft.block.Material;
2122
import net.minecraft.command.CommandException;
23+
import net.minecraft.server.MinecraftServer;
2224
import net.minecraft.server.command.ServerCommandSource;
2325
import net.minecraft.server.network.ServerPlayerEntity;
2426
import net.minecraft.server.world.ServerWorld;
@@ -78,15 +80,14 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
7880
playerData.setTimeUsedRtp(curServerTickTime);
7981
}
8082

81-
new Thread("RTP Location Calculator Thread") {
82-
public void run() {
83-
try {
84-
exec(context.getSource(), world);
85-
} catch (CommandSyntaxException e) {
86-
e.printStackTrace();
87-
}
83+
CompletableFuture.runAsync(() -> {
84+
try {
85+
exec(context.getSource(), world);
86+
} catch (CommandSyntaxException e) {
87+
e.printStackTrace();
8888
}
89-
}.start();
89+
});
90+
9091
return 1;
9192
}
9293

@@ -156,11 +157,11 @@ private static int exec(ServerPlayerEntity player, ServerWorld world, MinecraftL
156157
}
157158

158159
// Teleport the player
159-
PlayerTeleporter.requestTeleport(
160+
world.getServer().executeSync(() -> PlayerTeleporter.requestTeleport(
160161
player,
161162
new MinecraftLocation(world.getRegistryKey(), new_x, new_y, new_z, 0, 0),
162163
ecText.getText("cmd.rtp.location_name")
163-
);
164+
));
164165

165166
return 1;
166167
}

0 commit comments

Comments
 (0)