Skip to content

Commit

Permalink
Merge pull request #389 from Mindgamesnl/feature/qol-chores
Browse files Browse the repository at this point in the history
Minor fixes (logging during backups, backup compatibility on windows servers, fixes to datafiles on windows servers, errors when empty sources are used)
  • Loading branch information
Mindgamesnl authored Jan 31, 2024
2 parents 4934d01 + ed6ecd4 commit add57d3
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion plugin/src/main/bash/data.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BUILD_NUM="1063"
BUILD_NUM="1067"
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.Instant;

public class BackupService extends Service {

Expand All @@ -26,11 +27,13 @@ public void makeBackup(boolean force) {
backupRootDirectory.mkdir();
}

long unixTime = System.currentTimeMillis() / 1000L;
long unixTime = Instant.now().getEpochSecond();
// create current backup dir
File backupDir = new File(backupRootDirectory, "/backup-" + unixTime);
File backupDir = new File(backupRootDirectory, File.pathSeparator + "backup-" + unixTime);
if (!backupDir.exists()) {
backupDir.mkdir();
} else {
OpenAudioLogger.toConsole("Backup directory already exists");
}

try {
Expand All @@ -39,31 +42,42 @@ public void makeBackup(boolean force) {
new File(backupDir, "config.yml").toPath(),
StandardCopyOption.REPLACE_EXISTING
);
} catch (IOException e) {}
} catch (IOException e) {
OpenAudioLogger.toConsole("Failed to backup config.yml");
e.printStackTrace();
}

try {
Files.copy(
new File(MagicValue.STORAGE_DIRECTORY.get(File.class), "data.yml").toPath(),
new File(backupDir, "data.yml").toPath(),
StandardCopyOption.REPLACE_EXISTING
);
} catch (IOException e) {}
} catch (IOException e) {
OpenAudioLogger.toConsole("Failed to backup data.yml");
e.printStackTrace();
}

try {
Files.copy(
new File(MagicValue.STORAGE_DIRECTORY.get(File.class), "database.db").toPath(),
new File(backupDir, "database.db").toPath(),
StandardCopyOption.REPLACE_EXISTING
);
} catch (IOException e) {}
} catch (IOException e) {
// legacy, allowed to fail
}

