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
Add Spawn Point Command
/add command using AbstractAsyncCommand
Example Code
package dev.myplugin.example;
// import dev.myplugin.example.TroubleInTrorkTownPlugin; // Anonymized
// import dev.myplugin.example.InstanceConfig; // Anonymized
// import dev.myplugin.example.SpawnPoint; // Anonymized
import com.hypixel.hytale.component.Ref;
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.AbstractAsyncCommand;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import org.checkerframework.checker.nullness.compatqual.NonNullDecl;
import java.util.concurrent.CompletableFuture;
public class ExampleCommand extends AbstractAsyncCommand {
public ExampleCommand() {
super("add", "Add spawn point for player.");
}
@NonNullDecl
@Override
protected CompletableFuture<Void> executeAsync(@NonNullDecl CommandContext ctx) {
return CompletableFuture.runAsync(() -> {
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(() -> {
var transformComponent = reference.getStore().getComponent(reference, TransformComponent.getComponentType());
if (transformComponent == null) {
ctx.sendMessage(Message.raw("An error occurred while trying to access your player information."));
return;
}
// Here you would add the logic to actually store the loot position
SpawnPoint spawnPoint = new SpawnPoint(
transformComponent.getPosition().clone(),
transformComponent.getRotation().clone()
);
String worldName = world.getWorldConfig().getDisplayName().replace(" ", "_").toLowerCase();
InstanceConfig instanceConfig = TroubleInTrorkTownPlugin.instanceConfig.get(worldName).get();
SpawnPoint[] playerSpawnPoints = instanceConfig.getPlayerSpawnPoints();
if (playerSpawnPoints == null) {
playerSpawnPoints = new SpawnPoint[0];
}
SpawnPoint[] newPlayerSpawnPoints = new SpawnPoint[playerSpawnPoints.length + 1];
System.arraycopy(playerSpawnPoints, 0, newPlayerSpawnPoints, 0, playerSpawnPoints.length);
newPlayerSpawnPoints[playerSpawnPoints.length] = spawnPoint;
instanceConfig.setPlayerSpawnPoints(newPlayerSpawnPoints);
TroubleInTrorkTownPlugin.instanceConfig.get(worldName).save();
ctx.sendMessage(Message.raw("Spawn position added at your current location."));
});
});
}
}