Queue loading improvements

This commit is contained in:
RonanCraft
2022-10-05 13:48:22 -04:00
parent d460a6a510
commit 61d7e335d9
4 changed files with 87 additions and 33 deletions

View File

@@ -118,7 +118,7 @@
</build> </build>
<repositories> <repositories>
<!-- Github based Repos --> <!-- GitHub based Repos -->
<repository> <repository>
<id>jitpack.io</id> <id>jitpack.io</id>
<url>https://jitpack.io</url> <url>https://jitpack.io</url>
@@ -264,7 +264,7 @@
<dependency> <dependency>
<groupId>com.github.cryptomorin</groupId> <groupId>com.github.cryptomorin</groupId>
<artifactId>kingdoms</artifactId> <artifactId>kingdoms</artifactId>
<version>1.12.6.1</version> <version>1.13.9</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@@ -312,7 +312,7 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.22</version> <version>1.18.24</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- EssentialsX --> <!-- EssentialsX -->

View File

@@ -1,5 +1,6 @@
package me.SuperRonanCraft.BetterRTP.references.database; package me.SuperRonanCraft.BetterRTP.references.database;
import lombok.NonNull;
import me.SuperRonanCraft.BetterRTP.BetterRTP; import me.SuperRonanCraft.BetterRTP.BetterRTP;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.QueueData; import me.SuperRonanCraft.BetterRTP.references.rtpinfo.QueueData;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@@ -11,6 +12,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@@ -60,12 +62,12 @@ public class DatabaseQueue extends SQLite {
long x = rs.getLong(COLUMNS.X.name); long x = rs.getLong(COLUMNS.X.name);
long z = rs.getLong(COLUMNS.Z.name); long z = rs.getLong(COLUMNS.Z.name);
String worldName = rs.getString(COLUMNS.WORLD.name); String worldName = rs.getString(COLUMNS.WORLD.name);
//String id = rs.getString(COLUMNS.IDENTIFIER.name); int id = rs.getInt(COLUMNS.ID.name);
long generated = rs.getLong(COLUMNS.GENERATED.name); long generated = rs.getLong(COLUMNS.GENERATED.name);
World world = Bukkit.getWorld(worldName); World world = Bukkit.getWorld(worldName);
if (world != null) { if (world != null) {
QueueData data = new QueueData(new Location(world, x, 69 /*giggity*/, z), generated); queueDataList.add(new QueueData(new Location(world, x, 69 /*giggity*/, z), generated, id));
queueDataList.add(data); //queueDataList.add(data);
} }
} }
} catch (SQLException ex) { } catch (SQLException ex) {
@@ -77,7 +79,7 @@ public class DatabaseQueue extends SQLite {
} }
//Set a queue to save //Set a queue to save
public boolean addQueue(QueueData data) { public QueueData addQueue(Location loc) {
String pre = "INSERT INTO "; String pre = "INSERT INTO ";
String sql = pre + tables.get(0) + " (" String sql = pre + tables.get(0) + " ("
+ COLUMNS.X.name + ", " + COLUMNS.X.name + ", "
@@ -87,13 +89,62 @@ public class DatabaseQueue extends SQLite {
//+ COLUMNS.USES.name + " " //+ COLUMNS.USES.name + " "
+ ") VALUES(?, ?, ?, ?)"; + ") VALUES(?, ?, ?, ?)";
List<Object> params = new ArrayList<Object>() {{ List<Object> params = new ArrayList<Object>() {{
add(data.getLocation().getBlockX()); add(loc.getBlockX());
add(data.getLocation().getBlockZ()); add(loc.getBlockZ());
add(data.getLocation().getWorld().getName()); add(loc.getWorld().getName());
add(data.getGenerated()); add(System.currentTimeMillis());
//add(data.getUses()); //add(data.getUses());
}}; }};
return sqlUpdate(sql, params); //return sqlUpdate(sql, params);
int database_id = createQueue(sql, params);
if (database_id >= 0)
return new QueueData(loc, System.currentTimeMillis(), database_id);
return null;
}
public int getCount() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int count = 0;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + tables.get(0));
rs = ps.executeQuery();
count = rs.getFetchSize();
} catch (SQLException ex) {
BetterRTP.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
close(ps, rs, conn);
}
return count;
}
private int createQueue(String statement, @NonNull List<Object> params) {
Connection conn = null;
PreparedStatement ps = null;
int id = -1;
try {
conn = getSQLConnection();
ps = conn.prepareStatement(statement);
Iterator<Object> it = params.iterator();
int paramIndex = 1;
while (it.hasNext()) {
ps.setObject(paramIndex, it.next());
paramIndex++;
}
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
id = rs.getInt(COLUMNS.ID.name);
}
} catch (SQLException ex) {
BetterRTP.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
close(ps, null, conn);
}
return id;
} }
public boolean removeQueue(QueueData data) { public boolean removeQueue(QueueData data) {

View File

@@ -8,20 +8,14 @@ import org.bukkit.Location;
public class QueueData { public class QueueData {
//@Getter final String identifier; @Getter final int database_id;
@Getter @Setter Location location; @Getter @Setter Location location;
@Getter final long generated; @Getter final long generated;
public QueueData(Location location, long generated/*, String identifier*/) { public QueueData(Location location, long generated, int database_id) {
this.location = location; this.location = location;
this.generated = generated; this.generated = generated;
//this.identifier = identifier; this.database_id = database_id;
}
public QueueData(RTPWorld rtpWorld) {
this.location = WorldPlayer.generateLocation(rtpWorld);
this.generated = System.currentTimeMillis();
//this.identifier = rtpWorld.getID();
} }
} }

View File

@@ -10,6 +10,7 @@ import me.SuperRonanCraft.BetterRTP.references.customEvents.RTP_TeleportPostEven
import me.SuperRonanCraft.BetterRTP.references.database.DatabaseHandler; import me.SuperRonanCraft.BetterRTP.references.database.DatabaseHandler;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.RTPWorld; import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.RTPWorld;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.WorldCustom; import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.WorldCustom;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.WorldPlayer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
@@ -58,7 +59,11 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
return; return;
} }
//Download all queue cached from last session //Download all queue cached from last session
//long usedmemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
//BetterRTP.debug("Memory used " + usedmemory);
queueList = DatabaseHandler.getQueue().getQueues(); queueList = DatabaseHandler.getQueue().getQueues();
//usedmemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) - usedmemory;
//BetterRTP.debug("Memory used after load " + usedmemory);
loaded = true; loaded = true;
BetterRTP.debug("Loaded " + queueList.size() + " previously generated safe locations!"); BetterRTP.debug("Loaded " + queueList.size() + " previously generated safe locations!");
//Queue after everything was loaded //Queue after everything was loaded
@@ -183,34 +188,38 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
//Generate a safe location //Generate a safe location
private void generateFromWorld(RTPWorld world, String id, ReQueueData reQueueData) { private void generateFromWorld(RTPWorld world, String id, ReQueueData reQueueData) {
addQueue(world, new QueueData(world), id, reQueueData); addQueue(world, id, reQueueData);
} }
private void addQueue(RTPWorld rtpWorld, QueueData data, String id, ReQueueData reQueueData) { private void addQueue(RTPWorld rtpWorld, String id, ReQueueData reQueueData) {
if (data.getLocation() != null) { Location loc = WorldPlayer.generateLocation(rtpWorld);
if (loc != null) {
Bukkit.getScheduler().runTask(BetterRTP.getInstance(), () -> { Bukkit.getScheduler().runTask(BetterRTP.getInstance(), () -> {
//BetterRTP.debug("Queued up a new position, attempts " + reQueueData.attempts); //BetterRTP.debug("Queued up a new position, attempts " + reQueueData.attempts);
PaperLib.getChunkAtAsync(data.getLocation()) PaperLib.getChunkAtAsync(loc)
.thenAccept(v -> { .thenAccept(v -> {
Location loc = RTPPlayer.getSafeLocation( Location safeLoc = RTPPlayer.getSafeLocation(
RTP.getWorldType(rtpWorld), RTP.getWorldType(rtpWorld),
data.getLocation().getWorld(), loc.getWorld(),
data.getLocation(), loc,
rtpWorld.getMinY(), rtpWorld.getMinY(),
rtpWorld.getMaxY(), rtpWorld.getMaxY(),
rtpWorld.getBiomes()); rtpWorld.getBiomes());
data.setLocation(loc); //data.setLocation(safeLoc);
if (loc != null) { if (safeLoc != null) {
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> { Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
if (DatabaseHandler.getQueue().addQueue(data)) { QueueData data = DatabaseHandler.getQueue().addQueue(safeLoc);
if (data != null) {
queueList.add(data); queueList.add(data);
String _x = String.valueOf(data.getLocation().getBlockX()); String _x = String.valueOf(data.getLocation().getBlockX());
String _y = String.valueOf(data.getLocation().getBlockY()); String _y = String.valueOf(data.getLocation().getBlockY());
String _z = String.valueOf(data.getLocation().getBlockZ()); String _z = String.valueOf(data.getLocation().getBlockZ());
String _world = data.getLocation().getWorld().getName(); String _world = data.getLocation().getWorld().getName();
BetterRTP.debug("Queue position generated #" + queueList.size() + ": id= " + id + ", location= x:" + _x + ", y:" + _y + ", z:" + _z + ", world:" + _world); BetterRTP.debug("Queue position generated #" + queueList.size()
+ ": id= " + id + ", database_ID" + data.database_id
+ ", location= x:" + _x + ", y:" + _y + ", z:" + _z + ", world:" + _world);
} else } else
BetterRTP.debug("Database error occurred for a queue! " + data.getLocation().toString()); BetterRTP.debug("Database error occurred for a queue when trying to save: " + safeLoc);
queueGenerator(reQueueData); queueGenerator(reQueueData);
}); });
} else } else