chunk data caching

This commit is contained in:
RonanCraft 2023-02-27 17:06:47 -05:00
parent e20126a034
commit d2c0ed4162
7 changed files with 124 additions and 63 deletions

View File

@ -1,8 +1,10 @@
package me.SuperRonanCraft.BetterRTP.player.commands.types;
import me.SuperRonanCraft.BetterRTP.player.commands.RTPCommand;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.ChunkCacher;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.RandomLocation;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import java.util.List;
@ -13,7 +15,7 @@ public class CmdDeveloper implements RTPCommand {
}
public void execute(CommandSender sendi, String label, String[] args) {
RandomLocation.runChunkTest();
new ChunkCacher().runChunkTest();
}
@Override public List<String> tabComplete(CommandSender sendi, String[] args) {
@ -21,10 +23,10 @@ public class CmdDeveloper implements RTPCommand {
}
public boolean permission(CommandSender sendi) {
return sendi.getName().equalsIgnoreCase("SuperRonanCraft") || sendi.getName().equalsIgnoreCase("RonanCrafts");
return sendi instanceof ConsoleCommandSender || sendi.getName().equalsIgnoreCase("SuperRonanCraft") || sendi.getName().equalsIgnoreCase("RonanCrafts");
}
public void usage(CommandSender sendi, String label) {
sendi.sendMessage("This is for Developement use only!");
sendi.sendMessage("This is for Development use only!");
}
}

View File

@ -0,0 +1,17 @@
package me.SuperRonanCraft.BetterRTP.references.database;
import lombok.Getter;
import org.bukkit.Chunk;
import org.bukkit.block.Biome;
public class ChunkDatabaseCache {
private @Getter Chunk chunk;
private @Getter int maxy;
private @Getter Biome biome;
public ChunkDatabaseCache(Chunk chunk, int maxy, Biome biome) {
this.chunk = chunk;
this.maxy = maxy;
this.biome = biome;
}
}

View File

@ -52,24 +52,28 @@ public class DatabaseChunkData extends SQLite {
}
public void addChunk(Chunk chunk, int maxy, Biome biome) {
public void addChunk(List<ChunkDatabaseCache> cacheList) {
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
String pre = "INSERT OR REPLACE INTO ";
String sql = pre + tables.get(0) + " ("
+ COLUMNS.WORLD.name + ", "
+ COLUMNS.X.name + ", "
+ COLUMNS.Z.name + ", "
+ COLUMNS.BIOME.name + ", "
+ COLUMNS.MAX_Y.name + " "
+ ") VALUES(?, ?, ?, ?, ?)";
List<Object> params = new ArrayList<Object>() {{
add(chunk.getWorld().getName());
add(chunk.getX());
add(chunk.getZ());
add(biome.name());
add(maxy);
}};
sqlUpdate(sql, params);
List<String> sqls = new ArrayList<>();
List<List<Object>> params = new ArrayList<>();
for (ChunkDatabaseCache cache : cacheList) {
sqls.add(pre + tables.get(0) + " ("
+ COLUMNS.WORLD.name + ", "
+ COLUMNS.X.name + ", "
+ COLUMNS.Z.name + ", "
+ COLUMNS.BIOME.name + ", "
+ COLUMNS.MAX_Y.name + " "
+ ") VALUES(?, ?, ?, ?, ?)");
params.add(new ArrayList<Object>() {{
add(cache.getChunk().getWorld().getName());
add(cache.getChunk().getX());
add(cache.getChunk().getZ());
add(cache.getBiome().name());
add(cache.getMaxy());
}});
}
sqlUpdate(sqls, params);
});
}

View File

