Skip to content

Commit

Permalink
Make it clearer which functions go against the API and wich just chec…
Browse files Browse the repository at this point in the history
…k the cache (#86)
  • Loading branch information
NiklasEi committed May 8, 2021
1 parent f4492e2 commit b7c672f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/nikl/gamebox/GameBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public void hookAfterConnectingToCloud() {
getModulesManager().updateModulesAndPrintInfo();

CloudService cloudService = getModulesManager().getCloudService();
java.util.List<CloudModuleData> cloudModuleData = cloudService.getCloudContent();
java.util.List<CloudModuleData> cloudModuleData = cloudService.getCachedCloudContent();
HashMap<String, String> context = new HashMap<>();
context.put("amount", String.valueOf(cloudModuleData.size()));
Bukkit.getConsoleSender().sendMessage(lang.PREFIX + lang.replaceContext(lang.CMD_MODULES_LIST_HEADER, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void registerCommandCompletions() {
gameBox.getModulesManager().getLoadedVersionedModules().stream().map(ModuleBasicData::getId).collect(Collectors.toList())
);
getCommandCompletions().registerCompletion("cloudModuleIds", c ->
gameBox.getModulesManager().getCloudService().getCloudContent().stream().map(CloudModuleData::getId).collect(Collectors.toList())
gameBox.getModulesManager().getCloudService().getCachedCloudContent().stream().map(CloudModuleData::getId).collect(Collectors.toList())
);
getCommandCompletions().registerCompletion("SaveTypes", c ->
Arrays.asList(Arrays.stream(SaveType.values()).map(Object::toString).toArray(String[]::new))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void run() {
SemanticVersion semVersion = null;
try {
ModulesManager modulesManager = gameBox.getModulesManager();
CloudModuleData cloudModuleData = modulesManager.getCloudService().getModuleData(moduleID);
CloudModuleData cloudModuleData = modulesManager.getCloudService().getCachedModuleData(moduleID);
if (version != null) {
try {
semVersion = new SemanticVersion(version);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.nikl.gamebox.commands.admin.modules;

import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.PreCommand;
import co.aikar.commands.annotation.Subcommand;
import me.nikl.gamebox.GameBox;
Expand All @@ -12,7 +11,6 @@
import org.bukkit.command.CommandSender;

import java.util.HashMap;
import java.util.Map;

@CommandAlias("%adminCommand")
public class List extends GameBoxBaseCommand {
Expand All @@ -34,7 +32,7 @@ public boolean preCommand(CommandSender sender) {
@Subcommand("module|modules|m list|l")
public void onModuleList(CommandSender sender) {
CloudService cloudService = gameBox.getModulesManager().getCloudService();
java.util.List<CloudModuleData> cloudModuleData = cloudService.getCloudContent();
java.util.List<CloudModuleData> cloudModuleData = cloudService.getCachedCloudContent();
HashMap<String, String> context = new HashMap<>();
context.put("amount", String.valueOf(cloudModuleData.size()));
gameBox.lang.sendMessage(sender, gameBox.lang.CMD_MODULES_LIST_HEADER, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import me.nikl.gamebox.utility.ModuleUtility;
import me.nikl.gamebox.utility.Permission;
import me.nikl.gamebox.utility.versioning.SemanticVersion;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;

Expand Down Expand Up @@ -50,7 +49,7 @@ public void run() {
SemanticVersion semVersion = null;
try {
ModulesManager modulesManager = gameBox.getModulesManager();
CloudModuleData cloudModuleData = modulesManager.getCloudService().getModuleData(moduleID);
CloudModuleData cloudModuleData = modulesManager.getCloudService().getCachedModuleData(moduleID);
if (version != null) {
try {
semVersion = new SemanticVersion(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ModulesGuiManager(GameBox gameBox, GuiManager guiManager) {
}

public void loadGui() {
List<CloudModuleData> cloudModuleData = gameBox.getModulesManager().getCloudService().getCloudContent();
List<CloudModuleData> cloudModuleData = gameBox.getModulesManager().getCloudService().getCachedCloudContent();
prepareCloudModules(cloudModuleData);
preparePrivateModules(cloudModuleData);
sortAndRenderGui();
Expand Down Expand Up @@ -95,7 +95,7 @@ private String buildIdentifier(String name, String id) {

private void removeModule(VersionedModule module) {
try {
CloudModuleData data = gameBox.getModulesManager().getCloudService().getModuleData(module.getId());
CloudModuleData data = gameBox.getModulesManager().getCloudService().getCachedModuleData(module.getId());
Button updatedButton = buildModuleButton(data, null);
if (this.modulesListGui.updateModule(module.getId(), updatedButton)) {
this.moduleDetails.updateGuiForModule(module.getId());
Expand All @@ -109,7 +109,7 @@ private void removeModule(VersionedModule module) {

private void installModule(VersionedModule module) {
try {
CloudModuleData data = gameBox.getModulesManager().getCloudService().getModuleData(module.getId());
CloudModuleData data = gameBox.getModulesManager().getCloudService().getCachedModuleData(module.getId());
Button updatedButton = buildModuleButton(data, module.getVersionData().getVersion());
if(this.modulesListGui.updateModule(module.getId(), updatedButton)) {
this.moduleDetails.updateGuiForModule(module.getId());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/nikl/gamebox/module/ModulesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void run() {
@Override
public void run() {
try {
cloudService.updateCloudContent();
cloudService.cacheCloudContent();
hookAfterConnectingToCloud.runTask(gameBox);
} catch (GameBoxCloudException e) {
gameBox.getLogger().severe("Error while attempting to load cloud content");
Expand Down Expand Up @@ -209,7 +209,7 @@ public void softDeleteJarFile(File jar) throws IOException {
public void collectUpdatesForInstalledModules() {
hasUpdateAvailable.clear();
for (String moduleId : localModules.keySet()) {
if (cloudService.hasUpdate(localModules.get(moduleId))) {
if (cloudService.hasCachedUpdate(localModules.get(moduleId))) {
hasUpdateAvailable.add(moduleId);
}
}
Expand Down Expand Up @@ -404,7 +404,7 @@ private void autoUpdateModules() {
moduleContext.put("id", module.getIdentifier());
moduleContext.put("installedVersion", module.getModuleData().getVersionData().getVersion().toString());
try {
SemanticVersion latestVersion = cloudService.getModuleData(module.getIdentifier()).getLatestVersion();
SemanticVersion latestVersion = cloudService.getCachedModuleData(module.getIdentifier()).getLatestVersion();
moduleContext.put("availableVersion", latestVersion.toString());
if (!latestVersion.isCompatibleUpdateFor(module.getModuleData().getVersionData().getVersion())) {
skippCount ++;
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/me/nikl/gamebox/module/cloud/CloudService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -57,7 +56,12 @@ public CloudService(GameBox gameBox, CloudFacade facade) {
this.facade = facade;
}

public void updateCloudContent() throws GameBoxCloudException {
/**
* Cache the modules currently offered by the GameBox API
*
* Do not call this function on the main thread
*/
public void cacheCloudContent() throws GameBoxCloudException {
ApiResponse<CloudModuleData[]> response = this.facade.getCloudModuleData();
if (response.getError() != null) {
throw response.getError();
Expand All @@ -70,12 +74,17 @@ public void updateCloudContent() throws GameBoxCloudException {
}
}

public CloudModuleData getModuleData(String moduleID) throws GameBoxCloudException {
public CloudModuleData getCachedModuleData(String moduleID) throws GameBoxCloudException {
CloudModuleData cloudModuleData = cloudContent.get(moduleID);
if (cloudModuleData == null) throw new CloudModuleNotFoundException("No moduledata found for ID '" + moduleID + "'");
return cloudModuleData;
}

/**
* Cache the modules currently offered by the GameBox API
*
* Do not call this function on the main thread
*/
public CloudModuleDataWithVersions getCloudModuleDataWithVersions(String moduleId) throws GameBoxCloudException {
ApiResponse<CloudModuleDataWithVersions> response = this.facade.getCloudModuleDataWithVersions(moduleId);
if (response.getError() != null) {
Expand All @@ -84,7 +93,7 @@ public CloudModuleDataWithVersions getCloudModuleDataWithVersions(String moduleI
return response.getData();
}

public boolean hasUpdate(LocalModule localModule) {
public boolean hasCachedUpdate(LocalModule localModule) {
CloudModuleData cloudModule = cloudContent.get(localModule.getId());
if (cloudModule == null) {
// might be local module
Expand Down Expand Up @@ -148,6 +157,11 @@ public void downloadModule(URL fileUrl, String fileName, DataBase.Callback<Local
downloadingModules.get(fileName).start();
}

/**
* Get the versioned cloud module data from the GameBox API
*
* Do not call this function on the main thread
*/
public VersionedCloudModule getVersionedCloudModule(String moduleId, SemanticVersion version) throws GameBoxCloudException {
ApiResponse<VersionedCloudModule> response = this.facade.getVersionedCloudModuleData(moduleId, version);
if (response.getError() != null) {
Expand All @@ -156,11 +170,11 @@ public VersionedCloudModule getVersionedCloudModule(String moduleId, SemanticVer
return response.getData();
}

public List<CloudModuleData> getCloudContent() {
public List<CloudModuleData> getCachedCloudContent() {
return new ArrayList<>(this.cloudContent.values());
}

public long secondsSinceLastCloudContentUpdate() {
public long secondsSinceLastCloudContentCacheUpdate() {
return Math.round((System.currentTimeMillis() - lastCloudContentUpdate)/1000.);
}
}

0 comments on commit b7c672f

Please sign in to comment.