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
Hide Players Command
/hideplayers command using AbstractCommand
Example Code
package dev.myplugin.example;
import com.hypixel.hytale.component.Ref;
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.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
// import dev.myplugin.example.BetterMapConfig; // Anonymized
// import dev.myplugin.example.MapPrivacyManager; // Anonymized
import org.checkerframework.checker.nullness.compatqual.NonNullDecl;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import java.awt.*;
import java.util.concurrent.CompletableFuture;
/**
* Command to toggle the 'hide players on map' privacy feature.
* <p>
* When enabled, all players are hidden from the world map (similar to NoPlayersOnMap).
* </p>
*/
public class ExampleCommand extends AbstractCommand {
public ExampleCommand() {
super("hideplayers", "Toggle global map privacy (Hides players from map)");
this.requirePermission(ConfigCommand.CONFIG_PERMISSION);
}
@Override
protected boolean canGeneratePermission() {
return false;
}
/**
* Executes the hide players toggle command.
*
* @param commandContext The command execution context.
* @return A CompletableFuture representing the asynchronous execution.
*/
@NullableDecl
@Override
protected CompletableFuture<Void> execute(@NonNullDecl CommandContext commandContext) {
if (!commandContext.isPlayer()) {
commandContext.sendMessage(Message.raw("This command can only be used by a player.").color(Color.RED));
return CompletableFuture.completedFuture(null);
}
Ref<EntityStore> ref = commandContext.senderAsPlayerRef();
if (ref == null) {
return CompletableFuture.completedFuture(null);
}
var store = ref.getStore();
World world = store.getExternalData().getWorld();
return CompletableFuture.runAsync(() -> {
Player playerComponent = store.getComponent(ref, Player.getComponentType());
PlayerRef playerRef = store.getComponent(ref, PlayerRef.getComponentType());
if (playerComponent == null || playerRef == null) {
return;
}
BetterMapConfig config = BetterMapConfig.getInstance();
boolean newState = !config.isHidePlayersOnMap();
config.setHidePlayersOnMap(newState);
MapPrivacyManager.getInstance().updatePrivacyState();
String status = newState ? "ENABLED" : "DISABLED";
Color color = newState ? Color.GREEN : Color.RED;
playerRef.sendMessage(Message.raw("Global Map Privacy " + status).color(color));
if (newState) {
playerRef.sendMessage(Message.raw("Players are now hidden from the world map.").color(Color.GRAY));
} else {
playerRef.sendMessage(Message.raw("Players are now visible on the world map.").color(Color.GRAY));
}
}, world);
}
}