From 5607298898b44ed5f1a7e2b1b8fbfcffe4261e1f Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Wed, 7 Feb 2018 21:01:09 +0800 Subject: [PATCH] reduce 'return null' --- .../jackhuang/hmcl/game/AccountHelper.java | 7 +++-- .../hmcl/game/HMCLGameRepository.java | 5 +++ .../hmcl/game/HMCLModpackExportTask.java | 5 ++- .../jackhuang/hmcl/game/LauncherHelper.java | 2 +- .../org/jackhuang/hmcl/setting/Accounts.java | 18 +++++++---- .../org/jackhuang/hmcl/setting/Profile.java | 5 +++ .../org/jackhuang/hmcl/setting/Settings.java | 4 +-- .../hmcl/setting/VersionSetting.java | 2 +- .../jackhuang/hmcl/ui/DialogController.java | 3 +- .../hmcl/ui/InstallerController.java | 7 +++-- .../java/org/jackhuang/hmcl/ui/MainPage.java | 2 +- .../hmcl/auth/yggdrasil/YggdrasilAccount.java | 22 +++++++------ .../hmcl/download/forge/ForgeVersionList.java | 11 +++---- .../hmcl/download/game/GameVersionList.java | 7 +++-- .../liteloader/LiteLoaderInstallTask.java | 5 +-- .../liteloader/LiteLoaderVersionList.java | 9 +++--- .../optifine/OptiFineBMCLVersionList.java | 4 +-- .../optifine/OptiFineInstallTask.java | 5 +-- .../hmcl/game/CompatibilityRule.java | 21 ++++++------- .../org/jackhuang/hmcl/game/GameVersion.java | 31 ++++++++++--------- .../org/jackhuang/hmcl/mod/CurseManifest.java | 2 +- .../mod/MultiMCInstanceConfiguration.java | 11 ++++--- .../jackhuang/hmcl/util/CompressingUtils.java | 7 +++-- .../org/jackhuang/hmcl/util/StringUtils.java | 10 ------ .../jackhuang/hmcl/util/VersionNumber.java | 11 ++++--- 25 files changed, 111 insertions(+), 105 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/AccountHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/AccountHelper.java index 90ddd2b68..f22294d41 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/AccountHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/AccountHelper.java @@ -39,6 +39,7 @@ import java.net.Proxy; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Optional; public final class AccountHelper { public static final AccountHelper INSTANCE = new AccountHelper(); @@ -155,9 +156,9 @@ public final class AccountHelper { if (profile == null) return; String name = profile.getName(); if (name == null) return; - ProfileTexture texture = account.getSkin(profile); - if (texture == null) return; - String url = texture.getUrl(); + Optional texture = account.getSkin(profile); + if (!texture.isPresent()) return; + String url = texture.get().getUrl(); File file = getSkinFile(name); if (!refresh && file.exists()) return; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java index 4bf9b01db..9887f8e95 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java @@ -135,6 +135,11 @@ public class HMCLGameRepository extends DefaultGameRepository { } } + /** + * Create new version setting if version id has no version setting. + * @param id the version id. + * @return new version setting, null if given version does not exist. + */ public VersionSetting createVersionSetting(String id) { if (!hasVersion(id)) return null; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLModpackExportTask.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLModpackExportTask.java index b2d241cb1..8d88f0c23 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLModpackExportTask.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLModpackExportTask.java @@ -86,9 +86,8 @@ public class HMCLModpackExportTask extends TaskResult { }); Version mv = repository.getVersion(version).resolve(repository); - String gameVersion = GameVersion.minecraftVersion(repository.getVersionJar(version)); - if (gameVersion == null) - throw new IllegalStateException("Cannot parse the version of " + version); + String gameVersion = GameVersion.minecraftVersion(repository.getVersionJar(version)) + .orElseThrow(() -> new IllegalStateException("Cannot parse the version of " + version)); zip.putTextFile(Constants.GSON.toJson(mv.setJar(gameVersion)), "minecraft/pack.json"); // Making "jar" to gameVersion is to be compatible with old HMCL. zip.putTextFile(Constants.GSON.toJson(modpack.setGameVersion(gameVersion)), "modpack.json"); // Newer HMCL only reads 'gameVersion' field. } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java index c01040128..f80f706c1 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java @@ -156,7 +156,7 @@ public final class LauncherHelper { private static void checkGameState(Profile profile, VersionSetting setting, Version version, Runnable onAccept) throws InterruptedException { boolean flag = false, suggest = true; - VersionNumber gameVersion = VersionNumber.asVersion(GameVersion.minecraftVersion(profile.getRepository().getVersionJar(version))); + VersionNumber gameVersion = VersionNumber.asVersion(GameVersion.minecraftVersion(profile.getRepository().getVersionJar(version)).orElse("Unknown")); JavaVersion java = setting.getJavaVersion(); if (java == null) { Controllers.dialog(Main.i18n("launch.wrong_javadir"), Main.i18n("message.error"), MessageBox.ERROR_MESSAGE, onAccept); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java index 35c249172..4b342137e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java @@ -27,6 +27,7 @@ import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Pair; import java.util.Map; +import java.util.Optional; /** * @author huangyuhui @@ -52,14 +53,19 @@ public final class Accounts { account.getProperties().put("character", character); } - public static String getCurrentCharacter(Account account) { - return Lang.get(account.getProperties(), "character", String.class, null); + public static boolean hasCurrentCharacter(Account account) { + return Lang.get(account.getProperties(), "character", String.class, null) != null; } - public static String getCurrentCharacter(Map storage) { - Map properties = Lang.get(storage, "properties", Map.class, null); - if (properties == null) return null; - return Lang.get(properties, "character", String.class, null); + public static String getCurrentCharacter(Account account) { + return Lang.get(account.getProperties(), "character", String.class) + .orElseThrow(() -> new IllegalArgumentException("Account " + account + " has not set current character.")); + } + + public static Optional getCurrentCharacter(Map storage) { + Optional properties = Lang.get(storage, "properties", Map.class); + if (!properties.isPresent()) return Optional.empty(); + return Lang.get(properties.get(), "character", String.class); } static String getAccountId(Account account) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java index b0a870ae8..a35c89612 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java @@ -126,6 +126,11 @@ public final class Profile { return vs == null || vs.isUsesGlobal(); } + /** + * Make version use self version settings instead of the global one. + * @param id the version id. + * @return specialized version setting, null if given version does not exist. + */ public VersionSetting specializeVersionSetting(String id) { VersionSetting vs = repository.getVersionSetting(id); if (vs == null) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Settings.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Settings.java index efd25bfa8..bf3c574fb 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Settings.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Settings.java @@ -66,9 +66,9 @@ public class Settings { { for (Map settings : SETTINGS.getAccounts()) { - String characterName = Accounts.getCurrentCharacter(settings); + Optional characterName = Accounts.getCurrentCharacter(settings); AccountFactory factory = Accounts.ACCOUNT_FACTORY.get(Lang.get(settings, "type", String.class, "")); - if (factory == null || characterName == null) { + if (factory == null || !characterName.isPresent()) { // unrecognized account type, so remove it. SETTINGS.getAccounts().remove(settings); continue; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java index 3f4dc9a95..6bc4a7c0c 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java @@ -488,7 +488,7 @@ public final class VersionSetting { .setJavaArgs(getJavaArgs()) .setMaxMemory(getMaxMemory()) .setMinMemory(getMinMemory()) - .setMetaspace(StringUtils.parseInt(getPermSize())) + .setMetaspace(Lang.toIntOrNull(getPermSize())) .setWidth(getWidth()) .setHeight(getHeight()) .setFullscreen(isFullscreen()) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/DialogController.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/DialogController.java index 706bb750c..48aaaa863 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/DialogController.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/DialogController.java @@ -20,6 +20,7 @@ package org.jackhuang.hmcl.ui; import com.jfoenix.concurrency.JFXUtilities; import org.jackhuang.hmcl.auth.Account; import org.jackhuang.hmcl.auth.AuthInfo; +import org.jackhuang.hmcl.auth.MultiCharacterSelector; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount; import org.jackhuang.hmcl.task.SilentException; @@ -47,6 +48,6 @@ public final class DialogController { latch.await(); return Optional.ofNullable(res.get()).orElseThrow(SilentException::new); } - return null; + return account.logIn(MultiCharacterSelector.DEFAULT); } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerController.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerController.java index 2343e29c1..1d80c06fc 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerController.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerController.java @@ -31,6 +31,7 @@ import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.ui.download.InstallerWizardProvider; import java.util.LinkedList; +import java.util.Optional; import java.util.function.Consumer; public class InstallerController { @@ -85,11 +86,11 @@ public class InstallerController { @FXML private void onAdd() { - String gameVersion = GameVersion.minecraftVersion(profile.getRepository().getVersionJar(version)); + Optional gameVersion = GameVersion.minecraftVersion(profile.getRepository().getVersionJar(version)); - if (gameVersion == null) + if (!gameVersion.isPresent()) Controllers.dialog("version.cannot_read"); else - Controllers.getDecorator().startWizard(new InstallerWizardProvider(profile, gameVersion, version, forge, liteLoader, optiFine)); + Controllers.getDecorator().startWizard(new InstallerWizardProvider(profile, gameVersion.get(), version, forge, liteLoader, optiFine)); } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java index 8a4a022bd..b7fbdbe68 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java @@ -184,7 +184,7 @@ public final class MainPage extends StackPane implements DecoratorPage { List children = new LinkedList<>(); List versions = new LinkedList<>(profile.getRepository().getVersions()); for (Version version : versions) { - children.add(buildNode(profile, version.getId(), Lang.nonNull(GameVersion.minecraftVersion(profile.getRepository().getVersionJar(version.getId())), "Unknown"))); + children.add(buildNode(profile, version.getId(), GameVersion.minecraftVersion(profile.getRepository().getVersionJar(version.getId())).orElse("Unknown"))); } FXUtils.resetChildren(masonryPane, children); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java index 74668fc1f..ea87d16e2 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java @@ -144,8 +144,10 @@ public final class YggdrasilAccount extends Account { } private void logIn1(URL url, Object input, Proxy proxy) throws AuthenticationException { - AuthenticationResponse response = makeRequest(url, input, proxy); - if (response == null || !clientToken.equals(response.getClientToken())) + AuthenticationResponse response = makeRequest(url, input, proxy) + .orElseThrow(() -> new AuthenticationException("Server response empty")); + + if (!clientToken.equals(response.getClientToken())) throw new AuthenticationException("Client token changed"); if (response.getSelectedProfile() != null) @@ -205,12 +207,12 @@ public final class YggdrasilAccount extends Account { return result; } - private AuthenticationResponse makeRequest(URL url, Object input, Proxy proxy) throws AuthenticationException { + private Optional makeRequest(URL url, Object input, Proxy proxy) throws AuthenticationException { try { String jsonResult = input == null ? NetworkUtils.doGet(url, proxy) : NetworkUtils.doPost(url, GSON.toJson(input), "application/json", proxy); AuthenticationResponse response = GSON.fromJson(jsonResult, AuthenticationResponse.class); if (response == null) - return null; + return Optional.empty(); if (!StringUtils.isBlank(response.getError())) { if (response.getErrorMessage() != null) if (response.getErrorMessage().contains("Invalid credentials")) @@ -222,7 +224,7 @@ public final class YggdrasilAccount extends Account { throw new AuthenticationException(response.getError() + ": " + response.getErrorMessage()); } - return response; + return Optional.of(response); } catch (IOException e) { throw new ServerDisconnectException(e); } catch (JsonParseException e) { @@ -242,22 +244,22 @@ public final class YggdrasilAccount extends Account { } } - public ProfileTexture getSkin(GameProfile profile) throws IOException, JsonParseException { + public Optional getSkin(GameProfile profile) throws IOException, JsonParseException { if (StringUtils.isBlank(userId)) throw new IllegalStateException("Not logged in"); ProfileResponse response = GSON.fromJson(NetworkUtils.doGet(NetworkUtils.toURL(BASE_PROFILE + UUIDTypeAdapter.fromUUID(profile.getId()))), ProfileResponse.class); - if (response.getProperties() == null) return null; + if (response.getProperties() == null) return Optional.empty(); Property textureProperty = response.getProperties().get("textures"); - if (textureProperty == null) return null; + if (textureProperty == null) return Optional.empty(); TextureResponse texture; String json = new String(Base64.getDecoder().decode(textureProperty.getValue()), Charsets.UTF_8); texture = GSON.fromJson(json, TextureResponse.class); if (texture == null || texture.getTextures() == null) - return null; + return Optional.empty(); - return texture.getTextures().get(ProfileTexture.Type.SKIN); + return Optional.ofNullable(texture.getTextures().get(ProfileTexture.Type.SKIN)); } @Override diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java index d2f096fd7..289706a1d 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java @@ -27,10 +27,7 @@ import org.jackhuang.hmcl.util.NetworkUtils; import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.VersionNumber; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @@ -62,8 +59,8 @@ public final class ForgeVersionList extends VersionList { versions.clear(); for (Map.Entry entry : root.getGameVersions().entrySet()) { - String gameVersion = VersionNumber.parseVersion(entry.getKey()); - if (gameVersion == null) + Optional gameVersion = VersionNumber.parseVersion(entry.getKey()); + if (!gameVersion.isPresent()) continue; for (int v : entry.getValue()) { ForgeVersion version = root.getNumber().get(v); @@ -80,7 +77,7 @@ public final class ForgeVersionList extends VersionList { if (jar == null) continue; - versions.put(gameVersion, new RemoteVersion<>( + versions.put(gameVersion.get(), new RemoteVersion<>( version.getGameVersion(), version.getVersion(), jar, null )); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameVersionList.java index 92469c5fe..30640b183 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameVersionList.java @@ -28,6 +28,7 @@ import org.jackhuang.hmcl.util.VersionNumber; import java.util.Collection; import java.util.Collections; +import java.util.Optional; /** * @@ -55,10 +56,10 @@ public final class GameVersionList extends VersionList { GameRemoteVersions root = Constants.GSON.fromJson(task.getResult(), GameRemoteVersions.class); for (GameRemoteVersion remoteVersion : root.getVersions()) { - String gameVersion = VersionNumber.parseVersion(remoteVersion.getGameVersion()); - if (gameVersion == null) + Optional gameVersion = VersionNumber.parseVersion(remoteVersion.getGameVersion()); + if (!gameVersion.isPresent()) continue; - versions.put(gameVersion, new RemoteVersion<>( + versions.put(gameVersion.get(), new RemoteVersion<>( remoteVersion.getGameVersion(), remoteVersion.getGameVersion(), remoteVersion.getUrl(), diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderInstallTask.java index 3bcc1bc1f..823e9b11b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderInstallTask.java @@ -64,10 +64,7 @@ public final class LiteLoaderInstallTask extends TaskResult { if (!liteLoaderVersionList.isLoaded()) dependents.add(liteLoaderVersionList.refreshAsync(dependencyManager.getDownloadProvider()) - .then(s -> { - doRemote(); - return null; - })); + .then(Task.of(this::doRemote))); else doRemote(); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderVersionList.java index 4e5f16233..43b3c8043 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/liteloader/LiteLoaderVersionList.java @@ -38,6 +38,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.Map; +import java.util.Optional; /** * @@ -67,11 +68,11 @@ public final class LiteLoaderVersionList extends VersionList entry : root.getVersions().entrySet()) { String gameVersion = entry.getKey(); LiteLoaderGameVersions liteLoader = entry.getValue(); - String gg = VersionNumber.parseVersion(gameVersion); - if (gg == null) + Optional gg = VersionNumber.parseVersion(gameVersion); + if (!gg.isPresent()) continue; - doBranch(gg, gameVersion, liteLoader.getRepoitory(), liteLoader.getArtifacts(), false); - doBranch(gg, gameVersion, liteLoader.getRepoitory(), liteLoader.getSnapshots(), true); + doBranch(gg.get(), gameVersion, liteLoader.getRepoitory(), liteLoader.getArtifacts(), false); + doBranch(gg.get(), gameVersion, liteLoader.getRepoitory(), liteLoader.getSnapshots(), true); } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java index 5fe5980e7..6f99a6510 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java @@ -66,8 +66,8 @@ public final class OptiFineBMCLVersionList extends VersionList { if (StringUtils.isBlank(element.getGameVersion())) continue; - String gameVersion = VersionNumber.parseVersion(element.getGameVersion()); - versions.put(gameVersion, new RemoteVersion<>(gameVersion, version, mirror, null)); + VersionNumber.parseVersion(element.getGameVersion()) + .ifPresent(gameVersion -> versions.put(gameVersion, new RemoteVersion<>(gameVersion, version, mirror, null))); } } }; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineInstallTask.java index f8b5e00ca..1e11d0a69 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineInstallTask.java @@ -62,10 +62,7 @@ public final class OptiFineInstallTask extends TaskResult { if (!optiFineVersionList.isLoaded()) dependents.add(optiFineVersionList.refreshAsync(dependencyManager.getDownloadProvider()) - .then(s -> { - doRemote(); - return null; - })); + .then(Task.of(this::doRemote))); else doRemote(); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/CompatibilityRule.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/CompatibilityRule.java index f16be7c66..5d96aaf20 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/CompatibilityRule.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/CompatibilityRule.java @@ -19,10 +19,7 @@ package org.jackhuang.hmcl.game; import org.jackhuang.hmcl.util.Immutable; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.Objects; +import java.util.*; /** * @@ -49,20 +46,20 @@ public final class CompatibilityRule { this.features = features; } - public Action getAppliedAction(Map supportedFeatures) { + public Optional getAppliedAction(Map supportedFeatures) { if (os != null && !os.allow()) - return null; + return Optional.empty(); if (features != null) for (Map.Entry entry : features.entrySet()) if (!Objects.equals(supportedFeatures.get(entry.getKey()), entry.getValue())) - return null; + return Optional.empty(); - return action; + return Optional.ofNullable(action); } public static boolean appliesToCurrentEnvironment(Collection rules) { - return appliesToCurrentEnvironment(rules, Collections.EMPTY_MAP); + return appliesToCurrentEnvironment(rules, Collections.emptyMap()); } public static boolean appliesToCurrentEnvironment(Collection rules, Map features) { @@ -71,9 +68,9 @@ public final class CompatibilityRule { Action action = Action.DISALLOW; for (CompatibilityRule rule : rules) { - Action thisAction = rule.getAppliedAction(features); - if (thisAction != null) - action = thisAction; + Optional thisAction = rule.getAppliedAction(features); + if (thisAction.isPresent()) + action = thisAction.get(); } return action == Action.ALLOW; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameVersion.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameVersion.java index 725a5ff4e..58d9596d2 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameVersion.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameVersion.java @@ -24,6 +24,7 @@ import org.jackhuang.hmcl.util.IOUtils; import java.io.File; import java.io.IOException; +import java.util.Optional; /** * @author huangyuhui @@ -51,31 +52,31 @@ public final class GameVersion { return -1; } - private static String getVersionOfOldMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException { + private static Optional getVersionOfOldMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException { byte[] tmp = IOUtils.readFullyAsByteArray(file.getInputStream(entry)); byte[] bytes = "Minecraft Minecraft ".getBytes(Charsets.US_ASCII); int j = matchArray(tmp, bytes); if (j < 0) - return null; + return Optional.empty(); int i = j + bytes.length; if ((j = lessThan32(tmp, i)) < 0) - return null; + return Optional.empty(); - return new String(tmp, i, j - i, Charsets.US_ASCII); + return Optional.of(new String(tmp, i, j - i, Charsets.US_ASCII)); } - private static String getVersionOfNewMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException { + private static Optional getVersionOfNewMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException { byte[] tmp = IOUtils.readFullyAsByteArray(file.getInputStream(entry)); byte[] str = "-server.txt".getBytes(Charsets.US_ASCII); int j = matchArray(tmp, str); - if (j < 0) return null; + if (j < 0) return Optional.empty(); int i = j + str.length; i += 11; j = lessThan32(tmp, i); - if (j < 0) return null; + if (j < 0) return Optional.empty(); String result = new String(tmp, i, j - i, Charsets.US_ASCII); char ch = result.charAt(0); @@ -83,7 +84,7 @@ public final class GameVersion { if (ch < '0' || ch > '9') { str = "Can't keep up! Did the system time change, or is the server overloaded?".getBytes(Charsets.US_ASCII); j = matchArray(tmp, str); - if (j < 0) return null; + if (j < 0) return Optional.empty(); i = -1; while (j > 0) { if (tmp[j] >= 48 && tmp[j] <= 57) { @@ -92,21 +93,21 @@ public final class GameVersion { } j--; } - if (i == -1) return null; + if (i == -1) return Optional.empty(); int k = i; if (tmp[i + 1] >= (int) 'a' && tmp[i + 1] <= (int) 'z') i++; while (tmp[k] >= 48 && tmp[k] <= 57 || tmp[k] == (int) '-' || tmp[k] == (int) '.' || tmp[k] >= 97 && tmp[k] <= (int) 'z') k--; k++; - return new String(tmp, k, i - k + 1, Charsets.US_ASCII); + return Optional.of(new String(tmp, k, i - k + 1, Charsets.US_ASCII)); } - return result; + return Optional.of(result); } - public static String minecraftVersion(File file) { + public static Optional minecraftVersion(File file) { if (file == null || !file.exists() || !file.isFile() || !file.canRead()) - return null; + return Optional.empty(); ZipFile f = null; try { @@ -119,9 +120,9 @@ public final class GameVersion { ZipArchiveEntry minecraftserver = f.getEntry("net/minecraft/server/MinecraftServer.class"); if ((main != null) && (minecraftserver != null)) return getVersionOfNewMinecraft(f, minecraftserver); - return null; + return Optional.empty(); } catch (IOException e) { - return null; + return Optional.empty(); } finally { IOUtils.closeQuietly(f); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifest.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifest.java index 91fb1ec0b..2815803c2 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifest.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/CurseManifest.java @@ -123,7 +123,7 @@ public final class CurseManifest { if (manifest == null) throw new JsonParseException("`manifest.json` not found. Not a valid Curse modpack."); return new Modpack(manifest.getName(), manifest.getAuthor(), manifest.getVersion(), manifest.getMinecraft().getGameVersion(), - Optional.ofNullable(CompressingUtils.readTextZipEntryQuietly(f, "modlist.html")).orElse( "No description"), manifest); + CompressingUtils.readTextZipEntryQuietly(f, "modlist.html").orElse( "No description"), manifest); } public static final String MINECRAFT_MODPACK = "minecraftModpack"; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/MultiMCInstanceConfiguration.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/MultiMCInstanceConfiguration.java index aab6ab4d5..3d37ada8b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/MultiMCInstanceConfiguration.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/MultiMCInstanceConfiguration.java @@ -19,6 +19,7 @@ package org.jackhuang.hmcl.mod; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; +import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.StringUtils; import java.io.File; @@ -66,17 +67,17 @@ public final class MultiMCInstanceConfiguration { javaPath = p.getProperty("JavaPath"); jvmArgs = p.getProperty("JvmArgs"); fullscreen = Boolean.parseBoolean(p.getProperty("LaunchMaximized")); - maxMemory = StringUtils.parseInt(p.getProperty("MaxMemAlloc")); - minMemory = StringUtils.parseInt(p.getProperty("MinMemAlloc")); - height = StringUtils.parseInt(p.getProperty("MinecraftWinHeight")); - width = StringUtils.parseInt(p.getProperty("MinecraftWinWidth")); + maxMemory = Lang.toIntOrNull(p.getProperty("MaxMemAlloc")); + minMemory = Lang.toIntOrNull(p.getProperty("MinMemAlloc")); + height = Lang.toIntOrNull(p.getProperty("MinecraftWinHeight")); + width = Lang.toIntOrNull(p.getProperty("MinecraftWinWidth")); overrideCommands = Boolean.parseBoolean(p.getProperty("OverrideCommands")); overrideConsole = Boolean.parseBoolean(p.getProperty("OverrideConsole")); overrideJavaArgs = Boolean.parseBoolean(p.getProperty("OverrideJavaArgs")); overrideJavaLocation = Boolean.parseBoolean(p.getProperty("OverrideJavaLocation")); overrideMemory = Boolean.parseBoolean(p.getProperty("OverrideMemory")); overrideWindow = Boolean.parseBoolean(p.getProperty("OverrideWindow")); - permGen = StringUtils.parseInt(p.getProperty("PermGen")); + permGen = Lang.toIntOrNull(p.getProperty("PermGen")); postExitCommand = p.getProperty("PostExitCommand"); preLaunchCommand = p.getProperty("PreLaunchCommand"); showConsole = Boolean.parseBoolean(p.getProperty("ShowConsole")); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CompressingUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CompressingUtils.java index 1f0598852..21966c4c6 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CompressingUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CompressingUtils.java @@ -24,6 +24,7 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipFile; import java.io.*; +import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Predicate; @@ -217,11 +218,11 @@ public final class CompressingUtils { * @param name the location of the text in zip file, something like A/B/C/D.txt * @return the content of given file. */ - public static String readTextZipEntryQuietly(File file, String name) { + public static Optional readTextZipEntryQuietly(File file, String name) { try { - return readTextZipEntry(file, name); + return Optional.of(readTextZipEntry(file, name)); } catch (IOException e) { - return null; + return Optional.empty(); } } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java index 67325487f..c4f0c1beb 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java @@ -207,14 +207,4 @@ public final class StringUtils { return result; } - - public static Integer parseInt(String str) { - if (str == null) - return null; - try { - return Integer.parseInt(str); - } catch (NumberFormatException ex) { - return null; - } - } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/VersionNumber.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/VersionNumber.java index 7f277d0e2..22e5c8717 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/VersionNumber.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/VersionNumber.java @@ -19,6 +19,8 @@ package org.jackhuang.hmcl.util; import java.util.ArrayList; import java.util.Collections; +import java.util.Objects; +import java.util.Optional; /** * The formatted version number represents a version string. @@ -53,6 +55,7 @@ public abstract class VersionNumber implements Comparable { } public static VersionNumber asVersion(String version) { + Objects.requireNonNull(version); try { return asIntVersionNumber(version); } catch (IllegalArgumentException e) { @@ -60,13 +63,13 @@ public abstract class VersionNumber implements Comparable { } } - public static String parseVersion(String str) { + public static Optional parseVersion(String str) { if (str.chars().anyMatch(it -> it != '.' && (it < '0' || it > '9')) || StringUtils.isBlank(str)) - return null; + return Optional.empty(); String[] s = str.split("\\."); for (String i : s) if (StringUtils.isBlank(i)) - return null; + return Optional.empty(); StringBuilder builder = new StringBuilder(); int last = s.length - 1; for (int i = s.length - 1; i >= 0; --i) @@ -74,6 +77,6 @@ public abstract class VersionNumber implements Comparable { last = i; for (int i = 0; i < last; ++i) builder.append(s[i]).append("."); - return builder.append(s[last]).toString(); + return Optional.of(builder.append(s[last]).toString()); } }