add syncing as a config option/command

This commit is contained in:
ayunami2000 2022-04-25 10:27:36 -04:00
parent 6048af89f3
commit 52d0540c3a
4 changed files with 38 additions and 1 deletions

View File

@ -25,6 +25,8 @@ public class Main extends JavaPlugin implements CommandExecutor, Listener {
private final int[] mapSize = {1, 1};
private int mapSizeCap = 10;
private int mapOffset = 0;
private int interval = 10;
private int syncTask = -1;
@Override
public void onLoad(){
@ -44,6 +46,17 @@ public class Main extends JavaPlugin implements CommandExecutor, Listener {
sendToAllPlayers(videoMapCodec.disableVideoBukkit());
}
private void stopSyncTask() {
if (syncTask != -1) {
this.getServer().getScheduler().cancelTask(syncTask);
syncTask = -1;
}
}
private void createSyncTask() {
if (interval > 0) syncTask = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, this::syncToAllPlayers, 0, interval * 20L);
}
private void rlConfig() {
MessageHandler.initMessages();
audioLoc.setX(this.getConfig().getDouble("audio.x"));
@ -53,6 +66,9 @@ public class Main extends JavaPlugin implements CommandExecutor, Listener {
mapOffset = this.getConfig().getInt("offset");
setSize(this.getConfig().getInt("size.width"), this.getConfig().getInt("size.height"));
url = this.getConfig().getString("url");
interval = this.getConfig().getInt("interval");
stopSyncTask();
createSyncTask();
}
private void syncToPlayer(Player player) {
@ -205,6 +221,22 @@ public class Main extends JavaPlugin implements CommandExecutor, Listener {
this.saveConfig();
MessageHandler.sendPrefixedMessage(sender, "setSize", mapSize[0], mapSize[1], mapSize[0] * mapSize[1]);
break;
case "i":
case "int":
case "interval":
if (args.length < 2) {
MessageHandler.sendPrefixedMessage(sender, "currentInterval", interval);
break;
}
try {
interval = Math.max(0, Integer.parseInt(args[1]));
stopSyncTask();
createSyncTask();
MessageHandler.sendPrefixedMessage(sender, "setInterval");
} catch (NumberFormatException e) {
MessageHandler.sendPrefixedMessage(sender, "notANumber", args[1], "integer");
}
break;
default:
MessageHandler.sendPrefixedMessage(sender, "invalidUsage");
}

View File

@ -8,6 +8,8 @@ size:
cap: 10
# map ID offset
offset: 0
# sync interval, in seconds
interval: 10
# audio location
audio:
x: 0

View File

@ -7,6 +7,7 @@ usage:
- "&7&o/ayunvid <p|play|pause> [force] &3- Toggle playback of the video. This also loads the new video after the video URL is changed. Optionally force a reload of the video."
- "&7&o/ayunvid <s|size> [<width> <height>] &3- Sets or gets the size of video, in maps. E.g., a width of 2 and a height of 3 would result in a video using 6 maps."
- "&7&o/ayunvid <rl|reload> &3- Reloads the configuration file."
- "&7&o/ayunvid <i|int|interval> &3- Set the sync interval, in seconds, or 0 to disable."
currentUrl: "&3Current URL: {0}"
setUrl: "&3Successfully set URL."
locFromConsole: "&cError: You must specify the coordinates when running this command from the console!"
@ -22,3 +23,5 @@ currentSize: "&3Current size: {0} maps wide by {1} maps tall ({2} maps total)"
setSize: "&3Set size to {0} maps wide by {1} maps tall ({2} maps total)"
invalidUsage: "&cError: That is not a valid subcommand! Try &n/ayunvid&c help for usage."
reloaded: "&3Successfully reloaded configuration!"
currentInterval: "&3Current sync interval: {0} seconds"
setInterval: "&3Successfully set or disabled sync interval."