-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/vanish' into dev
- Loading branch information
Showing
13 changed files
with
240 additions
and
12 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
12 changes: 12 additions & 0 deletions
12
src/main/java/serverutils/command/pausewhenempty/CmdPauseWhenEmpty.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,12 @@ | ||
package serverutils.command.pausewhenempty; | ||
|
||
import serverutils.lib.command.CmdTreeBase; | ||
|
||
public class CmdPauseWhenEmpty extends CmdTreeBase { | ||
|
||
public CmdPauseWhenEmpty() { | ||
super("pause_when_empty"); | ||
addSubcommand(new CmdPauseWhenEmptySet()); | ||
addSubcommand(new CmdPauseWhenEmptyOneshot()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/serverutils/command/pausewhenempty/CmdPauseWhenEmptyOneshot.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,26 @@ | ||
package serverutils.command.pausewhenempty; | ||
|
||
import net.minecraft.command.ICommandSender; | ||
import net.minecraft.server.MinecraftServer; | ||
|
||
import serverutils.ServerUtilities; | ||
import serverutils.data.IPauseWhenEmpty; | ||
import serverutils.lib.command.CmdBase; | ||
|
||
public class CmdPauseWhenEmptyOneshot extends CmdBase { | ||
|
||
public CmdPauseWhenEmptyOneshot() { | ||
super("oneshot", Level.OP); | ||
} | ||
|
||
@Override | ||
public void processCommand(ICommandSender sender, String[] args) { | ||
checkArgs(sender, args, 1); | ||
int newValue = parseIntWithMin(sender, args[0], -1); | ||
|
||
if (MinecraftServer.getServer() instanceof IPauseWhenEmpty pauseWhenEmpty) { | ||
pauseWhenEmpty.serverUtilities$setPauseWhenEmptySeconds(newValue, true); | ||
sender.addChatMessage(ServerUtilities.lang(sender, "cmd.pause_when_empty_oneshot", newValue)); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/serverutils/command/pausewhenempty/CmdPauseWhenEmptySet.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,26 @@ | ||
package serverutils.command.pausewhenempty; | ||
|
||
import net.minecraft.command.ICommandSender; | ||
import net.minecraft.server.MinecraftServer; | ||
|
||
import serverutils.ServerUtilities; | ||
import serverutils.data.IPauseWhenEmpty; | ||
import serverutils.lib.command.CmdBase; | ||
|
||
public class CmdPauseWhenEmptySet extends CmdBase { | ||
|
||
CmdPauseWhenEmptySet() { | ||
super("set", Level.OP); | ||
} | ||
|
||
@Override | ||
public void processCommand(ICommandSender sender, String[] args) { | ||
checkArgs(sender, args, 1); | ||
int newValue = parseIntWithMin(sender, args[0], 0); | ||
|
||
if (MinecraftServer.getServer() instanceof IPauseWhenEmpty pauseWhenEmpty) { | ||
pauseWhenEmpty.serverUtilities$setPauseWhenEmptySeconds(newValue, false); | ||
sender.addChatMessage(ServerUtilities.lang(sender, "cmd.pause_when_empty_updated", newValue)); | ||
} | ||
} | ||
} |
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 serverutils.data; | ||
|
||
public interface IPauseWhenEmpty { | ||
|
||
int serverUtilities$getPauseWhenEmptySeconds(); | ||
|
||
void serverUtilities$setPauseWhenEmptySeconds(int value, boolean oneshot); | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/mixins/java/serverutils/mixins/early/minecraft/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,54 @@ | ||
package serverutils.mixins.early.minecraft; | ||
|
||
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 serverutils.data.IPauseWhenEmpty; | ||
|
||
@Mixin(DedicatedServer.class) | ||
public class MixinDedicatedServer_PauseWhenEmpty implements IPauseWhenEmpty { | ||
|
||
@Shadow | ||
private PropertyManager settings; | ||
|
||
@Unique | ||
private int serverUtilities$pauseWhenEmptySeconds = 0; | ||
|
||
@Unique | ||
private int serverUtilities$pauseWhenEmptySecondsOneShot = -1; | ||
|
||
@Override | ||
public int serverUtilities$getPauseWhenEmptySeconds() { | ||
return serverUtilities$pauseWhenEmptySecondsOneShot > -1 ? serverUtilities$pauseWhenEmptySecondsOneShot | ||
: serverUtilities$pauseWhenEmptySeconds; | ||
} | ||
|
||
@Override | ||
public void serverUtilities$setPauseWhenEmptySeconds(int value, boolean oneshot) { | ||
if (oneshot) { | ||
serverUtilities$pauseWhenEmptySecondsOneShot = Math.max(value, -1); | ||
} else { | ||
serverUtilities$pauseWhenEmptySeconds = Math.max(value, 0); | ||
settings.setProperty("pause-when-empty-seconds", serverUtilities$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 su$setupServer(CallbackInfoReturnable<Boolean> cir) { | ||
serverUtilities$pauseWhenEmptySeconds = settings.getIntProperty("pause-when-empty-seconds", 0); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/mixins/java/serverutils/mixins/early/minecraft/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,80 @@ | ||
package serverutils.mixins.early.minecraft; | ||
|
||
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 serverutils.ServerUtilities; | ||
import serverutils.data.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(); | ||
|
||
@Shadow | ||
public abstract int getTickCounter(); | ||
|
||
@Unique | ||
private int serverUtilities$emptyTicks = 0; | ||
@Unique | ||
private boolean serverUtilities$wasPaused = false; | ||
|
||
@Inject(method = "tick", at = @At("HEAD"), cancellable = true, order = 9000) | ||
public void serverUtilities$tick(CallbackInfo ci) { | ||
if ((Object) this instanceof DedicatedServer ds && ds instanceof IPauseWhenEmpty p) { | ||
int pauseTicks = p.serverUtilities$getPauseWhenEmptySeconds() * 20; | ||
if (pauseTicks > 0) { | ||
if (this.getCurrentPlayerCount() == 0) { | ||
this.serverUtilities$emptyTicks++; | ||
} else { | ||
this.serverUtilities$emptyTicks = 0; | ||
} | ||
|
||
if (serverUtilities$emptyTicks >= pauseTicks) { | ||
if (!serverUtilities$wasPaused) { | ||
ServerUtilities.LOGGER.info( | ||
"Server empty for {} seconds, saving and pausing", | ||
p.serverUtilities$getPauseWhenEmptySeconds()); | ||
this.serverConfigManager.saveAllPlayerData(); | ||
this.saveAllWorlds(true); | ||
serverUtilities$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(); | ||
return; | ||
} | ||
} | ||
|
||
if (serverUtilities$wasPaused) { | ||
ServerUtilities.LOGGER.info("Resuming server"); | ||
serverUtilities$wasPaused = false; | ||
// reset the oneshot value to -1 | ||
p.serverUtilities$setPauseWhenEmptySeconds(-1, true); | ||
} | ||
} | ||
} | ||
} |