mirror of
https://github.com/Suiranoil/SkinRestorer.git
synced 2026-01-16 04:42:12 +00:00
store exception in SkinResult
This commit is contained in:
@@ -25,7 +25,7 @@ public class MineskinSkinProvider {
|
||||
|
||||
return SkinResult.success(new Property("textures", texture.get("value").getAsString(), texture.get("signature").getAsString()));
|
||||
} catch (IOException e) {
|
||||
return SkinResult.error();
|
||||
return SkinResult.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class MojangSkinProvider {
|
||||
|
||||
return SkinResult.success(new Property("textures", texture.get("value").getAsString(), texture.get("signature").getAsString()));
|
||||
} catch (Exception e) {
|
||||
return SkinResult.error();
|
||||
return SkinResult.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -67,40 +67,41 @@ public class SkinRestorer implements DedicatedServerModInitializer {
|
||||
}
|
||||
|
||||
public static CompletableFuture<Pair<Collection<ServerPlayerEntity>, Collection<GameProfile>>> setSkinAsync(MinecraftServer server, Collection<GameProfile> targets, Supplier<SkinResult> skinSupplier) {
|
||||
return CompletableFuture.<Pair<Optional<Property>, Collection<GameProfile>>>supplyAsync(() -> {
|
||||
HashSet<GameProfile> acceptedProfiles = new HashSet<>();
|
||||
SkinResult result = skinSupplier.get();
|
||||
if (result.isError()) {
|
||||
SkinRestorer.LOGGER.error("Cannot get the skin for {}", targets.stream().findFirst().orElseThrow());
|
||||
return Pair.of(null, Collections.emptySet());
|
||||
}
|
||||
return CompletableFuture.<Pair<Property, Collection<GameProfile>>>supplyAsync(() -> {
|
||||
SkinResult result = skinSupplier.get();
|
||||
if (result.isError()) {
|
||||
SkinRestorer.LOGGER.error("Could not get skin", result.getError());
|
||||
return Pair.of(null, Collections.emptySet());
|
||||
}
|
||||
|
||||
Optional<Property> skin = result.getSkin();
|
||||
Property skin = result.getSkin();
|
||||
|
||||
for (GameProfile profile : targets) {
|
||||
SkinRestorer.getSkinStorage().setSkin(profile.getId(), skin.orElse(null));
|
||||
acceptedProfiles.add(profile);
|
||||
}
|
||||
for (GameProfile profile : targets)
|
||||
SkinRestorer.getSkinStorage().setSkin(profile.getId(), skin);
|
||||
|
||||
return Pair.of(skin, acceptedProfiles);
|
||||
}).<Pair<Collection<ServerPlayerEntity>, Collection<GameProfile>>>thenApplyAsync(pair -> {
|
||||
Property skin = pair.left().orElse(null);
|
||||
HashSet<GameProfile> acceptedProfiles = new HashSet<>(targets);
|
||||
|
||||
Collection<GameProfile> acceptedProfiles = pair.right();
|
||||
HashSet<ServerPlayerEntity> acceptedPlayers = new HashSet<>();
|
||||
return Pair.of(skin, acceptedProfiles);
|
||||
}).<Pair<Collection<ServerPlayerEntity>, Collection<GameProfile>>>thenApplyAsync(pair -> {
|
||||
Property skin = pair.left(); // NullPtrException will be caught by 'exceptionally'
|
||||
|
||||
for (GameProfile profile : acceptedProfiles) {
|
||||
ServerPlayerEntity player = server.getPlayerManager().getPlayer(profile.getId());
|
||||
Collection<GameProfile> acceptedProfiles = pair.right();
|
||||
HashSet<ServerPlayerEntity> acceptedPlayers = new HashSet<>();
|
||||
|
||||
if (player == null || areSkinPropertiesEquals(skin, getPlayerSkin(player)))
|
||||
continue;
|
||||
for (GameProfile profile : acceptedProfiles) {
|
||||
ServerPlayerEntity player = server.getPlayerManager().getPlayer(profile.getId());
|
||||
|
||||
applyRestoredSkin(player.getGameProfile(), skin);
|
||||
refreshPlayer(player);
|
||||
acceptedPlayers.add(player);
|
||||
}
|
||||
return Pair.of(acceptedPlayers, acceptedProfiles);
|
||||
}, server).orTimeout(10, TimeUnit.SECONDS).exceptionally(e -> Pair.of(Collections.emptySet(), Collections.emptySet()));
|
||||
if (player == null || areSkinPropertiesEquals(skin, getPlayerSkin(player)))
|
||||
continue;
|
||||
|
||||
applyRestoredSkin(player.getGameProfile(), skin);
|
||||
refreshPlayer(player);
|
||||
acceptedPlayers.add(player);
|
||||
}
|
||||
return Pair.of(acceptedPlayers, acceptedProfiles);
|
||||
}, server)
|
||||
.orTimeout(10, TimeUnit.SECONDS)
|
||||
.exceptionally(e -> Pair.of(Collections.emptySet(), Collections.emptySet()));
|
||||
}
|
||||
|
||||
public static void applyRestoredSkin(GameProfile profile, Property skin) {
|
||||
|
||||
@@ -7,31 +7,35 @@ import java.util.Optional;
|
||||
|
||||
public class SkinResult {
|
||||
private final Property skin;
|
||||
private final boolean isError;
|
||||
private final Exception exception;
|
||||
|
||||
private SkinResult(Property skin, boolean isError) {
|
||||
private SkinResult(Property skin, Exception exception) {
|
||||
this.skin = skin;
|
||||
this.isError = isError;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public Optional<Property> getSkin() {
|
||||
return Optional.ofNullable(this.skin);
|
||||
public Property getSkin() {
|
||||
return this.skin;
|
||||
}
|
||||
|
||||
public Exception getError() {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
public boolean isError() {
|
||||
return this.isError;
|
||||
return this.exception != null;
|
||||
}
|
||||
|
||||
public static SkinResult empty() {
|
||||
return new SkinResult(null, false);
|
||||
return new SkinResult(null, null);
|
||||
}
|
||||
|
||||
public static SkinResult error() {
|
||||
return new SkinResult(null, true);
|
||||
public static SkinResult error(Exception e) {
|
||||
return new SkinResult(null, e);
|
||||
}
|
||||
|
||||
public static SkinResult success(@NotNull Property skin) {
|
||||
return new SkinResult(skin, false);
|
||||
return new SkinResult(skin, null);
|
||||
}
|
||||
|
||||
public static SkinResult ofNullable(Property skin) {
|
||||
|
||||
@@ -40,7 +40,6 @@ public abstract class PlayerManagerMixin {
|
||||
|
||||
@Inject(method = "onPlayerConnect", at = @At("HEAD"))
|
||||
private void onPlayerConnected(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) {
|
||||
if (player.getClass() != ServerPlayerEntity.class) // if the player isn't a server player entity, it must be someone's fake player
|
||||
SkinRestorer.setSkinAsync(server, Collections.singleton(player.getGameProfile()), () -> SkinResult.ofNullable(SkinRestorer.getSkinStorage().getSkin(player.getUuid())));
|
||||
SkinRestorer.setSkinAsync(server, Collections.singleton(player.getGameProfile()), () -> SkinResult.ofNullable(SkinRestorer.getSkinStorage().getSkin(player.getUuid())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public abstract class ServerLoginNetworkHandlerMixin {
|
||||
if (!SkinRestorer.getSkinStorage().hasSavedSkin(profile.getId())) { // when player joins for the first time fetch Mojang skin by his username
|
||||
SkinResult result = MojangSkinProvider.getSkin(profile.getName());
|
||||
if (!result.isError())
|
||||
SkinRestorer.getSkinStorage().setSkin(profile.getId(), result.getSkin().orElse(null));
|
||||
SkinRestorer.getSkinStorage().setSkin(profile.getId(), result.getSkin());
|
||||
}
|
||||
|
||||
return SkinResult.ofNullable(SkinRestorer.getSkinStorage().getSkin(profile.getId()));
|
||||
@@ -44,10 +44,4 @@ public abstract class ServerLoginNetworkHandlerMixin {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "sendSuccessPacket", at = @At("HEAD"))
|
||||
public void applyRestoredSkinHook(GameProfile profile, CallbackInfo ci) {
|
||||
if (skinrestorer_pendingSkin != null)
|
||||
SkinRestorer.applyRestoredSkin(profile, skinrestorer_pendingSkin.getNow(SkinResult.empty()).getSkin().orElse(null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package net.lionarius.skinrestorer.util;
|
||||
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
public class PlayerUtils {
|
||||
|
||||
public static boolean isFakePlayer(ServerPlayerEntity player) {
|
||||
return player.getClass() != ServerPlayerEntity.class; // if the player isn't a server player entity, it must be someone's fake player
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user