Code Patterns
Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.
All
225
Command
60
Damage
2
Ecs
27
Entity
5
Gui
75
Interaction
2
Inventory
4
Moderation
3
Permission
2
Player
9
Plugin
4
Storage
2
Teleport
3
Ui
22
Util
5
← Back to Patterns
java
command
Auto Save Interval Command
/autosave command using AbstractCommand
Example Code
package dev.myplugin.example;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
// import dev.myplugin.example.BetterMapConfig; // Anonymized
// import dev.myplugin.example.ExplorationManager; // Anonymized
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.util.concurrent.CompletableFuture;
/**
* Command to get or set the auto-save interval.
*/
public class ExampleCommand extends AbstractCommand {
private final OptionalArg<Integer> intervalArg = this.withOptionalArg("interval", "Auto-save interval in minutes", ArgTypes.INTEGER);
/**
* Constructs the ExampleCommand.
*/
public ExampleCommand() {
super("autosave", "Get or set the exploration auto-save interval");
this.requirePermission(ConfigCommand.CONFIG_PERMISSION);
}
@Override
protected boolean canGeneratePermission() {
return false;
}
@Nullable
@Override
protected CompletableFuture<Void> execute(@Nonnull CommandContext context) {
Integer interval = context.get(this.intervalArg);
if (interval == null) {
int current = BetterMapConfig.getInstance().getAutoSaveInterval();
context.sendMessage(Message.raw("Current auto-save interval: " + current + " minutes.").color(Color.YELLOW));
} else {
if (interval < 0) {
context.sendMessage(Message.raw("Interval cannot be negative.").color(Color.RED));
return CompletableFuture.completedFuture(null);
}
BetterMapConfig.getInstance().setAutoSaveInterval(interval);
ExplorationManager.getInstance().startAutoSave();
context.sendMessage(Message.raw("Auto-save interval set to " + interval + " minutes.").color(Color.GREEN));
if (interval == 0) {
context.sendMessage(Message.raw("Auto-save is now DISABLED.").color(Color.YELLOW));
}
}
return CompletableFuture.completedFuture(null);
}
}