Skip to content

Commit 2752a4b

Browse files
committed
Introduce a PlayerInfo class with the uuid and player name
1 parent 84bdd86 commit 2752a4b

File tree

5 files changed

+83
-29
lines changed

5 files changed

+83
-29
lines changed

common/src/main/java/io/github/communityradargg/forgemod/util/CommonHandler.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.time.LocalDateTime;
1717
import java.time.format.DateTimeFormatter;
1818
import java.util.HashMap;
19+
import java.util.List;
1920
import java.util.Locale;
2021
import java.util.Map;
2122
import java.util.Optional;
@@ -151,10 +152,11 @@ public void setOnGrieferGames(final boolean isLocal, final @Nullable SocketAddre
151152
}
152153

153154
// Checking if there is a player with same name in the loaded world. If so, returning UUID from EntityPlayer.
154-
final Optional<UUID> playerUuid = versionBridge.getPlayerUuidByNameFromWorld(playerName);
155-
if (playerUuid.isPresent()) {
156-
UUID_NAME_CACHE.put(playerName, playerUuid.get());
157-
return CompletableFuture.completedFuture(playerUuid);
155+
for (final PlayerInfo playerInfo : versionBridge.getWorldPlayers()) {
156+
if (playerInfo.getPlayerName().equalsIgnoreCase(playerName) && playerInfo.getUuid() != null) {
157+
UUID_NAME_CACHE.put(playerName, playerInfo.getUuid());
158+
return CompletableFuture.completedFuture(Optional.of(playerInfo.getUuid()));
159+
}
158160
}
159161

160162
if (playerName.startsWith("!") || playerName.startsWith("~")) {
@@ -228,6 +230,15 @@ public void addMessageToChat(final @NotNull String message) {
228230
versionBridge.addMessageToChat(message);
229231
}
230232

233+
/**
234+
* Gets the player info data for all players in the current world.
235+
*
236+
* @return Returns the player info data.
237+
*/
238+
public @NotNull List<@NotNull PlayerInfo> getWorldPlayers() {
239+
return versionBridge.getWorldPlayers();
240+
}
241+
231242
/**
232243
* Gets the list manager.
233244
*
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.github.communityradargg.forgemod.util;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
import java.util.UUID;
6+
7+
/**
8+
* Holds the general player info.
9+
*/
10+
public class PlayerInfo {
11+
private final UUID uuid;
12+
private final String playerName;
13+
14+
/**
15+
* Constructs a {@link PlayerInfo}.
16+
*
17+
* @param uuid The uuid.
18+
* @param playerName The player name.
19+
*/
20+
public PlayerInfo(final @Nullable UUID uuid, final @NotNull String playerName) {
21+
this.uuid = uuid;
22+
this.playerName = playerName;
23+
}
24+
25+
/**
26+
* Gets the uuid.
27+
*
28+
* @return Returns the uuid.
29+
*/
30+
public @Nullable UUID getUuid() {
31+
return uuid;
32+
}
33+
34+
/**
35+
* Gets the player name.
36+
*
37+
* @return Returns the player name.
38+
*/
39+
public @NotNull String getPlayerName() {
40+
return playerName;
41+
}
42+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.github.communityradargg.forgemod.util;
22

33
import org.jetbrains.annotations.NotNull;
4-
import java.util.Optional;
5-
import java.util.UUID;
4+
import java.util.List;
65

76
public interface VersionBridge {
87
@NotNull String getVersion();
@@ -11,5 +10,5 @@ public interface VersionBridge {
1110

1211
boolean isNotInWorld();
1312

14-
Optional<UUID> getPlayerUuidByNameFromWorld(final @NotNull String playerName);
13+
@NotNull List<@NotNull PlayerInfo> getWorldPlayers();
1514
}

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/util/VersionBridgeImpl.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package io.github.communityradargg.forgemod.util;
22

3+
import com.mojang.authlib.GameProfile;
34
import net.minecraft.client.Minecraft;
45
import net.minecraft.client.network.NetHandlerPlayClient;
5-
import net.minecraft.client.network.NetworkPlayerInfo;
66
import net.minecraft.util.text.TextComponentString;
77
import net.minecraftforge.fml.common.Loader;
88
import net.minecraftforge.fml.common.ModContainer;
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111
import org.jetbrains.annotations.NotNull;
12-
import java.util.Optional;
13-
import java.util.UUID;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
1415

1516
public class VersionBridgeImpl implements VersionBridge {
1617
private static final Logger LOGGER = LogManager.getLogger(VersionBridgeImpl.class);
@@ -41,17 +42,17 @@ public boolean isNotInWorld() {
4142
}
4243

4344
@Override
44-
public Optional<UUID> getPlayerUuidByNameFromWorld(final @NotNull String playerName) {
45+
public @NotNull List<@NotNull PlayerInfo> getWorldPlayers() {
4546
final NetHandlerPlayClient connection = Minecraft.getMinecraft().getConnection();
4647
if (connection == null) {
47-
return Optional.empty();
48+
return Collections.emptyList();
4849
}
4950

50-
for (final NetworkPlayerInfo networkPlayerInfo : connection.getPlayerInfoMap()) {
51-
if (networkPlayerInfo.getGameProfile().getName().equalsIgnoreCase(playerName)) {
52-
return Optional.of(networkPlayerInfo.getGameProfile().getId());
53-
}
54-
}
55-
return Optional.empty();
51+
return connection.getPlayerInfoMap().stream()
52+
.map(networkPlayerInfo -> {
53+
final GameProfile gameProfile = networkPlayerInfo.getGameProfile();
54+
return new PlayerInfo(gameProfile.getId(), gameProfile.getName());
55+
})
56+
.collect(Collectors.toList());
5657
}
5758
}

versions/1.8.9/src/main/java/io/github/communityradargg/forgemod/util/VersionBridgeImpl.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package io.github.communityradargg.forgemod.util;
22

3+
import com.mojang.authlib.GameProfile;
34
import net.minecraft.client.Minecraft;
45
import net.minecraft.client.network.NetHandlerPlayClient;
5-
import net.minecraft.client.network.NetworkPlayerInfo;
66
import net.minecraft.util.ChatComponentText;
77
import net.minecraftforge.fml.common.Loader;
88
import net.minecraftforge.fml.common.ModContainer;
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111
import org.jetbrains.annotations.NotNull;
12-
import java.util.Optional;
13-
import java.util.UUID;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
1415

1516
public class VersionBridgeImpl implements VersionBridge {
1617
private static final Logger LOGGER = LogManager.getLogger(VersionBridgeImpl.class);
@@ -41,17 +42,17 @@ public boolean isNotInWorld() {
4142
}
4243

4344
@Override
44-
public Optional<UUID> getPlayerUuidByNameFromWorld(final @NotNull String playerName) {
45+
public @NotNull List<@NotNull PlayerInfo> getWorldPlayers() {
4546
final NetHandlerPlayClient connection = Minecraft.getMinecraft().getNetHandler();
4647
if (connection == null) {
47-
return Optional.empty();
48+
return Collections.emptyList();
4849
}
4950

50-
for (final NetworkPlayerInfo networkPlayerInfo : connection.getPlayerInfoMap()) {
51-
if (networkPlayerInfo.getGameProfile().getName().equalsIgnoreCase(playerName)) {
52-
return Optional.of(networkPlayerInfo.getGameProfile().getId());
53-
}
54-
}
55-
return Optional.empty();
51+
return connection.getPlayerInfoMap().stream()
52+
.map(networkPlayerInfo -> {
53+
final GameProfile gameProfile = networkPlayerInfo.getGameProfile();
54+
return new PlayerInfo(gameProfile.getId(), gameProfile.getName());
55+
})
56+
.collect(Collectors.toList());
5657
}
5758
}

0 commit comments

Comments
 (0)