io.github.dailystruggle.rtp.common.database.DatabaseAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of RTP Show documentation
Show all versions of RTP Show documentation
a random teleport plugin
package io.github.dailystruggle.rtp.common.database;
import io.github.dailystruggle.commandsapi.common.CommandsAPI;
import io.github.dailystruggle.rtp.common.RTP;
import io.github.dailystruggle.rtp.common.playerData.TeleportData;
import io.github.dailystruggle.rtp.common.selection.region.Region;
import io.github.dailystruggle.rtp.common.serverSide.substitutions.RTPCommandSender;
import io.github.dailystruggle.rtp.common.serverSide.substitutions.RTPLocation;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
/**
*
*/
public abstract class DatabaseAccessor {
public final AtomicBoolean stop = new AtomicBoolean(false);
protected Map> localTables = new ConcurrentHashMap<>();
/**
* pipelines to reduce overhead from opening/closing files and connections
*/
protected long avgTimeRead = 0;
protected long avgTimeWrite = 0;
protected ConcurrentLinkedQueue, CompletableFuture>>>>> readQueue = new ConcurrentLinkedQueue<>();
protected ConcurrentLinkedQueue>> writeQueue = new ConcurrentLinkedQueue<>();
public static Map toColumns(Object obj) {
Map res = new HashMap<>();
if (obj instanceof TableObj) {
obj = ((TableObj) obj).object;
}
if (obj instanceof Map.Entry) {
Map.Entry, ?> entry = (Map.Entry, ?>) obj;
res.put(entry.getKey().toString(), entry.getValue());
} else if (obj instanceof Map) {
Map, ?> map = (Map, ?>) obj;
map.forEach((o, o2) -> res.put(o.toString(), o2));
} else if (obj instanceof TeleportData) {
TeleportData teleportData = (TeleportData) obj;
RTPCommandSender sender = teleportData.sender;
RTPLocation selectedLocation = teleportData.selectedLocation;
if (selectedLocation == null)
selectedLocation = new RTPLocation(RTP.serverAccessor.getRTPWorlds().get(0), 0, 0, 0);
RTPLocation originalLocation = teleportData.originalLocation;
if (originalLocation == null)
originalLocation = new RTPLocation(RTP.serverAccessor.getRTPWorlds().get(0), 0, 0, 0);
Region targetRegion = teleportData.targetRegion;
if (targetRegion == null) targetRegion = RTP.selectionAPI.getRegion("default");
if (sender != null) {
res.put("senderName", sender.name());
res.put("senderId", sender.uuid().toString());
} else {
res.put("senderName", "console");
res.put("senderId", CommandsAPI.serverId);
}
res.put("time", teleportData.time);
res.put("delay", teleportData.delay);
res.put("selectedX", selectedLocation.x());
res.put("selectedY", selectedLocation.y());
res.put("selectedZ", selectedLocation.z());
res.put("selectedWorldName", selectedLocation.world().name());
res.put("selectedWorldId", selectedLocation.world().id().toString());
res.put("originalX", originalLocation.x());
res.put("originalY", originalLocation.y());
res.put("originalZ", originalLocation.z());
res.put("originalWorldName", originalLocation.world().name());
res.put("originalWorldId", originalLocation.world().id().toString());
res.put("region", targetRegion.name);
res.put("cost", teleportData.cost);
res.put("attempts", teleportData.attempts);
}
return res;
}
/**
* @return what sort of database is this?
*/
public abstract String name();
@NotNull
protected File getMainDirectory() {
return RTP.configs.pluginDirectory;
}
@NotNull
protected CompletableFuture>> getTable(String tableName) {
if (!localTables.containsKey(tableName)) return CompletableFuture.completedFuture(Optional.empty());
Map table = localTables.get(tableName);
if (table == null) return CompletableFuture.completedFuture(Optional.empty());
return CompletableFuture.completedFuture(Optional.of(table));
}
@NotNull
public Optional>> getValue(String table, Object key) {
Map map = localTables.get(table);
if (map == null) return Optional.empty();
TableObj tableKey = new TableObj(key);
TableObj tableValue = map.get(tableKey);
if (tableValue == null) return Optional.empty();
return Optional.of(CompletableFuture.completedFuture(Optional.of(tableValue.object)));
}
@NotNull
public CompletableFuture> getValue(String table, Object key, Object def) {
Optional>> res = getValue(table, key);
Optional