Added sync command
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package solutions.reslate.entertainment.spigotresourcesync;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
|
||||
public class CommandController implements CommandExecutor {
|
||||
private Set<String> names;
|
||||
private Map<String, Command> commands;
|
||||
|
||||
|
||||
public CommandController() {
|
||||
super();
|
||||
names = new HashSet<>();
|
||||
names.add("resourcesync");
|
||||
names.add("ressync");
|
||||
commands = new HashMap<>();
|
||||
}
|
||||
|
||||
public void registerCommand(Command command) {
|
||||
if (command == null) throw new IllegalArgumentException("Command cannot be null!");
|
||||
this.commands.put(command.getName(), command);
|
||||
for (String alias : command.getAliases()) {
|
||||
this.commands.put(alias, command);
|
||||
}
|
||||
}
|
||||
|
||||
public void deregisterCommand(Command command) {
|
||||
this.deregisterCommand(command.getName());
|
||||
}
|
||||
|
||||
public void deregisterCommand(String commandName) {
|
||||
if (commandName == null || commandName.isBlank()) throw new IllegalArgumentException("Command name cannot be null or blank.");
|
||||
Command removed = this.commands.remove(commandName);
|
||||
for (String alias : removed.getAliases()) {
|
||||
this.commands.remove(alias);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (args.length == 0) return false;
|
||||
if (commands.containsKey(args[0])) {
|
||||
Command subcommand = commands.get(args[0]);
|
||||
return subcommand.execute(sender, label, Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -5,12 +5,14 @@ import java.io.File;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import solutions.reslate.entertainment.spigotresourcesync.commands.SyncCommand;
|
||||
import solutions.reslate.entertainment.spigotresourcesync.serialisation.JacksonYamlSerialiser;
|
||||
import solutions.reslate.entertainment.spigotresourcesync.synchronisation.ApacheCommonsIOSynchroniser;
|
||||
|
||||
public class SpigotResourceSync extends JavaPlugin {
|
||||
private ConfigManager configManager;
|
||||
private SyncListManager syncListManager;
|
||||
private CommandController commandController;
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
@@ -26,11 +28,15 @@ public class SpigotResourceSync extends JavaPlugin {
|
||||
if (configManager.getConfiguration().getSyncOnLoad()) {
|
||||
syncListManager.synchroniseAllSyncList();
|
||||
}
|
||||
commandController = new CommandController();
|
||||
commandController.registerCommand(new SyncCommand(syncListManager));
|
||||
super.onLoad();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getLogger().info(getName());
|
||||
getCommand(getName()).setExecutor(commandController);
|
||||
super.onEnable();
|
||||
}
|
||||
|
||||
@@ -48,6 +54,6 @@ public class SpigotResourceSync extends JavaPlugin {
|
||||
public void saveDefaultConfig() {
|
||||
configManager.resetConfiguration();
|
||||
configManager.flush();
|
||||
super.saveDefaultConfig();
|
||||
}
|
||||
|
||||
}
|
@@ -36,6 +36,21 @@ public class SyncListManager implements ObjectLoadListener<Configuration> {
|
||||
logger.info("Done synchronising.");
|
||||
}
|
||||
|
||||
public void synchroniseGroupSyncList(String group) {
|
||||
logger.info("Synchronising group sync pairs...");
|
||||
for (SyncPair syncPair : this.configuration.getSyncList().gatherGroupSyncPairs(group)) {
|
||||
File source = new File(syncPair.getSource());
|
||||
File dest = new File(syncPair.getDestination());
|
||||
try {
|
||||
synchroniser.sync(source, dest);
|
||||
logger.info(String.format("Synchronised \"%s\" to \"%s\"!", source.getName(), dest.getName()));
|
||||
} catch (IOException e) {
|
||||
logger.warning(String.format("Failed to synchronise \"%s\" to \"%s\". %s", source.getAbsolutePath(), dest.getAbsolutePath(), e.getMessage()));
|
||||
}
|
||||
}
|
||||
logger.info("Done synchronising.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void objectLoaded(Configuration obj) {
|
||||
logger.info("Updating synchronisation list due to recently loading configuration...");
|
||||
|
@@ -0,0 +1,38 @@
|
||||
package solutions.reslate.entertainment.spigotresourcesync.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import solutions.reslate.entertainment.spigotresourcesync.SyncListManager;
|
||||
|
||||
public class SyncCommand extends Command {
|
||||
private SyncListManager syncListManager;
|
||||
|
||||
|
||||
public SyncCommand(SyncListManager syncListManager) {
|
||||
super(
|
||||
"sync",
|
||||
"Synchronise based off your configurations.",
|
||||
"sync [sync group name]",
|
||||
new ArrayList<>()
|
||||
);
|
||||
this.syncListManager = syncListManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
||||
if (args.length > 1) return false;
|
||||
if (args.length == 1) {
|
||||
String groupName = args[0];
|
||||
syncListManager.synchroniseGroupSyncList(groupName);
|
||||
sender.sendMessage(String.format("Synchronisation of \"%s\" complete!", groupName));
|
||||
} else {
|
||||
syncListManager.synchroniseAllSyncList();
|
||||
sender.sendMessage("Synchronisation of all items complete!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -2,6 +2,7 @@ package solutions.reslate.entertainment.spigotresourcesync.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -17,12 +18,17 @@ public class SyncList implements Serializable {
|
||||
this.syncPairs = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public List<SyncPair> gatherAllSyncPairs() {
|
||||
public SequencedCollection<SyncPair> gatherAllSyncPairs() {
|
||||
List<SyncPair> allSyncPairs = new ArrayList<>();
|
||||
for (SequencedCollection<SyncPair> syncPairs : this.syncPairs.values()) {
|
||||
allSyncPairs.addAll(syncPairs);
|
||||
}
|
||||
return allSyncPairs;
|
||||
return Collections.unmodifiableSequencedCollection(allSyncPairs);
|
||||
}
|
||||
|
||||
public SequencedCollection<SyncPair> gatherGroupSyncPairs(String groupName) {
|
||||
if (!this.syncPairs.containsKey(groupName)) throw new IllegalArgumentException(String.format("\"%s\" group does not exist!", groupName));
|
||||
return Collections.unmodifiableSequencedCollection(this.syncPairs.get(groupName));
|
||||
}
|
||||
|
||||
public void addSynchronisationPair(String source, String dest, String group) {
|
||||
@@ -52,8 +58,8 @@ public class SyncList implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> gatherSynchronisationGroups() {
|
||||
return new ArrayList<>(this.syncPairs.keySet());
|
||||
public SequencedCollection<String> gatherSynchronisationGroups() {
|
||||
return Collections.unmodifiableSequencedCollection(new ArrayList<>(this.syncPairs.keySet()));
|
||||
}
|
||||
|
||||
public SequencedMap<String, SequencedCollection<SyncPair>> getSyncPairs() {
|
||||
|
@@ -1,4 +1,9 @@
|
||||
name: SpigotResourceSync
|
||||
version: 1.0.0
|
||||
main: solutions.reslate.entertainment.spigotresourcesync.SpigotResourceSync
|
||||
api-version: '1.20'
|
||||
api-version: '1.20'
|
||||
|
||||
commands:
|
||||
spigotresourcesync:
|
||||
description: Tool suite for synchronisation resources across directories.
|
||||
usage: See "/spigotresourcesync help" for help.
|
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -27,6 +28,6 @@ public class TestConfigManager {
|
||||
assumeTrue(dummyConfig.exists());
|
||||
ConfigManager finalConfigManager = new ConfigManager(dummyConfig, dummySerialiser, logger);
|
||||
finalConfigManager.load();
|
||||
assertEquals(dummySyncPair, finalConfigManager.getConfiguration().getSyncList().gatherAllSyncPairs().get(0));
|
||||
assertEquals(dummySyncPair, new ArrayList<>(finalConfigManager.getConfiguration().getSyncList().gatherAllSyncPairs()).get(1));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,17 @@
|
||||
package solutions.reslate.entertainment.spigotresourcesync.data;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestSyncList {
|
||||
@Test
|
||||
void testGatherSyncGroup() {
|
||||
SyncList dummyList = new SyncList();
|
||||
dummyList.addSynchronisationPair("abc", "def", "a");
|
||||
dummyList.addSynchronisationPair("ghi", "jkl", "a");
|
||||
dummyList.addSynchronisationPair("123", "456", "b");
|
||||
assertEquals(new SyncPair("abc", "def"), dummyList.gatherGroupSyncPairs("a").getFirst());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user