Extract nullable stream to new util class.

This commit is contained in:
Matthew Messinger 2018-09-04 19:21:58 -04:00
parent d664cabb1a
commit d8913ed245
2 changed files with 20 additions and 12 deletions

View file

@ -26,6 +26,7 @@ import com.voxelmodpack.hdskins.skins.ServerType;
import com.voxelmodpack.hdskins.skins.SkinServer; import com.voxelmodpack.hdskins.skins.SkinServer;
import com.voxelmodpack.hdskins.skins.ValhallaSkinServer; import com.voxelmodpack.hdskins.skins.ValhallaSkinServer;
import com.voxelmodpack.hdskins.util.CallableFutures; import com.voxelmodpack.hdskins.util.CallableFutures;
import com.voxelmodpack.hdskins.util.MoreStreams;
import com.voxelmodpack.hdskins.util.PlayerUtil; import com.voxelmodpack.hdskins.util.PlayerUtil;
import com.voxelmodpack.hdskins.util.ProfileTextureUtil; import com.voxelmodpack.hdskins.util.ProfileTextureUtil;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -277,18 +278,18 @@ public final class HDSkinManager implements IResourceManagerReloadListener {
Streams.concat(getNPCs(mc), getPlayers(mc)) Streams.concat(getNPCs(mc), getPlayers(mc))
// filter nulls // filter nulls
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(INetworkPlayerInfo.class::cast) .map(INetworkPlayerInfo.class::cast)
.distinct() .distinct()
// and clear skins // and clear skins
.forEach(INetworkPlayerInfo::reloadTextures); .forEach(INetworkPlayerInfo::reloadTextures);
} }
private Stream<NetworkPlayerInfo> getNPCs(Minecraft mc) { private Stream<NetworkPlayerInfo> getNPCs(Minecraft mc) {
return nullableStream(mc.world) return MoreStreams.ofNullable(mc.world)
.flatMap(w -> w.playerEntities.stream()) .flatMap(w -> w.playerEntities.stream())
.filter(AbstractClientPlayer.class::isInstance) .filter(AbstractClientPlayer.class::isInstance)
.map(AbstractClientPlayer.class::cast) .map(AbstractClientPlayer.class::cast)
@ -296,14 +297,10 @@ public final class HDSkinManager implements IResourceManagerReloadListener {
} }
private Stream<NetworkPlayerInfo> getPlayers(Minecraft mc) { private Stream<NetworkPlayerInfo> getPlayers(Minecraft mc) {
return nullableStream(mc.getConnection()) return MoreStreams.ofNullable(mc.getConnection())
.flatMap(a -> a.getPlayerInfoMap().stream()); .flatMap(a -> a.getPlayerInfoMap().stream());
} }
private static <T> Stream<T> nullableStream(@Nullable T t) {
return t == null ? Stream.empty() : Stream.of(t);
}
public void parseSkin(GameProfile profile, Type type, ResourceLocation resource, MinecraftProfileTexture texture) { public void parseSkin(GameProfile profile, Type type, ResourceLocation resource, MinecraftProfileTexture texture) {
CallableFutures.scheduleTask(() -> { CallableFutures.scheduleTask(() -> {

View file

@ -0,0 +1,11 @@
package com.voxelmodpack.hdskins.util;
import java.util.stream.Stream;
import javax.annotation.Nullable;
public class MoreStreams {
public static <T> Stream<T> ofNullable(@Nullable T t) {
return t == null ? Stream.empty() : Stream.of(t);
}
}