Skip to content

Commit

Permalink
Player info screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jul 22, 2024
1 parent ef35429 commit f570920
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 7 deletions.
268 changes: 268 additions & 0 deletions src/main/java/io/github/gaming32/worldhost/GameProfileRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
package io.github.gaming32.worldhost;

import com.google.common.base.Suppliers;
import com.mojang.authlib.GameProfile;
import net.minecraft.Util;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.player.RemotePlayer;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.commands.Commands;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.server.packs.repository.ServerPacksSource;
import net.minecraft.world.Difficulty;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

//#if MC >= 1.20.0
import net.minecraft.client.gui.GuiGraphics;
//#else
//$$ import com.mojang.blaze3d.vertex.PoseStack;
//#endif

//#if MC >= 1.19.2
import net.minecraft.client.gui.screens.worldselection.WorldCreationContext;
import net.minecraft.server.WorldLoader;
import net.minecraft.world.level.dimension.BuiltinDimensionTypes;
import net.minecraft.world.level.levelgen.presets.WorldPresets;
//#else
//$$ import net.minecraft.world.level.dimension.DimensionType;
//#endif

//#if MC >= 1.19.4
import net.minecraft.client.telemetry.TelemetryEventSender;
import net.minecraft.client.telemetry.WorldSessionTelemetryManager;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.WorldDataConfiguration;
import net.minecraft.world.level.levelgen.WorldGenSettings;
import net.minecraft.world.level.levelgen.WorldOptions;
//#else
//$$ import com.mojang.datafixers.util.Pair;
//$$ import com.mojang.serialization.Lifecycle;
//$$ import net.minecraft.core.Registry;
//$$ import net.minecraft.core.RegistryAccess;
//$$ import net.minecraft.server.packs.PackType;
//$$ import net.minecraft.world.level.DataPackConfig;
//#endif

//#if MC >= 1.20.4
import net.minecraft.client.multiplayer.CommonListenerCookie;
import net.minecraft.world.flag.FeatureFlags;
//#endif

//#if MC >= 1.20.6
import java.util.Map;
//#endif

//#if MC >= 1.21
import java.util.List;
import net.minecraft.server.ServerLinks;
//#endif

//#if NEOFORGE
//$$ import net.neoforged.neoforge.network.connection.ConnectionType;
//#endif

public final class GameProfileRenderer {
private static final Supplier<ClientLevel> FAKE_LEVEL = Suppliers.memoize(GameProfileRenderer::createFakeLevel);

private final Player player;

private GameProfileRenderer(RemotePlayer player) {
this.player = player;
}

public static GameProfileRenderer create(GameProfile profile) {
final RemotePlayer player = new RemotePlayer(
FAKE_LEVEL.get(), profile
//#if MC == 1.19.2
//$$ , null
//#endif
);
player.setPos(0, 80, 0);
return new GameProfileRenderer(player);
}

public void renderFacingMouse(
//#if MC < 1.20.0
//$$ PoseStack context,
//#else
GuiGraphics context,
//#endif
int x1, int y1, int x2, int y2, int scale, float mouseX, float mouseY
) {
final EntityRenderDispatcher entityRenderDispatcher = Minecraft.getInstance().getEntityRenderDispatcher();
final Camera oldCamera = entityRenderDispatcher.camera;
entityRenderDispatcher.camera = new Camera();
//#if MC >= 1.19.4
context.pose().pushPose();
context.pose().translate(0f, 0f, 1000f);
InventoryScreen.renderEntityInInventoryFollowsMouse(
context, x1, y1, x2, y2, scale, 0.0625f, mouseX, mouseY, player
);
context.pose().popPose();
//#else
//$$ final int centerX = (x1 + x2) / 2;
//$$ final int centerY = (y1 + y2) / 2;
//$$ InventoryScreen.renderEntityInInventory(
//$$ centerX, centerY, scale, centerX - mouseX, centerY - mouseY, player
//$$ );
//#endif
entityRenderDispatcher.camera = oldCamera;
}

private static ClientLevel createFakeLevel() {
final Minecraft minecraft = Minecraft.getInstance();
final GameProfile nullProfile = new GameProfile(Util.NIL_UUID, "");
//#if MC >= 1.19.2
final WorldCreationContext context = loadDatapacks(minecraft);
//#endif
//#if MC >= 1.19.4
final WorldSessionTelemetryManager worldTelemetry = new WorldSessionTelemetryManager(
TelemetryEventSender.DISABLED, false, null
//#if MC >= 1.20.1
, null
//#endif
);
//#endif
return new ClientLevel(
new ClientPacketListener(
minecraft,
//#if MC <= 1.20.1
//$$ null,
//#endif
new Connection(PacketFlow.CLIENTBOUND),
//#if MC >= 1.20.4
new CommonListenerCookie(
nullProfile, worldTelemetry,
context.worldgenLoadContext(),
FeatureFlags.REGISTRY.allFlags(),
null, null, null
//#if MC >= 1.20.6
, Map.of(), null, false
//#endif
//#if MC >= 1.21
, Map.of(), new ServerLinks(List.of())
//#endif
//#if NEOFORGE
//$$ , ConnectionType.OTHER
//#endif
)
//#else
//#if MC >= 1.19.4
//$$ null,
//#endif
//$$ nullProfile,
//#if MC >= 1.19.4
//$$ worldTelemetry
//#else
//$$ minecraft.createTelemetryManager()
//#endif
//#endif
),
new ClientLevel.ClientLevelData(Difficulty.NORMAL, false, false),
Level.OVERWORLD,
//#if MC >= 1.19.4
context.worldgenLoadContext()
.registryOrThrow(Registries.DIMENSION_TYPE)
.getHolderOrThrow(BuiltinDimensionTypes.OVERWORLD),
//#elseif MC >= 1.19.2
//$$ context.registryAccess()
//$$ .registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY)
//$$ .getHolderOrThrow(BuiltinDimensionTypes.OVERWORLD),
//#else
//$$ RegistryAccess.BUILTIN.get()
//$$ .registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY)
//$$ .getOrCreateHolder(DimensionType.OVERWORLD_LOCATION),
//#endif
//#if MC >= 1.19.2
minecraft.options.renderDistance().get(),
minecraft.options.simulationDistance().get(),
//#else
//$$ minecraft.options.renderDistance,
//$$ minecraft.options.simulationDistance,
//#endif
minecraft::getProfiler, minecraft.levelRenderer, false, 0L
);
}

//#if MC >= 1.19.2
private static WorldCreationContext loadDatapacks(Minecraft minecraft) {
//#if MC >= 1.20.4
final ServerPacksSource packsSource = new ServerPacksSource(minecraft.directoryValidator());
//#else
//$$ final ServerPacksSource packsSource = new ServerPacksSource();
//#endif
final PackRepository packRepository = new PackRepository(
//#if MC < 1.19.4
//$$ PackType.SERVER_DATA,
//#endif
packsSource
);
final WorldLoader.InitConfig initConfig = new WorldLoader.InitConfig(
new WorldLoader.PackConfig(
packRepository,
//#if MC >= 1.19.4
WorldDataConfiguration.DEFAULT,
//#else
//$$ DataPackConfig.DEFAULT,
//#endif
false
//#if MC >= 1.19.4
, false
//#endif
),
Commands.CommandSelection.INTEGRATED, 2
);
//#if MC >= 1.19.4
record DataPackReloadCookie(WorldGenSettings worldGenSettings, WorldDataConfiguration dataConfiguration) {
}
//#endif
try (ExecutorService gameExecutor = Executors.newSingleThreadExecutor()) {
final CompletableFuture<WorldCreationContext> completableFuture = WorldLoader.load(
initConfig,
//#if MC >= 1.19.4
dataLoadContext -> new WorldLoader.DataLoadOutput<>(
new DataPackReloadCookie(
new WorldGenSettings(
WorldOptions.defaultWithRandomSeed(),
WorldPresets.createNormalWorldDimensions(dataLoadContext.datapackWorldgen())
),
dataLoadContext.dataConfiguration()
),
dataLoadContext.datapackDimensions()
),
//#else
//$$ (resourceManager, dataPackConfig) -> {
//$$ final var frozen = RegistryAccess.builtinCopy().freeze();
//$$ final var worldGenSettings = WorldPresets.createNormalWorldFromPreset(frozen);
//$$ return Pair.of(worldGenSettings, frozen);
//$$ },
//#endif
(closeableResourceManager, reloadableServerResources, layeredRegistryAccess, object) -> {
closeableResourceManager.close();
return new WorldCreationContext(
//#if MC >= 1.19.4
object.worldGenSettings, layeredRegistryAccess, reloadableServerResources, object.dataConfiguration
//#else
//$$ object, Lifecycle.stable(), layeredRegistryAccess, reloadableServerResources
//#endif
);
},
Util.backgroundExecutor(), gameExecutor
);
return completableFuture.join();
}
}
//#endif
}
10 changes: 7 additions & 3 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,9 @@ public static GameProfile fetchProfile(MinecraftSessionService sessionService, G
return fetchProfile(sessionService, profile.getId(), profile);
}

public static CompletableFuture<ProfileInfo> resolveProfileInfo(GameProfile profile) {
public static CompletableFuture<GameProfile> resolveGameProfile(GameProfile profile) {
if (profile.getId().version() != 4) {
return CompletableFuture.completedFuture(new GameProfileProfileInfo(profile));
return CompletableFuture.completedFuture(profile);
}
return CompletableFuture.supplyAsync(
() -> WorldHost.fetchProfile(Minecraft.getInstance().getMinecraftSessionService(), profile),
Expand All @@ -647,7 +647,11 @@ public static CompletableFuture<ProfileInfo> resolveProfileInfo(GameProfile prof
//#else
//$$ Util.ioPool()
//#endif
).thenApply(GameProfileProfileInfo::new);
);
}

public static CompletableFuture<ProfileInfo> resolveProfileInfo(GameProfile profile) {
return resolveGameProfile(profile).thenApply(GameProfileProfileInfo::new);
}

public static boolean isFriend(UUID user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class FriendsScreen extends ScreenWithInfoTexts {
public static final Component ADD_FRIEND_TEXT = Components.translatable("world-host.add_friend");

private final Screen parent;
private Button infoButton;
private Button removeButton;
private FriendsList list;

Expand Down Expand Up @@ -68,12 +69,16 @@ protected void init() {
.build()
);

addRenderableWidget(
button(Components.empty(), button -> {
infoButton = addRenderableWidget(
button(Components.translatable("world-host.friends.show_info"), button -> {
if (list.getSelected() != null) {
list.getSelected().friend.showFriendInfo(this);
}
}).width(152)
.pos(width / 2 - 154, height - 30)
.build()
);
infoButton.active = false;

removeButton = addRenderableWidget(
button(Components.translatable("world-host.friends.remove"), button -> {
Expand Down Expand Up @@ -138,6 +143,7 @@ public FriendsList() {
@Override
public void setSelected(@Nullable FriendsEntry entry) {
super.setSelected(entry);
infoButton.active = entry != null;
removeButton.active = entry != null;
}

Expand Down
Loading

0 comments on commit f570920

Please sign in to comment.