@ -5,6 +5,7 @@ import lombok.NonNull;
import me.SuperRonanCraft.BetterRTP.BetterRTP;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.RandomLocation;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
import java.io.File;
import java.io.IOException;
@ -34,7 +35,16 @@ public abstract class SQLite {
return getLocal();
}
private Connection con;
private Connection getLocal() {
if (con != null) {
try {
if (!con.isClosed())
return con;
} catch (SQLException ignored) {
}
}
File dataFolder = new File(BetterRTP.getInstance().getDataFolder().getPath() + File.separator + "data", db_file_name + ".db");
if (!dataFolder.exists()){
try {
@ -47,7 +57,8 @@ public abstract class SQLite {
}
try {
Class.forName("org.sqlite.JDBC");
return DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
con = DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
return con;
} catch (SQLException ex) {
BetterRTP.getInstance().getLogger().log(Level.SEVERE, "SQLite exception on initialize", ex);
} catch (ClassNotFoundException ex) {
@ -171,15 +182,16 @@ public abstract class SQLite {
boolean success = true;
try {
conn = getSQLConnection();
String str = statement1.get(0);
for (int i = 1; i < statement1.size(); i++) {
String state = statement1.get(i);
str = str.concat("; " + state);
}
Statement statement = conn.statement
for (int i = 0; i < statement1.size(); i++) {
String statement = statement1.get(i);
List<Object> params = params1.get(i);
if (ps == null)
ps = conn.prepareStatement(statement);
else
ps.addBatch(statement);
if (params != null) {
Iterator<Object> it = params.iterator();
List<Object> param = params1.get(i);
if (param != null) {
Iterator<Object> it = param.iterator();
int paramIndex = 1;
while (it.hasNext()) {
ps.setObject(paramIndex, it.next());
@ -219,8 +231,8 @@ public abstract class SQLite {
protected void close(PreparedStatement ps, ResultSet rs, Connection conn) {
try {
if (ps != null) ps.close();
if (conn != null) conn.close();
if (rs != null) rs.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
Error.close(BetterRTP.getInstance(), ex);
}

View File

@ -0,0 +1,58 @@
package me.SuperRonanCraft.BetterRTP.references.rtpinfo;
import io.papermc.lib.PaperLib;
import me.SuperRonanCraft.BetterRTP.BetterRTP;
import me.SuperRonanCraft.BetterRTP.references.database.ChunkDatabaseCache;
import me.SuperRonanCraft.BetterRTP.references.database.DatabaseHandler;
import org.bukkit.*;
import org.bukkit.block.Biome;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class ChunkCacher {
List<ChunkDatabaseCache> cache = new ArrayList<>();
public void runChunkTest() {
BetterRTP.getInstance().getLogger().info("---------------- Starting chunk test!");
World world = Bukkit.getWorld("world");
cacheChunkAt(world, 32, -32, -32, -32);
}
private void upload() {
BetterRTP.getInstance().getLogger().info("Uploading " + cache.size() + " chunk data's");
DatabaseHandler.getChunks().addChunk(cache);
}
private void cacheTask(World world, int goal, int start, int xat, int zat) {
zat += 1;
if (zat > goal) {
zat = start;
xat += 1;
}
if (xat <= goal)
cacheChunkAt(world, goal, start, xat, zat);
else
upload();
}
private void cacheChunkAt(World world, int goal, int start, int xat, int zat) {
CompletableFuture<Chunk> task = PaperLib.getChunkAtAsync(new Location(world, xat * 16, 0, zat * 16));
task.thenAccept(chunk -> {
try {
ChunkSnapshot snapshot = chunk.getChunkSnapshot(true, true, false);
int maxy = snapshot.getHighestBlockYAt(8, 8);
Biome biome = snapshot.getBiome(8, 8);
//BetterRTP.getInstance().getLogger().info("Added " + chunk.getX() + " " + chunk.getZ());
cache.add(new ChunkDatabaseCache(chunk, maxy, biome));
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException();
//BetterRTP.getInstance().getLogger().info("Tried Adding " + chunk.getX() + " " + chunk.getZ());
}
chunk.unload();
}).thenRun(() -> cacheTask(world, goal, start, xat, zat));
}
}

View File

@ -2,6 +2,7 @@ package me.SuperRonanCraft.BetterRTP.references.rtpinfo;
import io.papermc.lib.PaperLib;
import me.SuperRonanCraft.BetterRTP.BetterRTP;
import me.SuperRonanCraft.BetterRTP.references.database.ChunkDatabaseCache;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.RTPWorld;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.WORLD_TYPE;
import org.bukkit.*;
@ -149,38 +150,4 @@ public class RandomLocation {
return true;
//FALSE MEANS NO BAD BLOCKS/BIOME WHERE FOUND!
}
public static void runChunkTest() {
BetterRTP.getInstance().getLogger().info("---------------- Starting chunk test!");
World world = Bukkit.getWorld("world");
cacheChunkAt(world, 32, -32, -32, -32);
}
private static void cacheTask(World world, int goal, int start, int xat, int zat) {
zat += 1;
if (zat > goal) {
zat = start;
xat += 1;
}
if (xat <= goal)
cacheChunkAt(world, goal, start, xat, zat);
}
private static void cacheChunkAt(World world, int goal, int start, int xat, int zat) {
CompletableFuture<Chunk> task = PaperLib.getChunkAtAsync(new Location(world, xat * 16, 0, zat * 16));
task.thenAccept(chunk -> {
try {
ChunkSnapshot snapshot = chunk.getChunkSnapshot(true, true, false);
int maxy = snapshot.getHighestBlockYAt(8, 8);
Biome biome = snapshot.getBiome(8, 8);
//BetterRTP.getInstance().getLogger().info("Added " + chunk.getX() + " " + chunk.getZ());
BetterRTP.getInstance().getDatabaseHandler().getDatabaseChunks().addChunk(chunk, maxy, biome);
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException();
//BetterRTP.getInstance().getLogger().info("Tried Adding " + chunk.getX() + " " + chunk.getZ());
}
}).thenRun(() -> cacheTask(world, goal, start, xat, zat));
}
}

View File

@ -4,6 +4,7 @@ To-Do list for BetterRTP
Worlds with spaces:
- Support for worlds with spaces in their names
- FIX if console/player executes `/rtp player <player> <world>` the <player> will still be able to rtp after without cooldown applied
-----DONE-----
Add KingdomsX support: