Removed the requirement of loading all QueueData

This commit is contained in:
RonanCraft
2022-10-26 14:31:37 -04:00
parent ba5c0a4717
commit 865a6cead9
5 changed files with 26 additions and 23 deletions

View File

@@ -157,7 +157,7 @@ public class CmdInfo implements RTPCommand, RTPCommandHelpable {
info.add("&7- &6Biomes&7: &f" + _rtpworld.getBiomes().toString());
info.add("&7- &eShape&7: &f" + _rtpworld.getShape().toString() + getInfo(_rtpworld, worldDefault, "shape"));
info.add("&7- &6Permission Group&7: " + (_rtpworld.getConfig() != null ? "&a" + _rtpworld.getConfig().getGroupName() : "&cN/A"));
info.add("&7- &eQueue Available&7: " + QueueHandler.getApplicableAsync(null, _rtpworld).size());
info.add("&7- &eQueue Available&7: " + QueueHandler.getApplicableAsync(_rtpworld).size());
}
return info;
}

View File

@@ -58,7 +58,7 @@ public class CmdQueue implements RTPCommand {
info.add("&eWorld: &6" + world.getName());
RTPSetupInformation setup_info = new RTPSetupInformation(HelperRTP.getActualWorld(player, world), player, player, true);
WorldPlayer pWorld = HelperRTP.getPlayerWorld(setup_info);
for (QueueData queue : QueueHandler.getApplicableAsync(null, pWorld)) {
for (QueueData queue : QueueHandler.getApplicableAsync(pWorld)) {
String str = "&8- &7x= &b%x, &7z= &b%z";
Location loc = queue.getLocation();
str = str.replace("%x", String.valueOf(loc.getBlockX())).replace("%z", String.valueOf(loc.getBlockZ()));

View File

@@ -47,7 +47,7 @@ public class DatabaseQueue extends SQLite {
}
}
public List<QueueData> getAll() {
/*public List<QueueData> getAll() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
@@ -65,7 +65,7 @@ public class DatabaseQueue extends SQLite {
long generated = rs.getLong(COLUMNS.GENERATED.name);
World world = Bukkit.getWorld(worldName);
if (world != null) {
queueDataList.add(new QueueData(new Location(world, x, 69 /*giggity*/, z), generated, id));
queueDataList.add(new QueueData(new Location(world, x, 69, z), generated, id));
}
}
} catch (SQLException ex) {
@@ -74,7 +74,7 @@ public class DatabaseQueue extends SQLite {
close(ps, rs, conn);
}
return queueDataList;
}
}*/
public List<QueueData> getInRange(QueueRangeData range) {
Connection conn = null;
@@ -85,9 +85,10 @@ public class DatabaseQueue extends SQLite {
conn = getSQLConnection();
//ps = conn.prepareStatement("SELECT * FROM " + tables.get(0) + " WHERE ? BETWEEN ? AND ? AND ? BETWEEN ? AND ?");
ps = conn.prepareStatement("SELECT * FROM " + tables.get(0) + " WHERE "
+ COLUMNS.WORLD.name + " = '" + range.getWorld().getName() + "' AND "
+ COLUMNS.X.name + " BETWEEN " + range.getXLow() + " AND " + range.getXHigh()
+ " AND " + COLUMNS.Z.name + " BETWEEN " + range.getZLow() + " AND " + range.getZHigh()
+ " ORDER BY " + COLUMNS.ID.name + " DESC LIMIT " + (QueueGenerator.queueMax + 1)
+ " ORDER BY RANDOM() LIMIT " + (QueueGenerator.queueMax + 1)
);
/*ps.setString(1, COLUMNS.X.name);
ps.setInt(2, range.getXLow());
@@ -205,12 +206,14 @@ public class DatabaseQueue extends SQLite {
@Getter int xLow, xHigh;
@Getter int zLow, zHigh;
@Getter World world;
public QueueRangeData(RTPWorld rtpWorld) {
this.xLow = rtpWorld.getCenterX() - rtpWorld.getMaxRadius();
this.xHigh = rtpWorld.getCenterX() + rtpWorld.getMaxRadius();
this.zLow = rtpWorld.getCenterZ() - rtpWorld.getMaxRadius();
this.zHigh = rtpWorld.getCenterZ() + rtpWorld.getMaxRadius();
this.world = rtpWorld.getWorld();
}
}

View File

@@ -51,18 +51,18 @@ public class QueueGenerator {
//BetterRTP.debug("Loaded " + queueList.size() + " previously generated safe locations!");
//Queue after everything was loaded
BetterRTP.debug("Attempting to queue up some more safe locations...");
queueGenerator(new ReQueueData(DatabaseHandler.getQueue().getAll(), rtpWorld, queueMax, queueMin, 0, "noone", 0));
queueGenerator(new ReQueueData(rtpWorld, queueMax, queueMin, 0, "noone", 0));
}, 10L);
}
private void queueGenerator(ReQueueData data) {
generating = true;
task = Bukkit.getScheduler().runTaskLaterAsynchronously(BetterRTP.getInstance(), () -> {
//BetterRTP.debug("Generating a new position... attemp # " + data.attempts);
//BetterRTP.debug("Generating a new position... attempt # " + data.attempts);
//Generate more locations
//Rare cases where a rtp world didn't have a location generated (Permission Groups?)
if (data.rtpWorld != null) {
List<QueueData> applicable = QueueHandler.getApplicableAsync(data.queue, data.rtpWorld);
List<QueueData> applicable = QueueHandler.getApplicableAsync(data.rtpWorld);
String type = "rtp_" + (data.rtpWorld.getID() != null ? data.rtpWorld.getID() : data.rtpWorld.getWorld().getName());
int newCount = data.lastType.equalsIgnoreCase(type) ? data.lastCount : applicable.size();
int attempt = data.lastType.equalsIgnoreCase(type) ? data.attempts + 1: 0;
@@ -72,7 +72,7 @@ public class QueueGenerator {
return;
}
addQueue(data.rtpWorld, type, new ReQueueData(data.queue, data.rtpWorld, queueMax, queueMin, newCount, type, attempt)); //Generate another later
addQueue(data.rtpWorld, type, new ReQueueData(data.rtpWorld, queueMax, queueMin, newCount, type, attempt)); //Generate another later
return;
}
@@ -87,7 +87,7 @@ public class QueueGenerator {
for (Map.Entry<String, RTPWorld> rtpWorldEntry : map.entrySet()) {
RTPWorld world = rtpWorldEntry.getValue();
String type = getId(setup, rtpWorldEntry.getKey());
List<QueueData> applicable = QueueHandler.getApplicableAsync(data.queue, world);
List<QueueData> applicable = QueueHandler.getApplicableAsync(world);
int newCount = data.lastType.equalsIgnoreCase(type) ? data.lastCount : applicable.size();
int attempt = data.lastType.equalsIgnoreCase(type) ? data.attempts + 1 : 0;
if (newCount < queueMin && applicable.size() < queueMax) {
@@ -96,7 +96,7 @@ public class QueueGenerator {
continue;
}
//Generate a location sync to bukkit api
addQueue(world, type, new ReQueueData(data.queue, null, queueMax, queueMin, newCount, type, attempt)); //Generate another when done later
addQueue(world, type, new ReQueueData(null, queueMax, queueMin, newCount, type, attempt)); //Generate another when done later
return;
}
@@ -111,12 +111,10 @@ public class QueueGenerator {
static class ReQueueData {
List<QueueData> queue;
RTPWorld rtpWorld;
int queueMax, queueMin, lastCount, attempts;
String lastType;
ReQueueData(List<QueueData> queue, RTPWorld rtpWorld, int queueMax, int queueMin, int lastCount, String lastType, int attempts) {
this.queue = queue;
ReQueueData(RTPWorld rtpWorld, int queueMax, int queueMin, int lastCount, String lastType, int attempts) {
this.rtpWorld = rtpWorld;
this.queueMax = queueMax;
this.queueMin = queueMin;

View File

@@ -38,21 +38,23 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
@EventHandler
public void onRTP(RTP_TeleportPostEvent e) {
//Delete previously used location
Location location = e.getLocation();
remove(location);
remove(e.getLocation());
}
public static QueueData getRandomAsync(RTPWorld rtpWorld) {
List<QueueData> queueData = getApplicableAsync(null, rtpWorld);
List<QueueData> queueData = getApplicableAsync(rtpWorld);
if (queueData.size() <= QueueGenerator.queueMin && !BetterRTP.getInstance().getQueue().generator.generating)
BetterRTP.getInstance().getQueue().generator.generate(rtpWorld);
if (!queueData.isEmpty())
return queueData.get(new Random().nextInt(queueData.size()));
if (!queueData.isEmpty()) {
QueueData randomQueue = queueData.get(new Random().nextInt(queueData.size()));
queueData.clear();
return randomQueue;
}
return null;
}
public static List<QueueData> getApplicableAsync(@Nullable List<QueueData> queueCache, RTPWorld rtpWorld) {
List<QueueData> queueData = queueCache == null ? DatabaseHandler.getQueue().getInRange(new DatabaseQueue.QueueRangeData(rtpWorld)) : queueCache;
public static List<QueueData> getApplicableAsync(RTPWorld rtpWorld) {
List<QueueData> queueData = DatabaseHandler.getQueue().getInRange(new DatabaseQueue.QueueRangeData(rtpWorld));
List<QueueData> available = new ArrayList<>();
for (QueueData data : queueData) {
if (!Objects.equals(data.getLocation().getWorld().getName(), rtpWorld.getWorld().getName()))
@@ -68,7 +70,7 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
available.add(data);
}
}
BetterRTP.getInstance().getLogger().info("Available: " + available.size());
//BetterRTP.getInstance().getLogger().info("Available: " + available.size());
return available;
}