-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport 1.20's 'pause-when-empty-seconds' server property (#470)
- Loading branch information
Showing
7 changed files
with
193 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...chej123/hodgepodge/mixins/early/minecraft/server/MixinDedicatedServer_PauseWhenEmpty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.mitchej123.hodgepodge.mixins.early.minecraft.server; | ||
|
||
import net.minecraft.server.dedicated.DedicatedServer; | ||
import net.minecraft.server.dedicated.PropertyManager; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import com.mitchej123.hodgepodge.mixins.interfaces.IPauseWhenEmpty; | ||
|
||
@Mixin(DedicatedServer.class) | ||
public class MixinDedicatedServer_PauseWhenEmpty implements IPauseWhenEmpty { | ||
|
||
@Shadow | ||
private PropertyManager settings; | ||
|
||
@Unique | ||
private int hodgepodge$pauseWhenEmptySeconds = 0; | ||
|
||
@Override | ||
public int getPauseWhenEmptySeconds() { | ||
return hodgepodge$pauseWhenEmptySeconds; | ||
} | ||
|
||
@Override | ||
public void setPauseWhenEmptySeconds(int value) { | ||
value = Math.max(value, 0); | ||
hodgepodge$pauseWhenEmptySeconds = value; | ||
|
||
settings.setProperty("pause-when-empty-seconds", hodgepodge$pauseWhenEmptySeconds); | ||
settings.saveProperties(); | ||
} | ||
|
||
@Inject( | ||
method = "startServer", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lcpw/mods/fml/common/FMLCommonHandler;onServerStarted()V", | ||
remap = false, | ||
shift = At.Shift.AFTER)) | ||
public void hodgepodge$setupServer(CallbackInfoReturnable<Boolean> cir) { | ||
hodgepodge$pauseWhenEmptySeconds = settings.getIntProperty("pause-when-empty-seconds", 0); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...chej123/hodgepodge/mixins/early/minecraft/server/MixinMinecraftServer_PauseWhenEmpty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.mitchej123.hodgepodge.mixins.early.minecraft.server; | ||
|
||
import net.minecraft.network.NetworkSystem; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.dedicated.DedicatedServer; | ||
import net.minecraft.server.management.ServerConfigurationManager; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import com.mitchej123.hodgepodge.Common; | ||
import com.mitchej123.hodgepodge.mixins.interfaces.IPauseWhenEmpty; | ||
|
||
@Mixin(MinecraftServer.class) | ||
public abstract class MixinMinecraftServer_PauseWhenEmpty { | ||
|
||
@Shadow | ||
public abstract int getCurrentPlayerCount(); | ||
|
||
@Shadow | ||
private ServerConfigurationManager serverConfigManager; | ||
|
||
@Shadow | ||
protected abstract void saveAllWorlds(boolean dontLog); | ||
|
||
@Shadow | ||
public abstract NetworkSystem func_147137_ag(); | ||
|
||
@Unique | ||
private int hodgepodge$emptyTicks = 0; | ||
@Unique | ||
private boolean hodgepodge$wasPaused = false; | ||
|
||
@Inject(method = "tick", at = @At("HEAD"), cancellable = true, order = 9000) | ||
public void hodgepodge$tick(CallbackInfo ci) { | ||
if ((Object) this instanceof DedicatedServer ds && ds instanceof IPauseWhenEmpty p) { | ||
int pauseTicks = p.getPauseWhenEmptySeconds() * 20; | ||
if (pauseTicks > 0) { | ||
if (this.getCurrentPlayerCount() == 0) { | ||
this.hodgepodge$emptyTicks++; | ||
} else { | ||
this.hodgepodge$emptyTicks = 0; | ||
} | ||
|
||
if (hodgepodge$emptyTicks >= pauseTicks) { | ||
if (!hodgepodge$wasPaused) { | ||
Common.log | ||
.info("Server empty for {} seconds, saving and pausing", p.getPauseWhenEmptySeconds()); | ||
this.serverConfigManager.saveAllPlayerData(); | ||
this.saveAllWorlds(true); | ||
hodgepodge$wasPaused = true; | ||
} | ||
// to finish saving chunks | ||
net.minecraftforge.common.chunkio.ChunkIOExecutor.tick(); | ||
// to process new connections | ||
this.func_147137_ag().networkTick(); | ||
// to process console commands | ||
ds.executePendingCommands(); | ||
ci.cancel(); | ||
} else if (hodgepodge$wasPaused) { | ||
Common.log.info("Resuming server"); | ||
hodgepodge$wasPaused = false; | ||
} | ||
} else if (hodgepodge$wasPaused) { | ||
Common.log.info("Resuming server"); | ||
hodgepodge$wasPaused = false; | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/mitchej123/hodgepodge/mixins/interfaces/IPauseWhenEmpty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.mitchej123.hodgepodge.mixins.interfaces; | ||
|
||
public interface IPauseWhenEmpty { | ||
|
||
int getPauseWhenEmptySeconds(); | ||
|
||
void setPauseWhenEmptySeconds(int value); | ||
} |