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
Map Vote Command
/vote command using CommandBase
Example Code
package dev.myplugin.example;
// import dev.myplugin.example.PlayerGameModeInfo; // Anonymized
// import dev.myplugin.example.GameModeState; // Anonymized
// import dev.myplugin.example.MapVotePage; // Anonymized
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.protocol.packets.interface_.CustomPageLifetime;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
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 org.checkerframework.checker.nullness.compatqual.NonNullDecl;
import static ar.ncode.plugin.TroubleInTrorkTownPlugin.gameModeStateForWorld;
import static ar.ncode.plugin.TroubleInTrorkTownPlugin.worldPreviews;
public class ExampleCommand extends CommandBase {
public ExampleCommand() {
super("vote", "Command to open the map voting menu.");
super.addAliases("votemap");
}
private static void openGuiForPlayer(@NonNullDecl CommandContext ctx, Ref<EntityStore> reference) {
var playerInfo = reference.getStore().getComponent(reference, PlayerGameModeInfo.componentType);
var player = reference.getStore().getComponent(reference, Player.getComponentType());
var playerRef = reference.getStore().getComponent(reference, PlayerRef.getComponentType());
if (playerInfo == null || player == null || playerRef == null) {
ctx.sendMessage(Message.raw("An error occurred while trying to access your player information."));
return;
}
World world = player.getWorld();
if (world == null) {
ctx.sendMessage(Message.raw("An error occurred while trying to access your world information."));
return;
}
GameModeState gameModeState = gameModeStateForWorld.get(world.getWorldConfig().getUuid());
if (!gameModeState.hasLastRoundFinished()) {
ctx.sendMessage(Message.raw("You can only use this command after the last round has finished."));
return;
}
player.getPageManager().openCustomPage(
reference, reference.getStore(), new MapVotePage(playerRef, CustomPageLifetime.CanDismiss, worldPreviews,
playerInfo)
);
}
@Override
protected void executeSync(@NonNullDecl CommandContext ctx) {
Ref<EntityStore> reference = ctx.senderAsPlayerRef();
if (reference == null || !reference.isValid()) {
ctx.sendMessage(Message.raw("You can't use this command from the console."));
return;
}
var world = reference.getStore().getExternalData().getWorld();
world.execute(() -> openGuiForPlayer(ctx, reference));
}
}