Skip to content

Commit

Permalink
Refactor VersionListHelper, remove duplicate versions and show non-of…
Browse files Browse the repository at this point in the history
…ficial versions above official ones.
  • Loading branch information
leMaik committed Apr 20, 2016
1 parent 2a776d5 commit 030bd8c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public HeaderPanel() {
}

private void addBackground() {
BufferedImage bg = null;
BufferedImage bg;
try {
bg = ImageIO.read(getClass().getResource("/images/header.png"));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void init(Config config) {
if (mVersionList.isVersionAvailableOnline(version)) {
this.mCurrentVersion = new MinecraftVersion(version);
} else {
mVersionList.checkVesion(version);
mVersionList.checkVersion(version);
this.mCurrentVersion = new MinecraftVersion(version);
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public void logout() {

//TODO vorher besser alten Service ordentlich stoppen oder neu Init?
public void setMinecraftVersion(String version) throws CraftenLogicException {
this.mVersionList.checkVesion(version);
this.mVersionList.checkVersion(version);
this.mCurrentVersion = new MinecraftVersion(version);

mMinecraftPath.setVersionName(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,36 @@
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class VersionListHelper {
private static final Logger LOGGER = LogManager.getLogger(VersionListHelper.class);
private List<String> mVersions = new ArrayList<String>();
private MinecraftPath mMinecraftPath;
private final List<String> versions;
private final MinecraftPath minecraftPath;

public VersionListHelper(MinecraftPath mcPath) {
this.mMinecraftPath = mcPath;
fillVersionsList();
}
minecraftPath = mcPath;
versions = VersionLoader.getVersionStringList();

private void fillVersionsList() {
mVersions = VersionLoader.getVersionStringList();
checkVersionsPath();
}
//hash set to check for duplicates without evil n² time complexity
HashSet<String> foundVersions = new HashSet<>();
foundVersions.addAll(versions);

private void checkVersionsPath() {
String path = mMinecraftPath.getMinecraftVersionsDir();
File versionsDir = new File(path);
//prepend local (i.e. custom modded) versions
List<String> localVersions = getLocalVersions(new File(minecraftPath.getMinecraftVersionsDir()));
for (String version : localVersions) {
if (!foundVersions.contains(version)) {
versions.add(0, version);
}
}
}

private static List<String> getLocalVersions(File versionsDir) {
List<String> versions = new ArrayList<>();
File[] files = versionsDir.listFiles();

if (files != null)
if (files != null) {
for (File dir : files) {
if (dir.isDirectory()) {
File[] f = dir.listFiles(new FileFilter() {
Expand All @@ -46,18 +52,20 @@ public boolean accept(File pathname) {
});

if (f.length == 1) {
mVersions.add(dir.getName());
versions.add(dir.getName());
}
}
}
}
return versions;
}

public List<String> getVersionsList() {
return mVersions;
return versions;
}

public void checkVesion(String version) throws CraftenVersionNotKnownException {
if (!mVersions.contains(version)) {
public void checkVersion(String version) throws CraftenVersionNotKnownException {
if (!versions.contains(version)) {
throw new CraftenVersionNotKnownException("Version: " + version);
}
}
Expand All @@ -76,8 +84,8 @@ public boolean isVersionAvailableOnline(String versionName) {
return false;
}

if (!mVersions.contains(versionName)) {
mVersions.add(versionName);
if (!versions.contains(versionName)) {
versions.add(versionName);
}
return true;
} catch (Exception e) {
Expand Down

0 comments on commit 030bd8c

Please sign in to comment.