mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-12-22 20:41:05 +01:00
feat: finish locking
This commit is contained in:
parent
76477aa3f8
commit
4b14501c7c
5 changed files with 54 additions and 9 deletions
|
@ -1,5 +1,6 @@
|
|||
package dev.xhyrom.lighteco.common.command;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.ParseResults;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
|
@ -18,16 +19,23 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
|||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class CommandManager {
|
||||
protected final LightEcoPlugin plugin;
|
||||
private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
|
||||
.setDaemon(true)
|
||||
.setNameFormat("lighteco-command-executor")
|
||||
.build()
|
||||
);
|
||||
|
||||
@Getter
|
||||
private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
|
||||
|
||||
@Getter
|
||||
private final List<UUID> locks = new CopyOnWriteArrayList<>();
|
||||
private final Set<UUID> locks = ConcurrentHashMap.newKeySet();
|
||||
private final Map<UUID, UUID> locksMappings = new ConcurrentHashMap<>();
|
||||
|
||||
public CommandManager(LightEcoPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
|
@ -67,13 +75,30 @@ public class CommandManager {
|
|||
|
||||
locks.add(sender.getUniqueId());
|
||||
|
||||
this.plugin.getBootstrap().getScheduler().async().execute(() -> {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
dispatcher.execute(parseResults);
|
||||
} catch (CommandSyntaxException e) {
|
||||
this.sendError(sender, name, e);
|
||||
} finally {
|
||||
this.plugin.getBootstrap().getLogger().debug("Removing lock for " + sender.getUsername());
|
||||
|
||||
UUID target = locksMappings.get(sender.getUniqueId());
|
||||
if (target != null) {
|
||||
locks.remove(target);
|
||||
locksMappings.remove(sender.getUniqueId());
|
||||
|
||||
this.plugin.getBootstrap().getLogger().debug("Removing lock caused by " + sender.getUsername() + " for " + target);
|
||||
}
|
||||
|
||||
locks.remove(sender.getUniqueId());
|
||||
}
|
||||
});
|
||||
}, executor);
|
||||
}
|
||||
|
||||
public void lockBySender(CommandSender sender, UUID target) {
|
||||
locks.add(target);
|
||||
locksMappings.put(sender.getUniqueId(), target);
|
||||
}
|
||||
|
||||
private void sendError(CommandSender sender, String name, CommandSyntaxException e) {
|
||||
|
|
|
@ -23,7 +23,7 @@ public class OfflineUserArgument implements ArgumentType<String> {
|
|||
}
|
||||
|
||||
// Lock the user to prevent race conditions
|
||||
plugin.getCommandManager().getLocks().add(uniqueId);
|
||||
plugin.getCommandManager().lockBySender(context.getSource().sender(), uniqueId);
|
||||
|
||||
return plugin.getUserManager().loadUser(uniqueId).join();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,9 @@ public interface PluginLogger {
|
|||
void info(String message);
|
||||
void info(String message, Object ...args);
|
||||
|
||||
void debug(String message);
|
||||
void debug(String message, Object ...args);
|
||||
|
||||
void warn(String message);
|
||||
void warn(String message, Object ...args);
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ public class PaperLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstra
|
|||
|
||||
public PaperLightEcoBootstrap(JavaPlugin loader) {
|
||||
this.loader = loader;
|
||||
this.logger = new PaperLogger(loader.getLogger());
|
||||
this.logger = new PaperLogger(this.plugin, loader.getLogger());
|
||||
this.scheduler = new PaperSchedulerAdapter(this);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package dev.xhyrom.lighteco.paper.logger;
|
||||
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class PaperLogger implements PluginLogger {
|
||||
private final LightEcoPlugin plugin;
|
||||
private final Logger logger;
|
||||
|
||||
public PaperLogger(Logger logger) {
|
||||
public PaperLogger(LightEcoPlugin plugin, Logger logger) {
|
||||
this.plugin = plugin;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
@ -22,6 +25,20 @@ public class PaperLogger implements PluginLogger {
|
|||
this.logger.info(String.format(message, args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String message) {
|
||||
if (!this.plugin.getConfig().debug) return;
|
||||
|
||||
info(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String message, Object... args) {
|
||||
if (!this.plugin.getConfig().debug) return;
|
||||
|
||||
info(message, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String message) {
|
||||
this.logger.log(Level.WARNING, message);
|
||||
|
|
Loading…
Reference in a new issue