queue system up and running

This commit is contained in:
SuperRonanCraft 2022-04-09 00:31:13 -04:00
parent 4d4f6b2d86
commit 7f6afdfd7a
10 changed files with 122 additions and 60 deletions

View File

@ -130,6 +130,7 @@ public class BetterRTP extends JavaPlugin {
listener.load();
eco.load();
perms.register();
queue.load();
}
public static void debug(String str) {

View File

@ -155,7 +155,10 @@ public class CmdInfo implements RTPCommand, RTPCommandHelpable {
case "centerz":
return worldPlayer.getUseWorldborder() || worldPlayer.getCenterZ() == worldDefault.getCenterZ() ? worldPlayer.getUseWorldborder() ? " &8(worldborder)" : " &8(default)" : "";
case "maxrad":
return worldPlayer.getUseWorldborder() || worldPlayer.getMaxRadius() == worldDefault.getMaxRadius() ? worldPlayer.getUseWorldborder() ? " &8(worldborder)" : " &8(default)" : "";
return worldPlayer.getUseWorldborder() || worldPlayer.getMaxRadius() == worldDefault.getMaxRadius() ?
worldPlayer.getUseWorldborder() ?
worldPlayer.getMaxRadius() >= worldPlayer.getWorld().getWorldBorder().getSize() ?
" &8(worldborder)" : " &8(custom)" : " &8(default)" : "";
case "minrad":
return worldPlayer.getMinRadius() == worldDefault.getMinRadius() ? " &8(default)" : "";
case "price":

View File

@ -12,12 +12,13 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
public class DatabaseQueue extends SQLite {
public DatabaseQueue() {
super(DATABASE_TYPE.PLAYERS);
super(DATABASE_TYPE.QUEUE);
}
@Override
@ -28,15 +29,12 @@ public class DatabaseQueue extends SQLite {
}
public enum COLUMNS {
ID("id", "long PRIMARY KEY AUTOINCREMENT"),
//COOLDOWN DATA
ID("id", "integer PRIMARY KEY AUTOINCREMENT"),
//Location Data
X("x", "long"),
Y("y", "long"),
Z("z", "long"),
WORLD("world", "varchar(32)"),
GENERATED("generated", "long"),
//IDENTIFIER("identifier", "varchar(32)"),
//USES("uses", "integer"),
GENERATED("generated", "long")
;
public final String name;
@ -60,14 +58,13 @@ public class DatabaseQueue extends SQLite {
rs = ps.executeQuery();
while (rs.next()) {
long x = rs.getLong(COLUMNS.X.name);
long y = rs.getLong(COLUMNS.Y.name);
long z = rs.getLong(COLUMNS.Z.name);
String worldName = rs.getString(COLUMNS.WORLD.name);
//String id = rs.getString(COLUMNS.IDENTIFIER.name);
long generated = rs.getLong(COLUMNS.GENERATED.name);
World world = Bukkit.getWorld(worldName);
if (world != null) {
QueueData data = new QueueData(new Location(world, x, y, z), generated);
QueueData data = new QueueData(new Location(world, x, 69 /*giggity*/, z), generated);
queueDataList.add(data);
}
}
@ -80,24 +77,37 @@ public class DatabaseQueue extends SQLite {
}
//Set a queue to save
public void addQueue(QueueData data) {
public boolean addQueue(QueueData data) {
String pre = "INSERT INTO ";
String sql = pre + tables.get(0) + " ("
+ COLUMNS.X.name + ", "
+ COLUMNS.Y.name + ", "
+ COLUMNS.Z.name + ", "
+ COLUMNS.WORLD.name + ", "
+ COLUMNS.GENERATED.name + " "
//+ COLUMNS.USES.name + " "
+ ") VALUES(?, ?, ?, ?, ?)";
+ ") VALUES(?, ?, ?, ?)";
List<Object> params = new ArrayList<Object>() {{
add(data.getLocation().getX());
add(data.getLocation().getY());
add(data.getLocation().getZ());
add(data.getLocation().getWorld().getName());
add(data.getGenerated());
//add(data.getUses());
}};
sqlUpdate(sql, params);
return sqlUpdate(sql, params);
}
public boolean removeQueue(QueueData data) {
String sql = "DELETE FROM " + tables.get(0) + " WHERE "
+ COLUMNS.X.name + " = ? AND "
+ COLUMNS.Z.name + " = ? AND "
+ COLUMNS.WORLD.name + " = ?"
;
Location loc = data.getLocation();
List<Object> params = new ArrayList<Object>() {{
add(loc.getBlockX());
add(loc.getBlockZ());
add(loc.getWorld().getName());
}};
return sqlUpdate(sql, params);
}
}

View File

@ -140,6 +140,8 @@ public abstract class SQLite {
private Enum<?>[] getColumns(DATABASE_TYPE type) {
switch (type) {
case PLAYERS: return DatabasePlayers.COLUMNS.values();
case QUEUE: return DatabaseQueue.COLUMNS.values();
case COOLDOWN:
default: return DatabaseCooldownsWorlds.COLUMNS.values();
}
}
@ -147,6 +149,8 @@ public abstract class SQLite {
private String getColumnName(DATABASE_TYPE type, Enum<?> column) {
switch (type) {
case PLAYERS: return ((DatabasePlayers.COLUMNS) column).name;
case QUEUE: return ((DatabaseQueue.COLUMNS) column).name;
case COOLDOWN:
default: return ((DatabaseCooldownsWorlds.COLUMNS) column).name;
}
}
@ -154,6 +158,8 @@ public abstract class SQLite {
private String getColumnType(DATABASE_TYPE type, Enum<?> column) {
switch (type) {
case PLAYERS: return ((DatabasePlayers.COLUMNS) column).type;
case QUEUE: return ((DatabaseQueue.COLUMNS) column).type;
case COOLDOWN:
default: return ((DatabaseCooldownsWorlds.COLUMNS) column).type;
}
}
@ -250,6 +256,6 @@ public abstract class SQLite {
public enum DATABASE_TYPE {
PLAYERS,
COOLDOWN,
COOLDOWN_GLOBAL, //Table to know last time in general player has a cooldown for
QUEUE,
}
}

View File

@ -1,15 +1,14 @@
package me.SuperRonanCraft.BetterRTP.references.rtpinfo;
import lombok.NonNull;
import me.SuperRonanCraft.BetterRTP.BetterRTP;
import me.SuperRonanCraft.BetterRTP.player.rtp.RTP;
import me.SuperRonanCraft.BetterRTP.player.rtp.RTPPlayer;
import me.SuperRonanCraft.BetterRTP.references.customEvents.RTP_FindLocationEvent;
import me.SuperRonanCraft.BetterRTP.references.customEvents.RTP_TeleportPostEvent;
import me.SuperRonanCraft.BetterRTP.references.database.DatabaseHandler;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.RTPWorld;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.WORLD_TYPE;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.WorldDefault;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.WorldPlayer;
import me.SuperRonanCraft.BetterRTP.references.database.DatabaseQueue;
import me.SuperRonanCraft.BetterRTP.references.rtpinfo.worlds.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
@ -17,34 +16,28 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.*;
public class QueueHandler implements Listener { //Randomly queues up some safe locations
boolean loaded = false;
List<QueueData> queueList = new ArrayList<>();
private final int queueSize = 8; //Amount to ready up for each rtp world
private final int queueMax = 8, queueMin = 2; //Amount to ready up for each rtp world
private boolean generating;
public void registerEvents(BetterRTP pl) {
//DEBUG ONLY FOR THE TIME BEING
if (!BetterRTP.getInstance().getSettings().isDebug())
return;
PluginManager pm = pl.getServer().getPluginManager();
pm.registerEvents(this, pl);
}
public void load() {
//DEBUG ONLY FOR THE TIME BEING
if (!BetterRTP.getInstance().getSettings().isDebug())
return;
loaded = false;
queueDownload();
queueGenerator(null, queueMax, queueMin, 0, "noone");
}
private void queueDownload() {
queueList.clear();
//LOAD FROM DATABASE
Bukkit.getScheduler().runTaskLaterAsynchronously(BetterRTP.getInstance(), () -> {
@ -66,7 +59,8 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
QueueData data = queueData.get(new Random().nextInt(queueData.size()));
e.setLocation(data.location);
}
queueGenerator(e.getWorld());
if (!generating)
queueGenerator(e.getWorld(), queueMax, queueMin, 0, "noone");
}
@EventHandler
@ -77,71 +71,108 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
List<QueueData> deleteList = new ArrayList<>();
for (QueueData data : queueList) {
Location dataLoc = data.getLocation();
//BetterRTP.debug("--");
//BetterRTP.debug(location.getBlockX() + " -> " + dataLoc.getBlockX());
//BetterRTP.debug(location.getBlockZ() + " -> " + dataLoc.getBlockZ());
if (location.getBlockX() == dataLoc.getBlockX()
&& location.getBlockY() == dataLoc.getBlockY()
&& location.getBlockZ() == dataLoc.getBlockZ()
&& location.getWorld().getName().equals(dataLoc.getWorld().getName())) {
deleteList.add(data);
}
}
deleteList.forEach(data -> queueList.remove(data));
deleteList.forEach(data -> {
if (DatabaseHandler.getQueue().removeQueue(data)) {
queueList.remove(data);
BetterRTP.debug("-Removed a queue " + data.getLocation().toString());
}
});
});
}
private void queueGenerator(RTPWorld rtpWorld) {
private void queueGenerator(RTPWorld rtpWorld, int queueMax, int queueMin, int lastCount, @NonNull String lastType) {
if (queueMax != 10000)
return;
/*if (queueList.size() >= queueSize) {
//Plenty of locations, cancel out
return;
}*/
generating = true;
Bukkit.getScheduler().runTaskLaterAsynchronously(BetterRTP.getInstance(), () -> {
//Generate more locations
//Rare cases where a rtp world didnt have a location generated (Permission Groups?)
if (getApplicable(rtpWorld).size() < queueSize / 2) {
if (rtpWorld != null) {
List<QueueData> applicable = getApplicable(rtpWorld);
String type = "superCustom_" + (rtpWorld.getID() != null ? rtpWorld.getID() : rtpWorld.getWorld().getName());
int newCount = lastType.equalsIgnoreCase(type) ? lastCount : applicable.size();
if (newCount < queueMin && applicable.size() < queueMax) {
generateFromWorld(rtpWorld);
queueGenerator(rtpWorld); //Generate another later
queueGenerator(rtpWorld, queueMax, queueMin, newCount, type); //Generate another later
return;
}
if (lastType.equalsIgnoreCase(type))
BetterRTP.debug("Queue max reached for " + type + " (amount: " + applicable.size() + ") lastCount: " + lastCount);
}
//Generate Defaults
WorldDefault worldDefault = BetterRTP.getInstance().getRTP().RTPdefaultWorld;
for (World world : Bukkit.getWorlds()) {
if (!BetterRTP.getInstance().getRTP().getDisabledWorlds().contains(world.getName())
&& !BetterRTP.getInstance().getRTP().RTPcustomWorld.containsKey(world.getName())) {
if (getApplicable(worldDefault).size() < queueSize) {
generateFromWorld(worldDefault);
queueGenerator(null); //Generate another later
RTPWorld newWorld = new WorldCustom(world, worldDefault);
List<QueueData> applicable = getApplicable(newWorld);
String type = "default_" + world.getName();
int newCount = lastType.equalsIgnoreCase(type) ? lastCount : applicable.size();
if (newCount < queueMin && applicable.size() < queueMax) {
generateFromWorld(newWorld);
queueGenerator(null, queueMax, queueMin, newCount, type); //Generate another later
return;
}
if (lastType.equalsIgnoreCase(type))
BetterRTP.debug("Queue max reached for " + type + " (amount: " + applicable.size() + ") world: " + world.getName() + " lastCount: " + lastCount);
}
}
//Generate Custom Worlds
for (Map.Entry<String, RTPWorld> customWorld : BetterRTP.getInstance().getRTP().RTPcustomWorld.entrySet()) {
RTPWorld world = customWorld.getValue();
if (getApplicable(world).size() < queueSize) {
List<QueueData> applicable = getApplicable(world);
String type = "custom_" + customWorld.getKey();
int newCount = lastType.equalsIgnoreCase(type) ? lastCount : applicable.size();
if (newCount < queueMin && applicable.size() < queueMax) {
generateFromWorld(world);
queueGenerator(null); //Generate another later
queueGenerator(null, queueMax, queueMin, newCount, type); //Generate another later
return;
}
if (lastType.equalsIgnoreCase(type))
BetterRTP.debug("Queue max reached for " + type + " " + applicable.size() + " " + customWorld.getValue().getWorld().getName() + " lastCount: " + lastCount);
}
//Generate Locations
for (Map.Entry<String, RTPWorld> location : BetterRTP.getInstance().getRTP().RTPworldLocations.entrySet()) {
RTPWorld world = location.getValue();
if (getApplicable(world).size() < queueSize) {
List<QueueData> applicable = getApplicable(world);
String type = "location_" + location.getValue().getID();
int newCount = lastType.equalsIgnoreCase(type) ? lastCount : applicable.size();
if (newCount < queueMin && applicable.size() < queueMax) {
generateFromWorld(world);
queueGenerator(null); //Generate another later
queueGenerator(null, queueMax, queueMin, newCount, type); //Generate another later
return;
}
if (lastType.equalsIgnoreCase(type))
BetterRTP.debug("Queue max reached for " + type + " " + applicable.size() + " " + location.getValue().getID() + " lastCount: " + lastCount);
}
generating = false;
}, 20L * 5 /*delay before starting queue generator*/);
}
//Generate a location on another thread
private void generateFromWorld(RTPWorld world) {
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> addQueue(world, new QueueData(world)));
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
//BetterRTP.debug("Queue attempt started...");
QueueData data = new QueueData(world);
addQueue(world, data);
});
}
private void addQueue(RTPWorld rtpWorld, QueueData data) {
@ -154,14 +185,21 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
rtpWorld.getBiomes());
if (loc != null) {
data.setLocation(loc);
if (DatabaseHandler.getQueue().addQueue(data)) {
queueList.add(data);
}
BetterRTP.debug("Queue position added " + data.getLocation().toString());
} else
BetterRTP.debug("Database error occured for a queue! " + data.getLocation().toString());
} else
BetterRTP.debug("Queue position wasn't safe " + data.getLocation().toString());
}
public static List<QueueData> getApplicable(RTPWorld rtpWorld) {
List<QueueData> queueData = BetterRTP.getInstance().getQueue().queueList;
List<QueueData> available = new ArrayList<>();
for (QueueData data : queueData) {
if (!Objects.equals(data.getLocation().getWorld().getName(), rtpWorld.getWorld().getName()))
continue;
switch (rtpWorld.getShape()) {
case CIRCLE:
if (isInCircle(data.location, rtpWorld))

View File

@ -134,6 +134,11 @@ public class WorldCustom implements RTPWorld, RTPWorld_Defaulted {
//this.Biomes = config.getStringList("CustomWorlds." + world + ".Biomes");
}
public WorldCustom(World world, RTPWorld rtpWorld) {
setAllFrom(rtpWorld);
this.world = world;
}
@Override
public boolean getUseWorldborder() {
return useWorldborder;

View File

@ -124,8 +124,7 @@ public class WorldDefault implements RTPWorld {
return Biomes;
}
@NotNull
@Override
@NotNull @Override
public World getWorld() {
return null;
}

View File

@ -18,7 +18,7 @@ public class WorldLocations implements RTPWorld, RTPWorld_Defaulted {
private List<String> biomes;
private World world;
private RTP_SHAPE shape;
private String name;
private final String name;
public WorldLocations(String location_name) {
FileBasics.FILETYPE config = BetterRTP.getInstance().getFiles().getType(FileBasics.FILETYPE.LOCATIONS);

View File

@ -7,6 +7,7 @@ import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
import me.SuperRonanCraft.BetterRTP.BetterRTP;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@ -159,7 +160,7 @@ public class WorldPermissionGroup implements RTPWorld, RTPWorld_Defaulted {
return biomes;
}
@Override
@NotNull @Override
public @NonNull World getWorld() {
return world;
}

View File

@ -99,10 +99,9 @@ public class WorldPlayer implements RTPWorld, RTPWorld_Defaulted {
public static Location generateLocation(RTPWorld rtpWorld) {
Location loc;
switch (rtpWorld.getShape()) {
case CIRCLE:
loc = generateRound(rtpWorld); break;
default:
loc = generateSquare(rtpWorld); break;
case CIRCLE: loc = generateRound(rtpWorld); break;
case SQUARE:
default: loc = generateSquare(rtpWorld); break;
}
return loc;
}
@ -130,7 +129,7 @@ public class WorldPlayer implements RTPWorld, RTPWorld_Defaulted {
x += rtpWorld.getCenterX();
z += rtpWorld.getCenterZ();
//System.out.println(quadrant);
return new Location(rtpWorld.getWorld(), x, 0, z);
return new Location(rtpWorld.getWorld(), x, 69, z);
}
private static Location generateRound(RTPWorld rtpWorld) {
@ -150,7 +149,7 @@ public class WorldPlayer implements RTPWorld, RTPWorld_Defaulted {
z = (int) (r * Math.sin(theta));
x += rtpWorld.getCenterX();
z += rtpWorld.getCenterZ();
return new Location(rtpWorld.getWorld(), x, 0, z);
return new Location(rtpWorld.getWorld(), x, 69, z);
}
@NotNull