Skip to content

Commit

Permalink
WorldHost.friendWentOnline
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jul 16, 2024
1 parent eff68e8 commit 9873d1f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
64 changes: 40 additions & 24 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
import io.github.gaming32.worldhost.config.WorldHostConfig;
import io.github.gaming32.worldhost.gui.OnlineStatusLocation;
import io.github.gaming32.worldhost.gui.screen.JoiningWorldHostScreen;
import io.github.gaming32.worldhost.gui.screen.OnlineFriendsScreen;
import io.github.gaming32.worldhost.plugin.FriendAdder;
import io.github.gaming32.worldhost.plugin.InfoTextsCategory;
import io.github.gaming32.worldhost.plugin.OnlineFriend;
import io.github.gaming32.worldhost.plugin.ProfileInfo;
import io.github.gaming32.worldhost.plugin.WorldHostPlugin;
import io.github.gaming32.worldhost.plugin.vanilla.GameProfileProfileInfo;
import io.github.gaming32.worldhost.protocol.ProtocolClient;
import io.github.gaming32.worldhost.protocol.proxy.ProxyPassthrough;
import io.github.gaming32.worldhost.protocol.proxy.ProxyProtocolClient;
import io.github.gaming32.worldhost.proxy.ProxyClient;
import io.github.gaming32.worldhost.toast.IconRenderer;
import io.github.gaming32.worldhost.toast.WHToast;
import io.github.gaming32.worldhost.upnp.Gateway;
import io.github.gaming32.worldhost.upnp.GatewayFinder;
Expand Down Expand Up @@ -426,6 +428,17 @@ public static List<FriendAdder> getFriendAdders() {
.toList();
}

public static void friendWentOnline(OnlineFriend friend) {
ONLINE_FRIENDS.put(friend.uuid(), friend);
ONLINE_FRIEND_UPDATES.forEach(FriendsListUpdate::friendsListUpdate);
if (!CONFIG.isAnnounceFriendsOnline()) return;
if (Minecraft.getInstance().screen instanceof OnlineFriendsScreen) return;
showFriendOrOnlineToast(
friend.profileInfo(), "world-host.went_online", "world-host.went_online.desc", 200,
friend.unjoinableReason().isPresent() ? null : () -> friend.joinWorld(Minecraft.getInstance().screen)
);
}

public static void tickHandler() {
if (protoClient == null || protoClient.isClosed()) {
protoClient = null;
Expand Down Expand Up @@ -562,10 +575,6 @@ public static void shutdownClients() {
proxyProtocolClient = null;
}

public static String getName(GameProfile profile) {
return StringUtils.getIfBlank(profile.getName(), () -> profile.getId().toString());
}

public static GameProfileCache getProfileCache() {
return profileCache;
}
Expand Down Expand Up @@ -627,6 +636,20 @@ public static GameProfile fetchProfile(MinecraftSessionService sessionService, G
return fetchProfile(sessionService, profile.getId(), profile);
}

public static CompletableFuture<ProfileInfo> resolveProfileInfo(GameProfile profile) {
if (profile.getId().version() != 4) {
return CompletableFuture.completedFuture(new GameProfileProfileInfo(profile));
}
return CompletableFuture.supplyAsync(
() -> WorldHost.fetchProfile(Minecraft.getInstance().getMinecraftSessionService(), profile),
//#if MC >= 1.20.4
Util.nonCriticalIoPool()
//#else
//$$ Util.ioPool()
//#endif
).thenApply(GameProfileProfileInfo::new);
}

public static boolean isFriend(UUID user) {
return CONFIG.isEnableFriends() && CONFIG.getFriends().contains(user);
}
Expand All @@ -645,24 +668,21 @@ public static void addFriends(Collection<UUID> friends) {
}

public static void showFriendOrOnlineToast(
UUID user,
CompletableFuture<ProfileInfo> profileFuture,
@Translatable String title,
@Translatable String description,
int ticks,
Runnable clickAction
) {
Util.backgroundExecutor().execute(() -> {
final GameProfile profile = fetchProfile(Minecraft.getInstance().getMinecraftSessionService(), user);
getInsecureSkinLocation(profile).thenAcceptAsync(skinTexture -> {
WHToast.builder(Components.translatable(title, getName(profile)))
.description(Components.translatable(description))
.icon(IconRenderer.createSkinIconRenderer(skinTexture))
.clickAction(clickAction)
.ticks(ticks)
.important()
.show();
}, Minecraft.getInstance());
});
profileFuture.thenAccept(profile ->
WHToast.builder(Components.translatable(title, profile.name()))
.description(Components.translatable(description))
.icon(profile.iconRenderer())
.clickAction(clickAction)
.ticks(ticks)
.important()
.show()
);
}

public static FriendlyByteBuf createByteBuf() {
Expand Down Expand Up @@ -789,17 +809,13 @@ public static Long tryParseConnectionId(String str) {
return result;
}

public static void join(long connectionId, @Nullable Screen parentScreen) {
public static void join(long connectionId, Screen parentScreen) {
if (protoClient == null) {
LOGGER.error("Tried to join {}, but protoClient == null!", connectionIdToString(connectionId));
return;
}
final Minecraft minecraft = Minecraft.getInstance();
if (parentScreen == null) {
parentScreen = minecraft.screen;
}
protoClient.setAttemptingToJoin(connectionId);
minecraft.setScreen(new JoiningWorldHostScreen(parentScreen));
Minecraft.getInstance().setScreen(new JoiningWorldHostScreen(parentScreen));
protoClient.requestDirectJoin(connectionId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.plugin.Profilable;
import io.github.gaming32.worldhost.plugin.ProfileInfo;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;

import java.util.concurrent.CompletableFuture;

Expand All @@ -19,16 +17,6 @@ default ProfileInfo fallbackProfileInfo() {

@Override
default CompletableFuture<ProfileInfo> profileInfo() {
if (defaultProfile().getId().version() != 4) {
return CompletableFuture.completedFuture(fallbackProfileInfo());
}
return CompletableFuture.supplyAsync(
() -> WorldHost.fetchProfile(Minecraft.getInstance().getMinecraftSessionService(), defaultProfile()),
//#if MC >= 1.20.4
Util.nonCriticalIoPool()
//#else
//$$ Util.ioPool()
//#endif
).thenApply(GameProfileProfileInfo::new);
return WorldHost.resolveProfileInfo(defaultProfile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public GameProfileProfileInfo(GameProfile profile) {

@Override
public String name() {
return WorldHost.getName(profile);
return !profile.getName().isBlank()
? profile.getName()
: profile.getId().toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.gaming32.worldhost.protocol;

import com.mojang.authlib.GameProfile;
import io.github.gaming32.worldhost.FriendsListUpdate;
import io.github.gaming32.worldhost.SecurityLevel;
import io.github.gaming32.worldhost.WorldHost;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void handle(ProtocolClient client) {
}
if (!isFriend && !WorldHost.CONFIG.isAllowFriendRequests()) return;
WorldHost.showFriendOrOnlineToast(
fromUser,
WorldHost.resolveProfileInfo(new GameProfile(fromUser, "")),
isFriend ? "world-host.friend_added_you.already" : "world-host.friend_added_you",
isFriend ? "world-host.friend_added_you.already.desc" : "world-host.need_add_back",
isFriend ? 100 : 200,
Expand All @@ -133,16 +134,9 @@ record PublishedWorld(
@Override
public void handle(ProtocolClient client) {
if (!checkAndLogSecurity() || !WorldHost.isFriend(user)) return;
Minecraft.getInstance().execute(() -> {
WorldHost.ONLINE_FRIENDS.put(user, new WorldHostOnlineFriend(user, connectionId, security));
WorldHost.ONLINE_FRIEND_UPDATES.forEach(FriendsListUpdate::friendsListUpdate);
if (!WorldHost.CONFIG.isAnnounceFriendsOnline()) return;
if (Minecraft.getInstance().screen instanceof OnlineFriendsScreen) return;
WorldHost.showFriendOrOnlineToast(
user, "world-host.went_online", "world-host.went_online.desc", 200,
() -> WorldHost.join(connectionId, null)
);
});
Minecraft.getInstance().execute(() ->
WorldHost.friendWentOnline(new WorldHostOnlineFriend(user, connectionId, security))
);
}
}

Expand Down

0 comments on commit 9873d1f

Please sign in to comment.