1010import com .hypixel .hytale .server .core .util .Config ;
1111import net .nitrado .hytale .plugins .performance_saver .chunks .ChunkGarbageCollector ;
1212import net .nitrado .hytale .plugins .performance_saver .config .PerformanceSaverPluginConfig ;
13+ import net .nitrado .hytale .plugins .performance_saver .config .RestoreConfig ;
1314import net .nitrado .hytale .plugins .performance_saver .tps .TpsAdjuster ;
1415import net .nitrado .hytale .plugins .performance_saver .viewradius .GcMonitor ;
1516import net .nitrado .hytale .plugins .performance_saver .viewradius .Monitor ;
1617import net .nitrado .hytale .plugins .performance_saver .viewradius .TpsMonitor ;
1718import net .nitrado .hytale .plugins .performance_saver .viewradius .ViewRadiusResult ;
1819
1920import javax .annotation .Nonnull ;
21+ import java .io .IOException ;
22+ import java .nio .file .Files ;
2023import java .util .concurrent .ScheduledFuture ;
2124import java .util .concurrent .TimeUnit ;
2225
2326public class PerformanceSaverPlugin extends JavaPlugin {
2427
2528 private final Config <PerformanceSaverPluginConfig > _config = withConfig (PerformanceSaverPluginConfig .CODEC );
29+
30+ // _restoreConfig saves the server's initial max view radius to disk to allow a restore after a hard server crash
31+ private final Config <RestoreConfig > _restoreConfig = withConfig ("restore" , RestoreConfig .CODEC );
2632 private PerformanceSaverPluginConfig config ;
2733
2834 private ScheduledFuture <?> viewRadiusTask ;
@@ -46,18 +52,18 @@ public PerformanceSaverPlugin(@Nonnull JavaPluginInit init) {
4652 @ Override
4753 protected void setup () {
4854 this .config = this ._config .get ();
55+ this .initialViewRadius = this ._restoreConfig .get ().getInitialViewRadius ();
4956
5057 this .gcMonitor = new GcMonitor (getLogger ().getSubLogger ("GcMonitor" ), this .config .getViewRadiusConfig ().getGcMonitorConfig ());
5158 this .tpsMonitor = new TpsMonitor (getLogger ().getSubLogger ("TpsMonitor" ), this .config .getViewRadiusConfig ().getTpsMonitorConfig ());
5259 this .chunkGarbageCollector = new ChunkGarbageCollector (getLogger ().getSubLogger ("ChunkGarbageCollector" ), this .config .getChunkGarbageCollectorConfig ());
5360 this .tpsAdjuster = new TpsAdjuster (getLogger ().getSubLogger ("TpsAdjuster" ), this .config .getTpsAdjusterConfig ());
5461 this ._config .save ();
62+ this ._restoreConfig .save ();
5563 }
5664
5765 @ Override
5866 protected void start () {
59- this .initialViewRadius = HytaleServer .get ().getConfig ().getMaxViewRadius ();
60-
6167 getLogger ().atInfo ().log ("Initial view radius is %d" , this .initialViewRadius );
6268 try {
6369
@@ -223,6 +229,13 @@ protected void shutdown() {
223229 getLogger ().atInfo ().log ("Restoring view radius to %d" , this .initialViewRadius );
224230 HytaleServer .get ().getConfig ().setMaxViewRadius (this .initialViewRadius );
225231
232+ try {
233+ // Remove restore.json again to allow manual changes to the view radius in the server config
234+ Files .deleteIfExists (getDataDirectory ().resolve ("restore.json" ));
235+ } catch (IOException e ) {
236+ getLogger ().atWarning ().withCause (e ).log ("failed to delete restore.json" );
237+ }
238+
226239 try {
227240 this .gcMonitor .stop ();
228241 this .tpsMonitor .stop ();
0 commit comments