try {
Files.copy(
new File(MagicValue.STORAGE_DIRECTORY.get(File.class), "storm.db").toPath(),
new File(backupDir, "storm.db").toPath(),
StandardCopyOption.REPLACE_EXISTING
);
} catch (IOException e) {}
} catch (IOException e) {
OpenAudioLogger.toConsole("Failed to backup storm.db");
e.printStackTrace();
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

import com.craftmend.openaudiomc.api.interfaces.Client;
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection;
import com.craftmend.openaudiomc.generic.commands.objects.CommandError;
import com.craftmend.openaudiomc.generic.media.objects.OptionalError;
import com.craftmend.openaudiomc.generic.media.utils.Validation;
import com.craftmend.openaudiomc.generic.platform.OaColor;
import com.craftmend.openaudiomc.generic.user.User;
import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand;
import com.craftmend.openaudiomc.generic.commands.objects.Argument;
import com.craftmend.openaudiomc.generic.media.objects.Media;
import com.craftmend.openaudiomc.generic.media.objects.MediaOptions;
import lombok.SneakyThrows;

import java.util.Optional;

Expand All @@ -27,13 +30,18 @@ public PlaySubCommand() {
}

@Override
@SneakyThrows
public void onExecute(User sender, String[] args) {
if (args.length == 0) {
sender.makeExecuteCommand("oa help " + getCommand());
return;
}

if (args.length == 2) {
if (Validation.isStringInvalid(args[1])) {
throw new CommandError("Invalid source url.");
}

Media media = new Media(args[1]);
int affected = 0;

Expand All @@ -59,6 +67,10 @@ public void onExecute(User sender, String[] args) {
return;
}

if (Validation.isStringInvalid(args[1])) {
throw new CommandError("Invalid source url.");
}

Media media = new Media(args[1]).applySettings(mediaOptions);

for (User<?> user : resolveSelector(sender, args[0])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.craftmend.openaudiomc.generic.media.utils;

public class Validation {

public static boolean isStringInvalid(String s) {
if (s == null) return true;
if (s.isEmpty()) return true;
if (s.equalsIgnoreCase("set")) return true;
return s.trim().isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public void onCommand(ServerCommandEvent event) {
spigotPlayerSelector.setSender(new SpigotUserAdapter(event.getSender()));
spigotPlayerSelector.setString(selector);

if (spigotPlayerSelector.getResults().isEmpty()) {
event.getSender().sendMessage(MagicValue.COMMAND_PREFIX.get(String.class) + "No players found for selector " + selector);
event.setCancelled(true);
return;
}

// process the selector, build a new command and re-run
for (User<?> target : spigotPlayerSelector.getResults()) {
String playerCommand = commandPreset.replaceAll("%%player%%", target.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.craftmend.openaudiomc.OpenAudioMc;
import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand;
import com.craftmend.openaudiomc.generic.commands.objects.CommandError;
import com.craftmend.openaudiomc.generic.database.DatabaseService;
import com.craftmend.openaudiomc.generic.media.utils.Validation;
import com.craftmend.openaudiomc.generic.user.User;
import com.craftmend.openaudiomc.spigot.OpenAudioMcSpigot;
import com.craftmend.openaudiomc.spigot.modules.regions.objects.RegionProperties;
import com.craftmend.openaudiomc.spigot.modules.regions.registry.WorldRegionManager;
import lombok.SneakyThrows;
import org.bukkit.ChatColor;

public class RegionCreateSubCommand extends SubCommand {
Expand All @@ -19,6 +22,7 @@ public RegionCreateSubCommand(OpenAudioMcSpigot openAudioMcSpigot) {
}

@Override
@SneakyThrows
public void onExecute(User sender, String[] args) {
args[1] = args[1].toLowerCase();

Expand All @@ -43,6 +47,10 @@ public void onExecute(User sender, String[] args) {
return;
}

if (Validation.isStringInvalid(args[2])) {
throw new CommandError("Invalid source url.");
}

RegionProperties rp = new RegionProperties(args[2], volume, 1000, true, args[1], sender.getWorld());
OpenAudioMc.getService(DatabaseService.class).getRepository(RegionProperties.class)
.save(rp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.craftmend.openaudiomc.OpenAudioMc;
import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand;
import com.craftmend.openaudiomc.generic.commands.objects.CommandError;
import com.craftmend.openaudiomc.generic.media.MediaService;
import com.craftmend.openaudiomc.generic.media.utils.Validation;
import com.craftmend.openaudiomc.generic.user.User;
import com.craftmend.openaudiomc.spigot.modules.commands.subcommands.SpeakersSubCommand;
import com.craftmend.openaudiomc.spigot.modules.speakers.utils.SpeakerUtils;
import lombok.SneakyThrows;
import org.bukkit.entity.Player;

public class SpeakerGiveSubCommand extends SubCommand {
Expand All @@ -18,6 +21,7 @@ public SpeakerGiveSubCommand(SpeakersSubCommand parent) {
}

@Override
@SneakyThrows
public void onExecute(User sender, String[] args) {
if (!(sender.getOriginal() instanceof Player)) {
message(sender, "Only players can receive a speaker item.");
Expand All @@ -29,6 +33,10 @@ public void onExecute(User sender, String[] args) {
radius = Integer.valueOf(args[1]);
}

if (Validation.isStringInvalid(args[0])) {
throw new CommandError("Invalid source url.");
}

Player player = (Player) sender.getOriginal();
player.getInventory().addItem(SpeakerUtils.getSkull(OpenAudioMc.getService(MediaService.class).process(args[0]), radius));
message(sender, "Speaker media created! You've received a Speaker skull in your inventory. Placing it anywhere in the world will add the configured sound in the are.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void saveAll(boolean includeConfig) {
if (includeConfig) {
mainConfig.save(new File(OpenAudioMcSpigot.getInstance().getDataFolder(), "config.yml"));
}
dataConfig.save("plugins/OpenAudioMc/data.yml");
dataConfig.save("plugins"+ File.pathSeparator + "OpenAudioMc" + File.pathSeparator + "data.yml");
} catch (IOException e) {
OpenAudioLogger.handleException(e);
e.printStackTrace();
Expand All @@ -298,7 +298,7 @@ public void overwriteConfigFile() {

@Override
public boolean hasDataFile() {
File dataFile = new File("plugins/OpenAudioMc/data.yml");
File dataFile = new File("plugins" + File.pathSeparator + "OpenAudioMc" + File.pathSeparator + "data.yml");
return dataFile.exists();
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/resources/data.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BUILD_NUM="1063"
BUILD_NUM="1067"
4 changes: 2 additions & 2 deletions plugin/src/main/resources/openaudiomc-build.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BUILD_VERSION="1063"
BUILD_COMMIT="e67dacf726aa8734e913de2babe6e8c711e77023"
BUILD_VERSION="1067"
BUILD_COMMIT="230b9271da8d2e6945e2c7604cffb036a6be048c"
BUILD_AUTHOR="Mats"

0 comments on commit add57d3

Please sign in to comment.