|
| 1 | +--- |
| 2 | +title: "Player Stats" |
| 3 | +description: "A simple guide on how to edit player stats." |
| 4 | +authors: |
| 5 | + - name: "Bird" |
| 6 | + link: "https://discord.com/users/495709580068651009" |
| 7 | +--- |
| 8 | + |
| 9 | +# Overview |
| 10 | + |
| 11 | +This guide shows you how to read and modify player stats like health and stamina using the `EntityStatMap` component. |
| 12 | + |
| 13 | +## Available Stats |
| 14 | + |
| 15 | +Hytale provides several default stats via `DefaultEntityStatTypes`: |
| 16 | + |
| 17 | +- **Health**: `DefaultEntityStatTypes.getHealth()` |
| 18 | +- **Stamina**: `DefaultEntityStatTypes.getStamina()` |
| 19 | +- **Mana**: `DefaultEntityStatTypes.getMana()` |
| 20 | +- **Oxygen**: `DefaultEntityStatTypes.getOxygen()` |
| 21 | +- **Signature Energy**: `DefaultEntityStatTypes.getSignatureEnergy()` |
| 22 | +- **Ammo**: `DefaultEntityStatTypes.getAmmo()` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Example 1: Heal Command |
| 27 | + |
| 28 | +This command restores the player's health to its maximum value. |
| 29 | + |
| 30 | + |
| 31 | +```java |
| 32 | +public class HealCommand extends CommandBase { |
| 33 | + public HealCommand() { |
| 34 | + super("heal", "Restores your health to maximum."); |
| 35 | + this.setPermissionGroup(GameMode.Adventure); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void executeSync(@Nonnull CommandContext ctx) { |
| 40 | + // 1. Get the player reference |
| 41 | + Ref<EntityStore> playerRef = ctx.senderAsPlayerRef(); |
| 42 | + if (playerRef == null) return; |
| 43 | + |
| 44 | + // 2. Get the store and the world |
| 45 | + Store<EntityStore> store = playerRef.getStore(); |
| 46 | + EntityStore entityStore = store.getExternalData(); |
| 47 | + World world = entityStore.getWorld(); |
| 48 | + |
| 49 | + // 3. Perform modification on the world thread |
| 50 | + world.execute(() -> { |
| 51 | + EntityStatMap statMap = (EntityStatMap) store.getComponent(playerRef, EntityStatMap.getComponentType()); |
| 52 | + if (statMap != null) { |
| 53 | + statMap.maximizeStatValue(DefaultEntityStatTypes.getHealth()); |
| 54 | + ctx.sendMessage(Message.raw("Your health has been restored!")); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Example 2: Damage Self Command |
| 64 | + |
| 65 | +This command removes a specific amount of health from the player. |
| 66 | + |
| 67 | +```java |
| 68 | +public class DamageSelfCommand extends CommandBase { |
| 69 | + private final Argument amountArg; |
| 70 | + |
| 71 | + public DamageSelfCommand() { |
| 72 | + super("damageself", "Damages yourself by a specific amount."); |
| 73 | + this.setPermissionGroup(GameMode.Adventure); |
| 74 | + this.amountArg = this.withRequiredArg("amount", "Amount of damage", ArgTypes.FLOAT); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void executeSync(@Nonnull CommandContext ctx) { |
| 79 | + // 1. Get the player reference |
| 80 | + Ref<EntityStore> playerRef = ctx.senderAsPlayerRef(); |
| 81 | + if (playerRef == null) return; |
| 82 | + |
| 83 | + // 2. Get command arg |
| 84 | + Float amount = (Float) this.amountArg.get(ctx); |
| 85 | + if (amount == null) return; |
| 86 | + |
| 87 | + // 3. Get the store and the world |
| 88 | + Store<EntityStore> store = playerRef.getStore(); |
| 89 | + EntityStore entityStore = store.getExternalData(); |
| 90 | + World world = entityStore.getWorld(); |
| 91 | + |
| 92 | + // 4. Perform modification on the world thread |
| 93 | + world.execute(() -> { |
| 94 | + EntityStatMap statMap = (EntityStatMap) store.getComponent(playerRef, EntityStatMap.getComponentType()); |
| 95 | + if (statMap != null) { |
| 96 | + statMap.subtractStatValue(DefaultEntityStatTypes.getHealth(), amount); |
| 97 | + ctx.sendMessage(Message.raw("Ouch! You took " + amount + " damage.")); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## Useful Methods |
| 107 | + |
| 108 | +Common methods available on `EntityStatMap`: |
| 109 | + |
| 110 | +- **Set**: `statMap.setStatValue(statIndex, value)` |
| 111 | +- **Add**: `statMap.addStatValue(statIndex, amount)` |
| 112 | +- **Subtract**: `statMap.subtractStatValue(statIndex, amount)` |
| 113 | +- **Maximize**: `statMap.maximizeStatValue(statIndex)` |
| 114 | +- **Reset**: `statMap.resetStatValue(statIndex)` |
0 commit comments