Skip to content

Commit

Permalink
Add command to change server property
Browse files Browse the repository at this point in the history
  • Loading branch information
Caedis committed Jan 10, 2025
1 parent afe30f9 commit 3144f44
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
49 changes: 43 additions & 6 deletions src/main/java/com/mitchej123/hodgepodge/commands/DebugCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.mitchej123.hodgepodge.config.TweaksConfig;
import com.mitchej123.hodgepodge.mixins.interfaces.IPauseWhenEmpty;
import com.mitchej123.hodgepodge.util.AnchorAlarm;

public class DebugCommand extends CommandBase {
Expand All @@ -26,28 +28,31 @@ public String getCommandName() {

@Override
public String getCommandUsage(ICommandSender sender) {
return "Usage: hp <subcommand>. Valid subcommands are: toggle, anchor, randomNbt.";
return "Usage: hp <subcommand>. Valid subcommands are: toggle, anchor, randomNbt, pauseWhenEmpty.";
}

private void printHelp(ICommandSender sender) {
sender.addChatMessage(new ChatComponentText("Usage: hp <toggle|anchor|randomNbt>"));
sender.addChatMessage(new ChatComponentText("Usage: hp <toggle|anchor|randomNbt|pause_when_empty>"));
sender.addChatMessage(new ChatComponentText("\"toggle anchordebug\" - toggles RC anchor debugging"));
sender.addChatMessage(
new ChatComponentText(
"\"anchor list [player]\" - list RC anchors placed by the player (empty for current player)"));
sender.addChatMessage(
new ChatComponentText(
"\"randomNbt [bytes]\" - adds a random byte array of the given size to the held item"));
sender.addChatMessage(
new ChatComponentText(
"\"pauseWhenEmpty [seconds]\" - sets the time the server will wait before pausing when no players are online; 0 to disable"));
}

@Override
public List<String> addTabCompletionOptions(ICommandSender sender, String[] ss) {
List<String> l = new ArrayList<>();
String test = ss.length == 0 ? "" : ss[0].trim();
if (ss.length == 0 || ss.length == 1
&& (test.isEmpty() || Stream.of("toggle", "anchor", "randomNbt").anyMatch(s -> s.startsWith(test)))) {
Stream.of("toggle", "anchor", "randomNbt").filter(s -> test.isEmpty() || s.startsWith(test))
.forEach(l::add);
if (ss.length == 0 || ss.length == 1 && (test.isEmpty()
|| Stream.of("toggle", "anchor", "randomNbt", "pauseWhenEmpty").anyMatch(s -> s.startsWith(test)))) {
Stream.of("toggle", "anchor", "randomNbt", "pause_when_empty")
.filter(s -> test.isEmpty() || s.startsWith(test)).forEach(l::add);
} else if (test.equals("toggle")) {
String test1 = ss[1].trim();
if (test1.isEmpty() || "anchordebug".startsWith(test1)) l.add("anchordebug");
Expand Down Expand Up @@ -112,6 +117,38 @@ public void processCommand(ICommandSender sender, String[] strings) {
stack.stackTagCompound.setByteArray("DebugJunk", randomData);
player.inventory.inventoryChanged = true;
player.inventoryContainer.detectAndSendChanges();
break;
case "pauseWhenEmpty":
if (!TweaksConfig.pauseWhenEmpty) {
sender.addChatMessage(new ChatComponentText("'pauseWhenEmpty' config option is disabled"));
return;
}
if (strings.length < 2) {
printHelp(sender);
return;
}
final int pauseTimeout = NumberUtils.toInt(strings[1], -1);
if (pauseTimeout < 0) {
printHelp(sender);
return;
}

MinecraftServer server = MinecraftServer.getServer();
if (!server.isDedicatedServer()) {
sender.addChatMessage(
new ChatComponentText("'hp pauseWhenEmpty' only applies to dedicated servers"));
return;
}

if (server instanceof IPauseWhenEmpty p) {
p.setPauseWhenEmptySeconds(pauseTimeout);
sender.addChatMessage(
new ChatComponentText(
String.format(
"Server property 'pause-when-empty-seconds' set to %d",
pauseTimeout)));
}

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public abstract class MixinMinecraftServer_PauseWhenEmpty {

@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) {
Expand All @@ -45,19 +47,27 @@ public abstract class MixinMinecraftServer_PauseWhenEmpty {
}

if (hodgepodge$emptyTicks >= pauseTicks) {
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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public interface IPauseWhenEmpty {

int getPauseWhenEmptySeconds();

void setPauseWhenEmptySeconds(int value);
}

0 comments on commit 3144f44

Please sign in to comment.