Skip to content

Commit

Permalink
Improvements about the auto sync system
Browse files Browse the repository at this point in the history
  • Loading branch information
efekurbann committed May 2, 2022
1 parent 56a8088 commit 7edd0d4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.efekurbann.synccommands;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.github.efekurbann.synccommands.config.Config;
import io.github.efekurbann.synccommands.enums.ConnectionType;
import io.github.efekurbann.synccommands.executor.impl.BungeeExecutor;
Expand Down Expand Up @@ -30,7 +32,7 @@ public final class SyncCommandsBungee extends Plugin {
private final Scheduler scheduler = new BungeeScheduler(this);
private final Map<String, Server> servers = new HashMap<>();
private final Map<String, Set<Server>> groups = new HashMap<>();
private final Map<UUID, String> autoSyncMode = new HashMap<>();
private final Cache<UUID, String> autoSyncMode = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
private Server server;
private Messaging messaging;
private boolean connectedSuccessfully;
Expand Down Expand Up @@ -196,7 +198,7 @@ public Map<String, Set<Server>> getGroups() {
return groups;
}

public Map<UUID, String> getAutoSyncMode() {
public Cache<UUID, String> getAutoSyncMode() {
return autoSyncMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void execute(CommandSender sender, String[] args) {
return;
}

if (!plugin.getAutoSyncMode().containsKey(player.getUniqueId())) {
if (!plugin.getAutoSyncMode().asMap().containsKey(player.getUniqueId())) {
plugin.getAutoSyncMode().put(player.getUniqueId(), target);
sender.sendMessage(TextComponent.fromLegacyText(
ChatUtils.color(String.format("&aSuccessfully enabled the auto sync mode for %s!", target)))
Expand All @@ -47,7 +47,7 @@ public void execute(CommandSender sender, String[] args) {
ChatUtils.color("&aFrom now on, all the commands that you execute will be synced."))
);
} else {
plugin.getAutoSyncMode().remove(player.getUniqueId());
plugin.getAutoSyncMode().invalidate(player.getUniqueId());
sender.sendMessage(TextComponent.fromLegacyText(ChatUtils.color("&cDisabled the auto sync mode!")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public void onCommand(ChatEvent event) {
String command = event.getMessage().replace("/", "");

ProxiedPlayer sender = (ProxiedPlayer) event.getSender();
if (!plugin.getAutoSyncMode().containsKey(sender.getUniqueId())) return;
if (!plugin.getAutoSyncMode().asMap().containsKey(sender.getUniqueId())) return;
if (command.startsWith("bsync") || command.startsWith("bungeesync") || command.startsWith("syncbungee")) return;

String target = plugin.getAutoSyncMode().get(sender.getUniqueId());
String target = plugin.getAutoSyncMode().getIfPresent(sender.getUniqueId());

event.setMessage(String.format("/bsync %s %s", target, command));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.efekurbann.synccommands;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.github.efekurbann.synccommands.command.SyncCommand;
import io.github.efekurbann.synccommands.config.Config;
import io.github.efekurbann.synccommands.enums.ConnectionType;
Expand All @@ -25,14 +27,16 @@
import io.github.efekurbann.synccommands.scheduler.Scheduler;

import java.util.*;
import java.util.concurrent.TimeUnit;

public final class SyncCommandsSpigot extends JavaPlugin {

private final Config config = new Config(this, "config.yml");
private final ConsoleExecutor consoleExecutor = new BukkitExecutor(this);
private final Map<String, Server> servers = new HashMap<>();
private final Map<String, Set<Server>> groups = new HashMap<>();
private final Map<UUID, String> autoSyncMode = new HashMap<>();
// we will store the names instead of uuids
private final Cache<String, String> autoSyncMode = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
private final Scheduler scheduler = new BukkitScheduler(this);
private UpdateChecker updateChecker;
private Messaging messaging;
Expand Down Expand Up @@ -216,7 +220,7 @@ public Map<String, Set<Server>> getGroups() {
return groups;
}

public Map<UUID, String> getAutoSyncMode() {
public Cache<String, String> getAutoSyncMode() {
return autoSyncMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.efekurbann.synccommands.objects.server.Server;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import io.github.efekurbann.synccommands.SyncCommandsSpigot;
import io.github.efekurbann.synccommands.objects.Command;
Expand All @@ -26,8 +25,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
return true;
}

if (args.length == 1 && sender instanceof Player) {
Player player = (Player) sender;
if (args.length == 1) {
String target = args[0];
if (!plugin.getGroups().containsKey(target)
&& !plugin.getServers().containsKey(target)
Expand All @@ -36,8 +34,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
return true;
}

if (!plugin.getAutoSyncMode().containsKey(player.getUniqueId())) {
plugin.getAutoSyncMode().put(player.getUniqueId(), target);
if (!plugin.getAutoSyncMode().asMap().containsKey(sender.getName())) {
plugin.getAutoSyncMode().put(sender.getName(), target);
sender.sendMessage(
ChatUtils.color(String.format("&aSuccessfully enabled the auto sync mode for %s!", target))
);
Expand All @@ -53,7 +51,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
"we can not detect that. And it wont be synced.")
);
} else {
plugin.getAutoSyncMode().remove(player.getUniqueId());
plugin.getAutoSyncMode().invalidate(sender.getName());
sender.sendMessage(ChatUtils.color("&cDisabled the auto sync mode!"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.efekurbann.synccommands.listener;

import io.github.efekurbann.synccommands.SyncCommandsSpigot;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;

public class CommandListener implements Listener {

Expand All @@ -19,13 +21,27 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
String command = event.getMessage().replace("/", "");

Player player = event.getPlayer();
if (!plugin.getAutoSyncMode().containsKey(player.getUniqueId())) return;
if (!plugin.getAutoSyncMode().asMap().containsKey(player.getName())) return;
if (command.startsWith("bsync") || command.startsWith("bungeesync") || command.startsWith("syncbungee")
|| command.startsWith("sync") || command.startsWith("ssync")) return;

String target = plugin.getAutoSyncMode().get(player.getUniqueId());
String target = plugin.getAutoSyncMode().getIfPresent(player.getName());

event.setMessage(String.format("/sync %s %s", target, command));
}

@EventHandler
public void onCommand(ServerCommandEvent event) {
String command = event.getCommand().replace("/", "");

CommandSender sender = event.getSender();
if (!plugin.getAutoSyncMode().asMap().containsKey(sender.getName())) return;
if (command.startsWith("bsync") || command.startsWith("bungeesync") || command.startsWith("syncbungee")
|| command.startsWith("sync") || command.startsWith("ssync")) return;

String target = plugin.getAutoSyncMode().getIfPresent(sender.getName());

event.setCommand(String.format("sync %s %s", target, command));
}

}

1 comment on commit 7edd0d4

@efekurbann
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Now supports console command sender (only for spigot)
  • Mode will be disabled after 5 minutes

I guess it is not possible to listen the commands sent from console on Bungeecord.
If you know a way, create a PR.

Please sign in to comment.