queue saving + super-multithreading removed

This commit is contained in:
RonanCraft 2022-04-13 17:34:25 -04:00
parent 5ebee9590b
commit 6381bf5108
4 changed files with 78 additions and 19 deletions

View File

@ -7,7 +7,7 @@
<groupId>me.SuperRonanCraft</groupId> <groupId>me.SuperRonanCraft</groupId>
<artifactId>BetterRTP</artifactId> <artifactId>BetterRTP</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>3.4.0</version> <version>3.4.1</version>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>

View File

@ -53,6 +53,7 @@ public class BetterRTP extends JavaPlugin {
public void onDisable() { public void onDisable() {
invs.closeAll(); invs.closeAll();
queue.unload();
} }
public Files getFiles() { public Files getFiles() {

View File

@ -88,7 +88,7 @@ public class DatabaseQueue extends SQLite {
+ ") VALUES(?, ?, ?, ?)"; + ") VALUES(?, ?, ?, ?)";
List<Object> params = new ArrayList<Object>() {{ List<Object> params = new ArrayList<Object>() {{
add(data.getLocation().getBlockX()); add(data.getLocation().getBlockX());
add(data.getLocation().getBlockX()); add(data.getLocation().getBlockZ());
add(data.getLocation().getWorld().getName()); add(data.getLocation().getWorld().getName());
add(data.getGenerated()); add(data.getGenerated());
//add(data.getUses()); //add(data.getUses());

View File

@ -2,6 +2,7 @@ package me.SuperRonanCraft.BetterRTP.references.rtpinfo;
import lombok.NonNull; import lombok.NonNull;
import me.SuperRonanCraft.BetterRTP.BetterRTP; import me.SuperRonanCraft.BetterRTP.BetterRTP;
import me.SuperRonanCraft.BetterRTP.player.commands.RTP_SETUP_TYPE;
import me.SuperRonanCraft.BetterRTP.player.rtp.RTP; import me.SuperRonanCraft.BetterRTP.player.rtp.RTP;
import me.SuperRonanCraft.BetterRTP.player.rtp.RTPPlayer; import me.SuperRonanCraft.BetterRTP.player.rtp.RTPPlayer;
import me.SuperRonanCraft.BetterRTP.references.customEvents.RTP_FindLocationEvent; import me.SuperRonanCraft.BetterRTP.references.customEvents.RTP_FindLocationEvent;
@ -35,10 +36,15 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
pm.registerEvents(this, pl); pm.registerEvents(this, pl);
} }
public void unload() {
if (task != null)
Bukkit.getScheduler().cancelTask(task.getTaskId());
}
public void load() { public void load() {
loaded = false; loaded = false;
if (task != null && Bukkit.getScheduler().isCurrentlyRunning(task.getTaskId())) if (task != null)
task.cancel(); Bukkit.getScheduler().cancelTask(task.getTaskId());
if (!BetterRTP.getInstance().getSettings().isQueueEnabled()) if (!BetterRTP.getInstance().getSettings().isQueueEnabled())
return; return;
@ -116,13 +122,14 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
List<QueueData> applicable = getApplicable(rtpWorld); List<QueueData> applicable = getApplicable(rtpWorld);
String type = "superCustom_" + (rtpWorld.getID() != null ? rtpWorld.getID() : rtpWorld.getWorld().getName()); String type = "superCustom_" + (rtpWorld.getID() != null ? rtpWorld.getID() : rtpWorld.getWorld().getName());
int newCount = lastType.equalsIgnoreCase(type) ? lastCount : applicable.size(); int newCount = lastType.equalsIgnoreCase(type) ? lastCount : applicable.size();
int attempt = lastType.equalsIgnoreCase(type) ? attempts + 1 : 0; int attempt = lastType.equalsIgnoreCase(type) ? attempts : 0;
if (newCount < queueMin && applicable.size() < queueMax) { if (newCount < queueMin && applicable.size() < queueMax) {
if (attempt > queueMaxAttempts) { if (attempt > queueMaxAttempts) {
BetterRTP.debug("Max attempts to create a Queue reached for " + type + " (amount: " + applicable.size() + ")"); BetterRTP.debug("Max attempts to create a Queue reached for " + type + " (amount: " + applicable.size() + ")");
return; return;
} }
generateFromWorld(rtpWorld); if (!generateFromWorld(rtpWorld, type)) //If fails to generate position, add an attempt (max amount of times to generate are: queueMaxAttempts + queueMax)
attempt ++;
queueGenerator(rtpWorld, queueMax, queueMin, newCount, type, attempt); //Generate another later queueGenerator(rtpWorld, queueMax, queueMin, newCount, type, attempt); //Generate another later
return; return;
} }
@ -130,8 +137,31 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
BetterRTP.debug("Queue max reached for " + type + " (amount: " + applicable.size() + ") lastCount: " + lastCount); BetterRTP.debug("Queue max reached for " + type + " (amount: " + applicable.size() + ") lastCount: " + lastCount);
} }
for (RTP_SETUP_TYPE setup : RTP_SETUP_TYPE.values()) {
HashMap<String, RTPWorld> map = getFromSetup(setup);
if (map == null) continue;
for (Map.Entry<String, RTPWorld> rtpWorldEntry : map.entrySet()) {
RTPWorld world = rtpWorldEntry.getValue();
String type = getId(setup, rtpWorldEntry.getKey());
List<QueueData> applicable = getApplicable(world);
int newCount = lastType.equalsIgnoreCase(type) ? lastCount : applicable.size();
int attempt = lastType.equalsIgnoreCase(type) ? attempts + 1 : 0;
if (newCount < queueMin && applicable.size() < queueMax) {
if (attempt > queueMaxAttempts) {
BetterRTP.debug("Max attempts to create a Queue reached for " + type + " (amount: " + applicable.size() + ")");
continue;
}
generateFromWorld(world, type);
queueGenerator(null, queueMax, queueMin, newCount, type, attempt); //Generate another later
return;
}
if (lastType.equalsIgnoreCase(type))
BetterRTP.debug("Max queue reached for " + type + " (amount: " + applicable.size() + ") lastCount: " + lastCount);
}
}
//Generate Defaults //Generate Defaults
WorldDefault worldDefault = BetterRTP.getInstance().getRTP().RTPdefaultWorld; /*WorldDefault worldDefault = BetterRTP.getInstance().getRTP().RTPdefaultWorld;
for (World world : Bukkit.getWorlds()) { for (World world : Bukkit.getWorlds()) {
if (!BetterRTP.getInstance().getRTP().getDisabledWorlds().contains(world.getName()) if (!BetterRTP.getInstance().getRTP().getDisabledWorlds().contains(world.getName())
&& !BetterRTP.getInstance().getRTP().RTPcustomWorld.containsKey(world.getName())) { && !BetterRTP.getInstance().getRTP().RTPcustomWorld.containsKey(world.getName())) {
@ -192,22 +222,43 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
} }
if (lastType.equalsIgnoreCase(type)) if (lastType.equalsIgnoreCase(type))
BetterRTP.debug("Queue max reached for " + type + " " + applicable.size() + " " + location.getValue().getID() + " lastCount: " + lastCount); BetterRTP.debug("Queue max reached for " + type + " " + applicable.size() + " " + location.getValue().getID() + " lastCount: " + lastCount);
} }*/
generating = false; generating = false;
BetterRTP.debug("Queueing paused, max queue limit reached!"); BetterRTP.debug("Queueing paused, max queue limit reached!");
}, 20L * 5 /*delay before starting queue generator*/); }, 20L * 5 /*delay before starting queue generator*/);
} }
//Generate a location on another thread private static HashMap<String, RTPWorld> getFromSetup(RTP_SETUP_TYPE type) {
private void generateFromWorld(RTPWorld world) { switch (type) {
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> { case LOCATION: return BetterRTP.getInstance().getRTP().RTPworldLocations;
//BetterRTP.debug("Queue attempt started..."); case CUSTOM_WORLD: return BetterRTP.getInstance().getRTP().RTPcustomWorld;
QueueData data = new QueueData(world); case DEFAULT:
addQueue(world, data); HashMap<String, RTPWorld> list = new HashMap<>();
}); RTP rtp = BetterRTP.getInstance().getRTP();
for (World world : Bukkit.getWorlds())
if (!rtp.getDisabledWorlds().contains(world.getName()) && !rtp.RTPcustomWorld.containsKey(world.getName()))
list.put(world.getName(), new WorldCustom(world, rtp.RTPdefaultWorld));
return list;
}
return null;
} }
private void addQueue(RTPWorld rtpWorld, QueueData data) { private static String getId(RTP_SETUP_TYPE type, String id) {
switch (type) {
case CUSTOM_WORLD: return "custom_" + id;
case LOCATION: return "location_" + id;
case DEFAULT: return "default_" + id;
}
return "unknown_" + id;
}
//Generate a safe location
private boolean generateFromWorld(RTPWorld world, String id) {
QueueData data = new QueueData(world);
return addQueue(world, data, id);
}
private boolean addQueue(RTPWorld rtpWorld, QueueData data, String id) {
Location loc = null; Location loc = null;
if (data.getLocation() != null) if (data.getLocation() != null)
loc = RTPPlayer.getSafeLocation( loc = RTPPlayer.getSafeLocation(
@ -221,7 +272,11 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
data.setLocation(loc); data.setLocation(loc);
if (DatabaseHandler.getQueue().addQueue(data)) { if (DatabaseHandler.getQueue().addQueue(data)) {
queueList.add(data); queueList.add(data);
BetterRTP.debug("Queue position added " + data.getLocation().toString()); String _x = String.valueOf(data.getLocation().getBlockX());
String _z = String.valueOf(data.getLocation().getBlockZ());
String _world = data.getLocation().getWorld().getName();
BetterRTP.debug("Queue position generated id= " + id +", location= x:" + _x + ", z:" + _z + ", world:" + _world);
return true;
} else } else
BetterRTP.debug("Database error occured for a queue! " + data.getLocation().toString()); BetterRTP.debug("Database error occured for a queue! " + data.getLocation().toString());
} else if (data.getLocation() != null) { } else if (data.getLocation() != null) {
@ -237,14 +292,17 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
}*/ }*/
} else } else
BetterRTP.debug("Queue position wasn't able to generate a location!"); BetterRTP.debug("Queue position wasn't able to generate a location!");
return false;
} }
public static List<QueueData> getApplicable(RTPWorld rtpWorld) { public static List<QueueData> getApplicable(RTPWorld rtpWorld) {
List<QueueData> queueData = BetterRTP.getInstance().getQueue().queueList; List<QueueData> queueData = BetterRTP.getInstance().getQueue().queueList;
List<QueueData> available = new ArrayList<>(); List<QueueData> available = new ArrayList<>();
for (QueueData data : queueData) { for (QueueData data : queueData) {
if (!Objects.equals(data.getLocation().getWorld().getName(), rtpWorld.getWorld().getName())) if (!Objects.equals(data.getLocation().getWorld().getName(), rtpWorld.getWorld().getName())) {
//BetterRTP.getInstance().getLogger().info(data.getLocation().getWorld().getName() + " != " + rtpWorld.getWorld().getName());
continue; continue;
}
switch (rtpWorld.getShape()) { switch (rtpWorld.getShape()) {
case CIRCLE: case CIRCLE:
if (isInCircle(data.location, rtpWorld)) if (isInCircle(data.location, rtpWorld))
@ -295,7 +353,7 @@ public class QueueHandler implements Listener { //Randomly queues up some safe l
if (!(xleft || xright)) if (!(xleft || xright))
return false; return false;
boolean zbottom = z <= center_z + radius && z >= center_z + radius_min; boolean zbottom = z <= center_z + radius && z >= center_z + radius_min;
boolean ztop = z >= center_z - radius && z <=center_z - radius_min; boolean ztop = z >= center_z - radius && z <= center_z - radius_min;
return ztop || zbottom; return ztop || zbottom;
} }
} }