|
11 | 11 | import net.minecraft.world.World; |
12 | 12 | import net.minecraft.world.WorldServer; |
13 | 13 | import net.minecraftforge.common.util.FakePlayer; |
14 | | -import net.minecraftforge.common.util.FakePlayerFactory; |
15 | 14 | import net.minecraftforge.common.util.ITeleporter; |
16 | 15 | import net.minecraftforge.fml.common.FMLCommonHandler; |
17 | 16 |
|
18 | 17 | import com.mojang.authlib.GameProfile; |
| 18 | +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.jetbrains.annotations.Nullable; |
19 | 21 |
|
20 | 22 | import java.lang.ref.WeakReference; |
| 23 | +import java.util.Map; |
21 | 24 | import java.util.UUID; |
22 | 25 |
|
23 | 26 | public class GregFakePlayer extends EntityPlayer { |
24 | 27 |
|
25 | | - private static final GameProfile GREGTECH = new GameProfile(UUID.fromString("518FDF18-EC2A-4322-832A-58ED1721309B"), |
26 | | - "[GregTech]"); |
27 | | - private static WeakReference<FakePlayer> GREGTECH_PLAYER = null; |
| 28 | + private static final String FAKE_NAME = "[GregTech]"; |
| 29 | + private static final UUID FAKE_UUID = UUID.fromString("518FDF18-EC2A-4322-832A-58ED1721309B"); |
| 30 | + private static final GameProfile FAKE_PROFILE = new GameProfile(FAKE_UUID, FAKE_NAME); |
| 31 | + private static final Map<UUID, GameProfile> PROFILE_CACHE = new Object2ObjectOpenHashMap<>(); |
| 32 | + private static final Map<UUID, WeakReference<FakePlayer>> GREGTECH_PLAYERS = new Object2ObjectOpenHashMap<>(); |
28 | 33 |
|
29 | | - public static FakePlayer get(WorldServer world) { |
30 | | - FakePlayer ret = GREGTECH_PLAYER != null ? GREGTECH_PLAYER.get() : null; |
31 | | - if (ret == null) { |
32 | | - ret = FakePlayerFactory.get(world, GREGTECH); |
33 | | - GREGTECH_PLAYER = new WeakReference<>(ret); |
| 34 | + public static @NotNull FakePlayer get(@NotNull WorldServer world) { |
| 35 | + return get(world, FAKE_PROFILE); |
| 36 | + } |
| 37 | + |
| 38 | + public static @NotNull FakePlayer get(@NotNull WorldServer world, @Nullable UUID fakePlayerUUID) { |
| 39 | + return get(world, fakePlayerUUID == null ? FAKE_PROFILE : |
| 40 | + PROFILE_CACHE.computeIfAbsent(fakePlayerUUID, id -> new GameProfile(id, FAKE_NAME))); |
| 41 | + } |
| 42 | + |
| 43 | + public static @NotNull FakePlayer get(@NotNull WorldServer world, @NotNull GameProfile fakePlayerProfile) { |
| 44 | + UUID id = fakePlayerProfile.getId(); |
| 45 | + if (GREGTECH_PLAYERS.containsKey(id)) { |
| 46 | + FakePlayer fakePlayer = GREGTECH_PLAYERS.get(id).get(); |
| 47 | + if (fakePlayer != null) { |
| 48 | + return fakePlayer; |
| 49 | + } |
34 | 50 | } |
35 | | - return ret; |
| 51 | + |
| 52 | + FakePlayer fakePlayer = new FakePlayer(world, fakePlayerProfile); |
| 53 | + GREGTECH_PLAYERS.put(id, new WeakReference<>(fakePlayer)); |
| 54 | + return fakePlayer; |
36 | 55 | } |
37 | 56 |
|
38 | | - public GregFakePlayer(World worldIn) { |
39 | | - super(worldIn, GREGTECH); |
| 57 | + public GregFakePlayer(@NotNull World worldIn) { |
| 58 | + super(worldIn, FAKE_PROFILE); |
40 | 59 | } |
41 | 60 |
|
42 | 61 | @Override |
|
0 commit comments