Skip to content

Commit

Permalink
Option for adjusting view distance in reactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
froobynooby committed Nov 19, 2023
1 parent 29fdc10 commit 32d38e2
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 94 deletions.
52 changes: 5 additions & 47 deletions src/main/java/com/froobworld/viewdistancetweaks/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class TaskManager {
private ViewDistanceLimiter noTickLimiterTask;
private ViewDistanceClamper simulationDistanceClamper;
private ViewDistanceClamper viewDistanceClamper;
private AdjustmentModes adjustmentModes;
private Listener clamperListener;
private ManualViewDistanceManager manualSimulationDistanceManager;
private ManualViewDistanceManager manualViewDistanceManager;
Expand All @@ -39,6 +40,8 @@ public void init() {
initMsptTracker();
initViewDistanceClampers();
initManualViewDistanceManagers();
adjustmentModes = new AdjustmentModes(viewDistanceTweaks);
adjustmentModes.init();
initLimiterTask();
initNoTickLimiterTask();
}
Expand Down Expand Up @@ -87,45 +90,10 @@ private void initManualViewDistanceManagers() {
}

private void initLimiterTask() {
AdjustmentMode proactiveAdjustmentMode = null;
AdjustmentMode reactiveAdjustmentMode = null;
AdjustmentMode.Mode mode = viewDistanceTweaks.getVdtConfig().adjustmentMode.get();
if (mode == AdjustmentMode.Mode.REACTIVE || mode == AdjustmentMode.Mode.MIXED) {
reactiveAdjustmentMode = new ReactiveAdjustmentMode(
msptTracker,
viewDistanceTweaks.getHookManager().getChunkCounter(),
viewDistanceTweaks.getVdtConfig().reactiveMode.increaseMsptThreshold.get(),
viewDistanceTweaks.getVdtConfig().reactiveMode.decreaseMsptThreshold.get(),
viewDistanceTweaks.getVdtConfig().reactiveMode.msptPrediction.historyLength.get(),
viewDistanceTweaks.getVdtConfig().reactiveMode.msptPrediction.enabled.get(),
viewDistanceTweaks.getHookManager().getSimulationDistanceHook(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).simulationDistance.exclude.get(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).simulationDistance.maximumSimulationDistance.get(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).simulationDistance.minimumSimulationDistance.get(),
viewDistanceTweaks.getVdtConfig().passedChecksForIncrease.get(),
viewDistanceTweaks.getVdtConfig().passedChecksForDecrease.get()
);
}
if (mode == AdjustmentMode.Mode.PROACTIVE || mode == AdjustmentMode.Mode.MIXED) {
proactiveAdjustmentMode = new ProactiveAdjustmentMode(
viewDistanceTweaks.getVdtConfig().proactiveMode.globalTickingChunkCountTarget.get(),
viewDistanceTweaks.getHookManager().getSimulationDistanceHook(),
viewDistanceTweaks.getHookManager().getChunkCounter(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).simulationDistance.exclude.get(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).simulationDistance.maximumSimulationDistance.get(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).simulationDistance.minimumSimulationDistance.get(),
viewDistanceTweaks.getVdtConfig().passedChecksForIncrease.get(),
viewDistanceTweaks.getVdtConfig().passedChecksForDecrease.get()
);
}
AdjustmentMode adjustmentMode = mode == AdjustmentMode.Mode.REACTIVE ? reactiveAdjustmentMode
: (mode == AdjustmentMode.Mode.PROACTIVE ? proactiveAdjustmentMode
: new MixedAdjustmentMode(proactiveAdjustmentMode, reactiveAdjustmentMode, AdjustmentMode.Adjustment::strongest));

limiterTask = new ViewDistanceLimiter(
viewDistanceTweaks,
viewDistanceTweaks.getHookManager().getSimulationDistanceHook(),
adjustmentMode,
adjustmentModes.getSimulationDistanceAdjustmentMode(),
manualSimulationDistanceManager,
viewDistanceTweaks.getVdtConfig().logChanges.get(),
"Changed simulation distance of {0} ({1} -> {2})"
Expand All @@ -136,20 +104,10 @@ private void initLimiterTask() {
private void initNoTickLimiterTask() {
SimulationDistanceHook noTickViewDistanceHook = viewDistanceTweaks.getHookManager().getViewDistanceHook();
if (noTickViewDistanceHook != null) {
AdjustmentMode noTickAdjustmentMode = new ProactiveAdjustmentMode(
viewDistanceTweaks.getVdtConfig().proactiveMode.globalNonTickingChunkCountTarget.get(),
noTickViewDistanceHook,
viewDistanceTweaks.getHookManager().getNoTickChunkCounter(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).viewDistance.exclude.get(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).viewDistance.maximumViewDistance.get(),
world -> viewDistanceTweaks.getVdtConfig().worldSettings.of(world).viewDistance.minimumViewDistance.get(),
viewDistanceTweaks.getVdtConfig().passedChecksForIncrease.get(),
viewDistanceTweaks.getVdtConfig().passedChecksForDecrease.get()
);
noTickLimiterTask = new ViewDistanceLimiter(
viewDistanceTweaks,
noTickViewDistanceHook,
noTickAdjustmentMode,
adjustmentModes.getViewDistanceAdjustmentMode(),
manualViewDistanceManager,
viewDistanceTweaks.getVdtConfig().logChanges.get(),
"Changed view distance of {0} ({1} -> {2})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.io.File;

public class VdtConfig extends NabConfiguration {
public static final int VERSION = 8;
public static final int VERSION = 9;

public VdtConfig(ViewDistanceTweaks viewDistanceTweaks) {
super(
Expand Down Expand Up @@ -51,12 +51,25 @@ public static class ReactiveModeSettings extends ConfigSection {
@Entry(key = "increase-mspt-threshold")
public final ConfigEntry<Double> increaseMsptThreshold = ConfigEntries.doubleEntry();

@Section(key = "reactive-view-distance")
public final ReactiveViewDistanceSettings reactiveViewDistance = new ReactiveModeSettings.ReactiveViewDistanceSettings();

@Section(key = "mspt-prediction")
public final ReactiveModeSettings.MsptPredictionSettings msptPrediction = new ReactiveModeSettings.MsptPredictionSettings();

@Section(key = "mspt-tracker-settings")
public final ReactiveModeSettings.MsptTrackerSettings msptTracker = new ReactiveModeSettings.MsptTrackerSettings();

public static class ReactiveViewDistanceSettings extends ConfigSection {

@Entry(key = "use-reactive-view-distance")
public final ConfigEntry<Boolean> useReactiveViewDistance = new ConfigEntry<>();

@Entry(key = "target-view-distance-ratio")
public final ConfigEntry<Double> targetViewDistanceRatio = ConfigEntries.doubleEntry();

}

public static class MsptTrackerSettings extends ConfigSection {

@Entry(key = "collection-period")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ViewDistanceLimiter implements Runnable {
private final SimulationDistanceHook simulationDistanceHook;
private final AdjustmentMode adjustmentMode;
private final ManualViewDistanceManager manualViewDistanceManager;
private final Set<ViewDistanceChangeTask> changeViewDistanceTasks = new HashSet<>();
private final boolean logChanges;
private final String logFormat;
private Integer taskId;
Expand All @@ -32,30 +31,25 @@ public ViewDistanceLimiter(ViewDistanceTweaks viewDistanceTweaks, SimulationDist

@Override
public void run() {
if (changeViewDistanceTasks.isEmpty()) {
List<World> nonEmptyWorlds = Bukkit.getWorlds().stream()
.filter(world -> !world.getPlayers().isEmpty())
.filter(world -> !manualViewDistanceManager.hasManuallySetViewDistance(world))
.collect(Collectors.toList());
Map<World, AdjustmentMode.Adjustment> adjustments = nonEmptyWorlds.isEmpty() ? Collections.emptyMap() : adjustmentMode.getAdjustments(nonEmptyWorlds);
List<World> nonEmptyWorlds = Bukkit.getWorlds().stream()
.filter(world -> !world.getPlayers().isEmpty())
.filter(world -> !manualViewDistanceManager.hasManuallySetViewDistance(world))
.collect(Collectors.toList());
Map<World, AdjustmentMode.Adjustment> adjustments = nonEmptyWorlds.isEmpty() ? Collections.emptyMap() : adjustmentMode.getAdjustments(nonEmptyWorlds);

for (Map.Entry<World, AdjustmentMode.Adjustment> entry : adjustments.entrySet()) {
World world = entry.getKey();
AdjustmentMode.Adjustment adjustment = entry.getValue();
int oldViewDistance = simulationDistanceHook.getDistance(world);
for (Map.Entry<World, AdjustmentMode.Adjustment> entry : adjustments.entrySet()) {
World world = entry.getKey();
AdjustmentMode.Adjustment adjustment = entry.getValue();
int oldViewDistance = simulationDistanceHook.getDistance(world);

if (adjustment != AdjustmentMode.Adjustment.STAY) {
int newViewDistance = oldViewDistance + (adjustment == AdjustmentMode.Adjustment.INCREASE ? 1 : -1);
ViewDistanceChangeTask changeTask = new ViewDistanceChangeTask(viewDistanceTweaks, simulationDistanceHook, world, newViewDistance, 0);
changeTask.run();
changeViewDistanceTasks.add(changeTask);
if (logChanges) {
viewDistanceTweaks.getLogger().info(MessageFormat.format(logFormat, world.getName(), oldViewDistance, newViewDistance));
}
if (adjustment != AdjustmentMode.Adjustment.STAY) {
int newViewDistance = oldViewDistance + (adjustment == AdjustmentMode.Adjustment.INCREASE ? 1 : -1);
simulationDistanceHook.setDistance(world, newViewDistance);
if (logChanges) {
viewDistanceTweaks.getLogger().info(MessageFormat.format(logFormat, world.getName(), oldViewDistance, newViewDistance));
}
}
}
changeViewDistanceTasks.removeIf(ViewDistanceChangeTask::completed);
}

public void start(long period, long startUpDelay) {
Expand All @@ -68,8 +62,6 @@ public void start(long period, long startUpDelay) {
public void cancel() {
if (taskId != null) {
Bukkit.getScheduler().cancelTask(taskId);
changeViewDistanceTasks.forEach(ViewDistanceChangeTask::cancel);
changeViewDistanceTasks.clear();
taskId = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

public interface AdjustmentMode {

Map<World, Adjustment> getAdjustments(Collection<World> worlds);
Map<World, Adjustment> getAdjustments(Collection<World> worlds, boolean mutate);

default Map<World, Adjustment> getAdjustments(Collection<World> worlds) {
return getAdjustments(worlds, true);
}

enum Adjustment {
INCREASE,
Expand Down
Loading

0 comments on commit 32d38e2

Please sign in to comment.