Skip to content

Commit

Permalink
Make most IO related things async, cleanup code style
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Oct 9, 2020
1 parent 0b5956d commit b9eb00b
Show file tree
Hide file tree
Showing 12 changed files with 634 additions and 457 deletions.
10 changes: 3 additions & 7 deletions src/main/java/me/danjono/inventoryrollback/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public UpdateChecker(JavaPlugin plugin, Integer resourceId, boolean enabled) {
this.currentVersion = regex(plugin.getDescription().getVersion());

if (!enabled) {
result = UpdateResult.DISABLED;
return;
}

Expand All @@ -48,7 +47,7 @@ public UpdateChecker(JavaPlugin plugin, Integer resourceId, boolean enabled) {
}

private void run() {
URLConnection con = null;
URLConnection con;
try {
con = checkURL.openConnection();
} catch (IOException e1) {
Expand All @@ -65,17 +64,14 @@ private void run() {

if (availableVersion.isEmpty()) {
result = UpdateResult.FAIL_SPIGOT;
return;
} else if (availableVersion.equalsIgnoreCase(currentVersion)) {
result = UpdateResult.NO_UPDATE;
return;
} else if (!availableVersion.equalsIgnoreCase(currentVersion)) {
result = UpdateResult.UPDATE_AVAILABLE;
return;
} else {
result = UpdateResult.FAIL_SPIGOT;
}

result = UpdateResult.FAIL_SPIGOT;

}

public UpdateResult getResult() {
Expand Down
62 changes: 31 additions & 31 deletions src/main/java/me/danjono/inventoryrollback/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

public class Commands extends ConfigFile implements CommandExecutor {

Expand All @@ -28,36 +29,35 @@ public boolean onCommand(CommandSender sender, Command cmd, String Label, String
} else {
switch (args[0]) {
case "restore": {
if (sender instanceof Player) {
if (sender.hasPermission("inventoryrollback.restore")) {
if (!ConfigFile.enabled) {
sender.sendMessage(MessageData.pluginName + MessageData.disabledMessage);
break;
}

Player staff = (Player) sender;

if (args.length == 1) {
try {
staff.openInventory(new MainMenu(staff, staff).getMenu());
} catch (NullPointerException ignored) {
}
} else if (args.length == 2) {
@SuppressWarnings("deprecation")
OfflinePlayer rollbackPlayer = Bukkit.getOfflinePlayer(args[1]);

try {
staff.openInventory(new MainMenu(staff, rollbackPlayer).getMenu());
} catch (NullPointerException ignored) {
}
} else {
sender.sendMessage(MessageData.pluginName + MessageData.error);
}
} else {
sender.sendMessage(MessageData.pluginName + MessageData.noPermission);
if ( !(sender instanceof Player)) {
sender.sendMessage(MessageData.pluginName + MessageData.playerOnly);
break;
}
if (!sender.hasPermission("inventoryrollback.restore")) {
sender.sendMessage(MessageData.pluginName + MessageData.noPermission);
break;
}
if (!ConfigFile.enabled) {
sender.sendMessage(MessageData.pluginName + MessageData.disabledMessage);
break;
}

Player staff = (Player) sender;

if (args.length == 1) {
final Inventory inventory = new MainMenu(staff, staff).getMenu();
if (inventory != null) {
staff.openInventory(inventory);
}
} else if (args.length == 2) {
@SuppressWarnings("deprecation")
OfflinePlayer rollbackPlayer = Bukkit.getOfflinePlayer(args[1]);

final Inventory inventory = new MainMenu(staff, rollbackPlayer).getMenu();
staff.openInventory(inventory);

} else {
sender.sendMessage(MessageData.pluginName + MessageData.playerOnly);
sender.sendMessage(MessageData.pluginName + MessageData.error);
}
break;
}
Expand All @@ -77,8 +77,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String Label, String
}

Player player = (Player) offlinePlayer;
new SaveInventory(player, LogType.FORCE, null, player.getInventory(), player.getEnderChest()).createSave();
sender.sendMessage(MessageData.pluginName + messages.forceSaved(offlinePlayer.getName()));
new SaveInventory(player, LogType.FORCE, null, player.getInventory(), player.getEnderChest())
.saveToDiskAsync().thenAccept(unused -> sender.sendMessage(MessageData.pluginName + messages.forceSaved(offlinePlayer.getName())));

break;
} else {
Expand Down Expand Up @@ -125,4 +125,4 @@ public boolean onCommand(CommandSender sender, Command cmd, String Label, String
return true;

}
}
}
146 changes: 97 additions & 49 deletions src/main/java/me/danjono/inventoryrollback/data/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import me.danjono.inventoryrollback.InventoryRollback;
import me.danjono.inventoryrollback.config.ConfigFile;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class PlayerData {

Expand All @@ -18,55 +20,80 @@ public class PlayerData {
private final File folderLocation;

private File playerFile;
private FileConfiguration playerData;

public PlayerData(Player player, LogType logType) {
this.logType = logType;
this.uuid = player.getUniqueId();
this.folderLocation = new File(ConfigFile.folderLocation, "saves/");

findPlayerFile();
findPlayerData();
}
private FileConfiguration playerData = new YamlConfiguration();

public PlayerData(OfflinePlayer player, LogType logType) {
this.logType = logType;
this.uuid = player.getUniqueId();
this.folderLocation = new File(ConfigFile.folderLocation, "saves/");
this(player.getUniqueId(), logType, true);
}

findPlayerFile();
findPlayerData();
public PlayerData(OfflinePlayer player, LogType logType, boolean load) {
this(player.getUniqueId(), logType, load);
}

public PlayerData(UUID uuid, LogType logType) {
this(uuid, logType, true);
}

public PlayerData(UUID uuid, LogType logType, boolean load) {
this.logType = logType;
this.uuid = uuid;
this.folderLocation = new File(ConfigFile.folderLocation, "saves/");

findPlayerFile();
findPlayerData();
if (load) {
loadData();
}
}

private boolean findPlayerFile() {
if (logType == LogType.JOIN) {
this.playerFile = new File(folderLocation, "joins/" + uuid + ".yml");
} else if (logType == LogType.QUIT) {
this.playerFile = new File(folderLocation, "quits/" + uuid + ".yml");
} else if (logType == LogType.DEATH) {
this.playerFile = new File(folderLocation, "deaths/" + uuid + ".yml");
} else if (logType == LogType.WORLD_CHANGE) {
this.playerFile = new File(folderLocation, "worldChanges/" + uuid + ".yml");
} else if (logType == LogType.FORCE) {
this.playerFile = new File(folderLocation, "force/" + uuid + ".yml");
private File determinePlayerFile() {
final String prefix;
switch (logType) {
case JOIN:
prefix = "joins";
break;
case QUIT:
prefix = "quits";
break;
case DEATH:
prefix = "deaths";
break;
case WORLD_CHANGE:
prefix = "worldChanges";
break;
case FORCE:
prefix = "force";
break;
default:
return null;
}

return this.playerFile != null;
return new File(folderLocation, prefix + File.separator + uuid + ".yml");
}

private boolean findPlayerData() {
this.playerData = YamlConfiguration.loadConfiguration(playerFile);
public boolean loadData() {
this.playerData = null;
final File file = determinePlayerFile();
if (file != null) {
this.playerData = YamlConfiguration.loadConfiguration(file);
return true;
}
return false;
}

return this.playerData != null;
public CompletableFuture<Boolean> loadDataAsync() {
this.playerData = null;
final File file = determinePlayerFile();
final CompletableFuture<Boolean> completableFuture = new CompletableFuture<>();
if (file == null) {
return CompletableFuture.completedFuture(false);
}
Bukkit.getScheduler().runTaskAsynchronously(InventoryRollback.getInstance(), () -> {
final YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
Bukkit.getScheduler().callSyncMethod(InventoryRollback.getInstance(), () -> {
this.playerData = configuration;
completableFuture.complete(true);
return null;
});
});
return completableFuture;
}

public File getFile() {
Expand All @@ -77,30 +104,51 @@ public FileConfiguration getData() {
return this.playerData;
}

public void saveData() {
public CompletableFuture<Boolean> saveData() {
final CompletableFuture<Boolean> completableFuture = new CompletableFuture<>();
final YamlConfiguration configuration = new YamlConfiguration();
try {
configuration.loadFromString(playerData.saveToString());
} catch (InvalidConfigurationException ex) {
ex.printStackTrace();
return CompletableFuture.completedFuture(false);
}
InventoryRollback.getInstance().getServer().getScheduler().runTaskAsynchronously(InventoryRollback.getInstance(), () -> {
try {
playerData.save(playerFile);
configuration.save(playerFile);
completableFuture.complete(true);
} catch (IOException e) {
e.printStackTrace();

completableFuture.complete(false);
}
});
return completableFuture;
}

public boolean saveDataSync() {
try {
playerData.save(playerFile);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

public int getMaxSaves() {
if (logType == LogType.JOIN) {
return ConfigFile.maxSavesJoin;
} else if (logType == LogType.QUIT) {
return ConfigFile.maxSavesQuit;
} else if (logType == LogType.DEATH) {
return ConfigFile.maxSavesDeath;
} else if (logType == LogType.WORLD_CHANGE) {
return ConfigFile.maxSavesWorldChange;
} else if (logType == LogType.FORCE) {
return ConfigFile.maxSavesForce;
} else {
return 0;
switch (logType) {
case JOIN:
return ConfigFile.maxSavesJoin;
case FORCE:
return ConfigFile.maxSavesForce;
case WORLD_CHANGE:
return ConfigFile.maxSavesWorldChange;
case DEATH:
return ConfigFile.maxSavesDeath;
case QUIT:
return ConfigFile.maxSavesQuit;
default:
return 0;
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/me/danjono/inventoryrollback/gui/BackupMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ public Inventory showItems() {
int position = 0;

//If the backup file is invalid it will return null, we want to catch it here
try {
//Add items
for (int i = 0; i < mainInventory.length - 5; i++) {
if (mainInventory[item] != null) {
inv.setItem(position, mainInventory[item]);
position++;
}

item++;
}
} catch (NullPointerException e) {
if (mainInventory == null) {
staff.sendMessage(MessageData.pluginName + MessageData.errorInventory);
return null;
}
//Add items
for (int i = 0; i < mainInventory.length - 5; i++) {
final ItemStack itemStack = mainInventory[item];
if (itemStack != null) {
inv.setItem(position, itemStack);
position++;
}

item++;
}

item = 36;
position = 44;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public Inventory getMenu() {

if (forceSaveFile.exists()) {
mainMenu.setItem(position, buttons.createLogTypeButton(new ItemStack(ConfigFile.forceSaveIcon), uuid, MessageData.forceSaveIconName, LogType.FORCE, null));
position++;
}

return mainMenu;
Expand Down
Loading

0 comments on commit b9eb00b

Please sign in to comment.