Skip to content

Commit

Permalink
Fix thread-safety issues with /rtp
Browse files Browse the repository at this point in the history
  • Loading branch information
anhcraft committed Feb 26, 2023
1 parent f52bb5b commit b224b2c
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fibermc.essentialcommands.commands;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

import com.fibermc.essentialcommands.EssentialCommands;
import com.fibermc.essentialcommands.ManagerLocator;
Expand All @@ -19,6 +20,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.command.CommandException;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
Expand Down Expand Up @@ -78,15 +80,14 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
playerData.setTimeUsedRtp(curServerTickTime);
}

new Thread("RTP Location Calculator Thread") {
public void run() {
try {
exec(context.getSource(), world);
} catch (CommandSyntaxException e) {
e.printStackTrace();
}
CompletableFuture.runAsync(() -> {
try {
exec(context.getSource(), world);
} catch (CommandSyntaxException e) {
e.printStackTrace();
}
}.start();
});

return 1;
}

Expand Down Expand Up @@ -156,11 +157,11 @@ private static int exec(ServerPlayerEntity player, ServerWorld world, MinecraftL
}

// Teleport the player
PlayerTeleporter.requestTeleport(
world.getServer().executeSync(() -> PlayerTeleporter.requestTeleport(
player,
new MinecraftLocation(world.getRegistryKey(), new_x, new_y, new_z, 0, 0),
ecText.getText("cmd.rtp.location_name")
);
));

return 1;
}
Expand Down

0 comments on commit b224b2c

Please sign in to comment.