-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable or disable stacking based on TPS hysteresis #99
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,10 @@ | |
import dev.rosewood.rosestacker.stack.settings.EntityStackSettings; | ||
import dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings; | ||
import dev.rosewood.rosestacker.utils.DataUtils; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Chunk; | ||
import org.bukkit.Location; | ||
|
@@ -46,6 +43,8 @@ public class StackManager extends Manager implements StackingLogic { | |
|
||
private boolean isEntityStackingTemporarilyDisabled; | ||
private boolean isEntityUnstackingTemporarilyDisabled; | ||
private boolean isEntityStackingEnabledForPerformance; | ||
private boolean stackingBasedOnPerformance; | ||
|
||
private StackedEntityDataStorageType entityDataStorageType; | ||
|
||
|
@@ -55,6 +54,8 @@ public StackManager(RosePlugin rosePlugin) { | |
this.stackingThreads = new ConcurrentHashMap<>(); | ||
|
||
this.isEntityStackingTemporarilyDisabled = false; | ||
this.isEntityStackingEnabledForPerformance = false; | ||
this.stackingBasedOnPerformance = false; | ||
} | ||
|
||
@Override | ||
|
@@ -70,6 +71,30 @@ public void reload() { | |
long interval = autosaveFrequency * 20 * 60; | ||
this.autosaveTask = Bukkit.getScheduler().runTaskTimer(this.rosePlugin, () -> this.saveAllData(false), interval, interval); | ||
} | ||
|
||
this.stackingBasedOnPerformance = ConfigurationManager.Setting.PERFORMANCE_TPS_TOGGLE.getBoolean(); | ||
if(this.stackingBasedOnPerformance){ | ||
Bukkit.getServer().getScheduler().runTaskAsynchronously(rosePlugin, new Runnable() { | ||
@Override | ||
public void run() { | ||
if(ConfigurationManager.Setting.PERFORMANCE_TPS_TOGGLE.getBoolean()){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why this is being checked again? You checked it above and had a variable assigned to it, but aren't using that variable here. |
||
Bukkit.getScheduler().runTaskTimerAsynchronously(rosePlugin, () -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to assign async repeating tasks to a variable and ensure they are cancelled when the plugin shuts down. See how the autosave task is handled in this class. Additionally, as it is now, since this never gets cancelled and gets run every time /rs reload gets run, you can end up with multiple of these tasks running at once, that's bad. |
||
double[] tps = Bukkit.getServer().getTPS(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Server#getTPS() is a Paper-API method and this will throw an error on Spigot servers. I believe it's only possible to check the server TPS using NMS on Spigot servers, so that functionality will need to be added to all the NMSHandler interface and implemented for each handler. |
||
if(tps.length == 0) return; | ||
double lastTPS = tps[0]; | ||
// hysteresis | ||
if(isEntityStackingEnabledForPerformance && lastTPS >= Setting.PERFORMANCE_TPS_DISABLE_ABOVE.getDouble()){ | ||
// stacking was enabled due to performance, the TPS increased, we can turn it off again | ||
isEntityStackingEnabledForPerformance=false; | ||
} else if(!isEntityStackingEnabledForPerformance && lastTPS <= Setting.PERFORMANCE_TPS_ENABLE_BELOW.getDouble()) { | ||
// stacking was disabled because the performance was above the low bound, but they have decreased past that | ||
isEntityStackingEnabledForPerformance=true; | ||
} | ||
}, 0L, 20L*30L); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -533,7 +558,7 @@ public void setEntityUnstackingTemporarilyDisabled(boolean disabled) { | |
* @return true if instant entity stacking is temporarily disabled, otherwise false | ||
*/ | ||
public boolean isEntityStackingTemporarilyDisabled() { | ||
return this.isEntityStackingTemporarilyDisabled; | ||
return this.isEntityStackingTemporarilyDisabled || (this.stackingBasedOnPerformance && !this.isEntityStackingEnabledForPerformance); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is creating this done in an async task?