Skip to content

Commit

Permalink
Made a lot of changes:
Browse files Browse the repository at this point in the history
- Added interval saves for prevent data losts in crashes
- Fixed issues with MongoDB storage method
- Fixed issue with update checker only reading stable versions
  • Loading branch information
Picono435 committed Aug 19, 2020
1 parent d60c605 commit b55628f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.picono435</groupId>
<artifactId>picojobs</artifactId>
<version>1.0-alpha-1a</version>
<version>1.0-alpha-1b</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.picono435</groupId>
<artifactId>picojobs</artifactId>
<version>1.0-alpha-1a</version>
<version>1.0-alpha-1b</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
34 changes: 28 additions & 6 deletions src/main/java/com/gmail/picono435/picojobs/PicoJobsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

Expand Down Expand Up @@ -50,6 +51,7 @@ public class PicoJobsPlugin extends JavaPlugin {
private static Plugin plugin;
private static PicoJobsPlugin instance;
private static boolean legacy;
private static boolean oldVersion;
//DATA
//JOBS DATA
public static Map<String, Job> jobs = new HashMap<String, Job>();
Expand All @@ -61,7 +63,6 @@ public class PicoJobsPlugin extends JavaPlugin {
public void onEnable() {
plugin = this;
instance = this;
saveDefaultConfig();
Bukkit.getServer();
sendConsoleMessage("[PicoJobs] Plugin created by: Picono435#2011. Thank you for use it.");
if(!verificarLicenca()) return;
Expand All @@ -73,11 +74,18 @@ public void onEnable() {
}

sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Creating and configuring internal files...");
saveDefaultConfig();
if(!getConfig().contains("config-version") || !getConfig().getString("config-version").equalsIgnoreCase(getDescription().getVersion())) {
sendConsoleMessage(ChatColor.YELLOW + "[PicoJobs] You were using a old configuration file... Updating it and removing comments, for more information check our WIKI.");
getConfig().options().copyDefaults(true);
getConfig().set("config-version", getDescription().getVersion());
saveConfig();
}
LanguageManager.createLanguageFile();
if(!FileCreator.generateFiles());


sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Getting data from configuration files...");
sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Getting data from storage...");
if(!generateJobsFromConfig()) return;
PicoJobsAPI.getStorageManager().getData();

Expand Down Expand Up @@ -116,12 +124,21 @@ public Integer call() throws Exception {
sendConsoleMessage(ChatColor.GREEN + "[PicoJobs] The plugin was succefully enabled.");

checkVersion();

long saveInterval = getConfig().getConfigurationSection("storage").getInt("save-interval");
if(saveInterval != 0) {
new BukkitRunnable() {
public void run() {
PicoJobsAPI.getStorageManager().saveData(false);
}
}.runTaskTimerAsynchronously(this, saveInterval, saveInterval);
}
}

public void onDisable() {
sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Saving data and configurations...");
jobs.clear();
PicoJobsAPI.getStorageManager().saveData();
PicoJobsAPI.getStorageManager().saveData(true);

sendConsoleMessage(ChatColor.GREEN + "[PicoJobs] The plugin was succefully disabled.");
}
Expand All @@ -142,6 +159,10 @@ public static boolean isLegacy() {
return legacy;
}

public static boolean isOldVersion() {
return oldVersion;
}

private static boolean generateJobsFromConfig() {
ConfigurationSection jobsc = FileCreator.getJobsConfig().getConfigurationSection("jobs");
for(String jobname : jobsc.getKeys(false)) {
Expand Down Expand Up @@ -230,7 +251,7 @@ private static boolean checkLegacy() {
private void checkVersion() {
String version = "1.0";
try {
URL url = new URL("https://api.github.com/repos/Picono435/PicoJobs/releases/latest");
URL url = new URL("https://api.github.com/repos/Picono435/PicoJobs/releases");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
Expand All @@ -246,8 +267,8 @@ private void checkVersion() {
in.close();

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(content.toString());

JSONArray jsonArray = (JSONArray) parser.parse(content.toString());
JSONObject json = (JSONObject) jsonArray.get(0);
version = (String)json.get("tag_name");

} catch(Exception ex) {
Expand All @@ -261,6 +282,7 @@ private void checkVersion() {
new BukkitRunnable() {
public void run() {
sendConsoleMessage(ChatColor.DARK_RED + "[PicoJobs] You are using a old version of the plugin. Please download the new version in our pages.");
oldVersion = true;
return;
}
}.runTaskLater(this, 5L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scheduler.BukkitRunnable;

import com.gmail.picono435.picojobs.PicoJobsPlugin;
import com.gmail.picono435.picojobs.api.JobPlayer;
import com.gmail.picono435.picojobs.managers.LanguageManager;

public class CreatePlayerListener implements Listener {

Expand All @@ -17,4 +20,16 @@ public void onJoin(PlayerJoinEvent e) {
PicoJobsPlugin.playersdata.put(p.getUniqueId(), new JobPlayer(null, 0, 1, 0, false));
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void onCheckVersionJoin(PlayerJoinEvent e) {
Player p = e.getPlayer();
new BukkitRunnable() {
public void run() {
if(p.hasPermission("picojobs.admin") && PicoJobsPlugin.isOldVersion()) {
p.sendMessage("\n" + LanguageManager.formatMessage("&cYou are using an old version of PicoJobs, please update. This new version can include fixes to current errors.\n&c"));
}
}
}.runTaskLater(PicoJobsPlugin.getPlugin(), 20L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,29 @@ private void getDataInMongoDB() {
* SAVE DATA METHODS
*/
// GENERAL
public void saveData() {
public void saveData(boolean log) {
storageMethod = StorageMethod.getStorageMethod(PicoJobsPlugin.getPlugin().getConfig().getConfigurationSection("storage").getString("storage-method"));

if(storageMethod == StorageMethod.YAML) {
PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the YAML storage method.");
if(log) {
PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the YAML storage method.");
}
saveInConfig();
return;
}

if(storageMethod == StorageMethod.MYSQL) {
PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the MySQL storage method.");
if(log) {
PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the MySQL storage method.");
}
saveInMySQL();
return;
}

if(storageMethod == StorageMethod.MONGODB) {
PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the MongoDB storage method.");
if(log) {
PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the MongoDB storage method.");
}
saveInMongoDB();
return;
}
Expand All @@ -118,14 +124,13 @@ public void saveData() {

// YAML
private void saveInConfig() {
if(FileCreator.getDataFile() != null) {
FileCreator.getDataFile().delete();
}
if(!FileCreator.createDataFile()) return;
ConfigurationSection playerDataCategory = FileCreator.getData().getConfigurationSection("playerdata");
for(UUID uuid : PicoJobsPlugin.playersdata.keySet()) {
JobPlayer jp = PicoJobsPlugin.playersdata.get(uuid);
ConfigurationSection player = playerDataCategory.createSection(uuid.toString());
ConfigurationSection player = playerDataCategory.getConfigurationSection(uuid.toString());
if(player == null) {
player = playerDataCategory.createSection(uuid.toString());
}
if(jp.getJob() == null) {
player.set("job", null);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public boolean openConnection(){
return true;
}

public void addINDB(final String uuid, final String job, final double method, final double level, final double salary, final boolean isWorking) {
public void addINDB(final String uuid, String job, final double method, final double level, final double salary, final boolean isWorking) {
Document obj = new Document("uuid", uuid);
if(job == null) job = "";
obj.put("job", job);
obj.put("method", method);
obj.put("level", level);
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# Here you will only find basic configurations
# IMPORTANT #

# DO NOT CHANGE THIS
config-version: ${project.version}

#################################################################################
# #
# Important configurations #
Expand All @@ -33,6 +36,8 @@ storage:
# MySQL - Save the data in a MySQL database
# MongoDB - Save the data in a MongoDB database
storage-method: YAML
# Here you should put how many ticks should be the interval between saves, put 0 to disable
save-interval: 0
# MySQL configuration, required only if the storage method is MySQL
mysql:
host: localhost
Expand Down

0 comments on commit b55628f

Please sign in to comment.