Skip to content

Commit

Permalink
Added Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzJustSamu committed Dec 6, 2023
1 parent 8f5414a commit 4e98aaa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 19 deletions.
28 changes: 11 additions & 17 deletions src/main/java/me/itzjustsamu/playerskills/PlayerSkills.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import me.itzjustsamu.playerskills.skill.Skill;
import me.itzjustsamu.playerskills.storage.FlatFileStorage;
import me.itzjustsamu.playerskills.storage.PlayerStorage;
import org.bukkit.Bukkit;
import me.itzjustsamu.playerskills.util.Updater;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.HandlerList;

Expand Down Expand Up @@ -73,7 +73,11 @@ public void enable() {
registerCommand(new SkillsAdminCommand(this));
registerListener(new MenuController());
registerListener(new PlayerListener());
}

Updater updater = new Updater(this, 113626);
updater.checkForUpdates();

}


private void loadSkillsFromConfig() {
Expand All @@ -92,18 +96,13 @@ private void loadSkillsFromConfig() {
boolean isEnabled = skillEntry.getBoolean("enable", true);

if (isEnabled) {
// Convert skillName to lowercase
String lowerCaseSkillName = skillName.toLowerCase();
// Continue with the existing code for enabled skills
registerSkill(lowerCaseSkillName, skillEntry.getString("class"));
} else {
// Skill is disabled, add it to disabledSkills map
try {
Class<?> skillClass = Class.forName(skillEntry.getString("class"));
Constructor<?> constructor = skillClass.getConstructor(PlayerSkills.class);
Skill skill = (Skill) constructor.newInstance(this);

// Convert skillName to lowercase
String lowerCaseSkillName = skillName.toLowerCase();
disabledSkills.put(lowerCaseSkillName, skill);
} catch (Exception e) {
Expand All @@ -125,17 +124,16 @@ private Skill createSkillInstance(String skillClassName) throws Exception {
}

private void registerSkills() {
disabledSkills.clear(); // Clear the disabledSkills map to avoid duplicates
disabledSkills.clear();
loadSkillsFromConfig();
}


private void registerSkill(String skillName, String skillClassName) {
// Convert skillName to lowercase
String lowerCaseSkillName = skillName.toLowerCase();

if (disabledSkills.containsKey(lowerCaseSkillName)) {
return; // Skill is disabled
return;
}

try {
Expand Down Expand Up @@ -173,21 +171,19 @@ private void startAutoSaveTask() {
}
}

// Inside the PlayerSkills class
@Override
public void disable() {
for (SPlayer player : SPlayer.getPlayers().values()) {
SPlayer.save(player);
}

// Save the current state of skill enable/disable to the SkillsSettings.yml file
saveSkillSettings();

for (Skill skill : skills.values()) {
skill.disable();
}

HandlerList.unregisterAll(this); // Assuming the PlayerSkills class is also a listener
HandlerList.unregisterAll(this);

skills.clear();
}
Expand All @@ -206,16 +202,14 @@ public void saveSkillSettings() {

for (Map.Entry<String, Skill> entry : disabledSkills.entrySet()) {
String originalSkillName = entry.getKey();
String lowerCaseSkillName = originalSkillName.toLowerCase(); // Convert to lowercase
String lowerCaseSkillName = originalSkillName.toLowerCase();

boolean isDisabled = skillsSection.getBoolean(lowerCaseSkillName + ".enable", false);

// Save the modified configuration with the new value
skillsSection.set(lowerCaseSkillName + ".enable", isDisabled);

// Update the in-memory state of disabled skills
if (isDisabled) {
entry.getValue().disable(); // Optional: Disable the skill if it was enabled
entry.getValue().disable();
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/main/java/me/itzjustsamu/playerskills/util/Updater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package me.itzjustsamu.playerskills.util;

import me.itzjustsamu.playerskills.PlayerSkills;
import org.bukkit.Bukkit;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Scanner;
import java.util.function.Consumer;

public class Updater {
private final PlayerSkills plugin;
private final int resourceId;

public Updater(PlayerSkills plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}

public void checkForUpdates() {
getLatestVersion(
latestVersion -> {
String currentVersion = plugin.getDescription().getVersion();

if (compareVersions(currentVersion, latestVersion) < 0) {
plugin.getLogger().info("A new version of the PlayerSkills is available: " + latestVersion);
} else if (compareVersions(currentVersion, latestVersion) > 0) {
plugin.getLogger().info("Experimental build: " + currentVersion);
} else {
plugin.getLogger().info("Your plugin is up to date!");
}
},
error -> plugin.getLogger().warning("Failed to check for updates: " + error.getMessage())
);
}

private void getLatestVersion(Consumer<String> consumer, Consumer<Throwable> errorHandler) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
URI uri = new URI("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId);
URL url = uri.toURL();

try (InputStream inputStream = url.openStream(); Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
}
} catch (IOException | RuntimeException | Error exception) {
errorHandler.accept(exception);
} catch (Exception exception) {
errorHandler.accept(new IOException(exception));
}
});
}

private int compareVersions(String version1, String version2) {
String[] parts1 = version1.split("\\.");
String[] parts2 = version2.split("\\.");

for (int i = 0; i < Math.max(parts1.length, parts2.length); i++) {
int part1 = i < parts1.length ? Integer.parseInt(parts1[i]) : 0;
int part2 = i < parts2.length ? Integer.parseInt(parts2[i]) : 0;

if (part1 < part2) {
return -1;
} else if (part1 > part2) {
return 1;
}
}

return 0;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ points:
enabled: false # if true, the price will increase based on total skill points bought and spent
price-increase-per-point: 1 # for every skill point bought, the price of the next will increase by 1 level


options:
logging:
verbose: false
Expand All @@ -351,5 +352,4 @@ messages:
dodge: "&a*** ATTACK DODGED ***"
bleeding-enemy: "&a*** ENEMY BLEEDING ***"
bleeding-self: "&c*** YOU ARE BLEEDING ***"
menu-world-restriction: "&cYou cannot modify your skills in your current location."

menu-world-restriction: "&cYou cannot modify your skills in your current location."

0 comments on commit 4e98aaa

Please sign in to comment.