Initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DataLock implements DataLockInterface {
|
||||
|
||||
private static DataLock instance = null;
|
||||
|
||||
public static DataLock getInstance() {
|
||||
if (instance == null)
|
||||
instance = new DataLock();
|
||||
return instance;
|
||||
}
|
||||
|
||||
private DataLock() {}
|
||||
|
||||
private final HashMap<UUID, IdempotencyData> activeQueries = new HashMap<>();
|
||||
|
||||
protected void putQuery(IdempotencyData idempotencyData) {
|
||||
activeQueries.put(idempotencyData.idempotencyToken(), idempotencyData);
|
||||
}
|
||||
|
||||
protected IdempotencyData getQuery(UUID idempotencyToken) {
|
||||
return activeQueries.getOrDefault(idempotencyToken, null);
|
||||
}
|
||||
|
||||
protected IdempotencyData removeQuery(UUID idempotencyToken) {
|
||||
return activeQueries.remove(idempotencyToken);
|
||||
}
|
||||
|
||||
private final HashSet<String> activeChannels = new HashSet<>();
|
||||
|
||||
public void registerChannel(String channel) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tryLock(String channel, String data) {
|
||||
IdempotencyData idempotencyData = new IdempotencyData(channel, data, UUID.randomUUID());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tryUnlock(String channel, String data) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public interface DataLockInterface {
|
||||
|
||||
void tryLock(String channel, String data);
|
||||
|
||||
void tryUnlock(String channel, String data);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class DataLockLib extends JavaPlugin {
|
||||
|
||||
public static DataLockLib instance;
|
||||
|
||||
protected static DataLockLib getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
registerEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
||||
}
|
||||
|
||||
private void registerEvents() {
|
||||
// getServer().getPluginManager().registerEvents(new TalkToQuest(), this);
|
||||
// getServer().getMessenger().registerOutgoingPluginChannel(this, "aquest:player-data");
|
||||
// getServer().getMessenger().registerIncomingPluginChannel(this, "aquest:player-data", new PluginMessageListener());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class DataLockListener implements Listener {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import java.util.UUID;
|
||||
|
||||
record IdempotencyData(String channel, String data, UUID idempotencyToken) {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Channel: [" + channel + "] Data: [" + data + "] Idempotency Token: [" + idempotencyToken + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String channel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID idempotencyToken() {
|
||||
return idempotencyToken;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class LockResponseEvent extends Event {
|
||||
|
||||
private final HandlerList handlers = new HandlerList();
|
||||
private final String channel;
|
||||
private final String data;
|
||||
private final boolean result;
|
||||
|
||||
protected LockResponseEvent(boolean isAsync, String channel, ResponseType responseType, String data, boolean result) {
|
||||
super(isAsync);
|
||||
this.channel = channel;
|
||||
this.data = data;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PluginMessageListener implements org.bukkit.plugin.messaging.PluginMessageListener {
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] bytes) {
|
||||
if (!activeChannels.contains(channel)) {
|
||||
return;
|
||||
}
|
||||
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
|
||||
String data = in.readUTF();
|
||||
boolean result = in.readBoolean();
|
||||
UUID idempotency = UUID.fromString(in.readUTF());
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
switch (in.readUTF()) {
|
||||
case "try-lock-result" -> new LockResponseEvent(true, channel, ResponseType.TRY_LOCK_RESULT, data, result);
|
||||
case "queue-lock-failed" -> new LockResponseEvent(true, channel, ResponseType.QUEUE_LOCK_FAILED, data, result);
|
||||
case "try-unlock-result" -> new LockResponseEvent(true, channel, ResponseType.TRY_UNLOCK_RESULT, data, result);
|
||||
case "locked-queue-lock" -> new LockResponseEvent(true, channel, ResponseType.LOCKED_QUEUE_LOCK, data, result);
|
||||
case "check-lock-result" -> new LockResponseEvent(true, channel, ResponseType.CHECK_LOCK_RESULT, data, result);
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(DataLockLib.getInstance());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public enum ResponseType {
|
||||
TRY_LOCK_RESULT,
|
||||
QUEUE_LOCK_FAILED,
|
||||
TRY_UNLOCK_RESULT,
|
||||
LOCKED_QUEUE_LOCK,
|
||||
CHECK_LOCK_RESULT
|
||||
}
|
||||
Reference in New Issue
Block a user