Skip to content

Commit

Permalink
Fixes #1958
Browse files Browse the repository at this point in the history
  • Loading branch information
senseiwells authored and gnembon committed Nov 13, 2024
1 parent a178129 commit a83d04e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/carpet/commands/PlayerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ private static boolean cantSpawn(CommandContext<CommandSourceStack> context)
MinecraftServer server = context.getSource().getServer();
PlayerList manager = server.getPlayerList();

if (EntityPlayerMPFake.isSpawningPlayer(playerName))
{
Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is currently logging on");
return true;
}
if (manager.getPlayerByName(playerName) != null)
{
Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is already logged on");
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/carpet/patches/EntityPlayerMPFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
import carpet.fakes.ServerPlayerInterface;
import carpet.utils.Messenger;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

@SuppressWarnings("EntityConstructor")
public class EntityPlayerMPFake extends ServerPlayer
{
private static final Set<String> spawning = new HashSet<>();

public Runnable fixStartingPosition = () -> {};
public boolean isAShadow;

Expand All @@ -69,7 +72,21 @@ public static boolean createFake(String username, MinecraftServer server, Vec3 p
}
}
GameProfile finalGP = gameprofile;
fetchGameProfile(gameprofile.getName()).thenAcceptAsync(p -> {

// We need to mark this player as spawning so that we do not
// try to spawn another player with the name while the profile
// is being fetched - preventing multiple players spawning
String name = gameprofile.getName();
spawning.add(name);

fetchGameProfile(name).whenCompleteAsync((p, t) -> {
// Always remove the name, even if exception occurs
spawning.remove(name);
if (t != null)
{
return;
}

GameProfile current = finalGP;
if (p.isPresent())
{
Expand Down Expand Up @@ -127,6 +144,11 @@ public static EntityPlayerMPFake respawnFake(MinecraftServer server, ServerLevel
return new EntityPlayerMPFake(server, level, profile, cli, false);
}

public static boolean isSpawningPlayer(String username)
{
return spawning.contains(username);
}

private EntityPlayerMPFake(MinecraftServer server, ServerLevel worldIn, GameProfile profile, ClientInformation cli, boolean shadow)
{
super(server, worldIn, profile, cli);
Expand Down

0 comments on commit a83d04e

Please sign in to comment.