|
| 1 | +--- |
| 2 | +title: "Example ECS Plugin" |
| 3 | +description: "In this guide you will learn how to create a simple poison system utilising all of the features you previously learned about Hytale's ECS system" |
| 4 | +authors: |
| 5 | + - name: "oskarscot" |
| 6 | + link: "https://oskar.scot" |
| 7 | +--- |
| 8 | + |
| 9 | +## Practical Example: Poison System |
| 10 | + |
| 11 | +Putting together everything you learned so far, here's a complete poison effect. When applied to any entity, it deals damage at a set interval until it expires and removes itself. |
| 12 | + |
| 13 | + |
| 14 | +```java |
| 15 | +package scot.oskar.hytaletemplate.components; |
| 16 | + |
| 17 | +import com.hypixel.hytale.component.Component; |
| 18 | +import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; |
| 19 | +import javax.annotation.Nullable; |
| 20 | + |
| 21 | +public class PoisonComponent implements Component<EntityStore> { |
| 22 | + |
| 23 | + private float damagePerTick; |
| 24 | + private float tickInterval; |
| 25 | + private int remainingTicks; |
| 26 | + private float elapsedTime; |
| 27 | + |
| 28 | + public PoisonComponent() { |
| 29 | + this(5f, 1.0f, 10); |
| 30 | + } |
| 31 | + |
| 32 | + public PoisonComponent(float damagePerTick, float tickInterval, int totalTicks) { |
| 33 | + this.damagePerTick = damagePerTick; |
| 34 | + this.tickInterval = tickInterval; |
| 35 | + this.remainingTicks = totalTicks; |
| 36 | + this.elapsedTime = 0f; |
| 37 | + } |
| 38 | + |
| 39 | + public PoisonComponent(PoisonComponent other) { |
| 40 | + this.damagePerTick = other.damagePerTick; |
| 41 | + this.tickInterval = other.tickInterval; |
| 42 | + this.remainingTicks = other.remainingTicks; |
| 43 | + this.elapsedTime = other.elapsedTime; |
| 44 | + } |
| 45 | + |
| 46 | + @Nullable |
| 47 | + @Override |
| 48 | + public Component<EntityStore> clone() { |
| 49 | + return new PoisonComponent(this); |
| 50 | + } |
| 51 | + |
| 52 | + public float getDamagePerTick() { |
| 53 | + return damagePerTick; |
| 54 | + } |
| 55 | + |
| 56 | + public float getTickInterval() { |
| 57 | + return tickInterval; |
| 58 | + } |
| 59 | + |
| 60 | + public int getRemainingTicks() { |
| 61 | + return remainingTicks; |
| 62 | + } |
| 63 | + |
| 64 | + public float getElapsedTime() { |
| 65 | + return elapsedTime; |
| 66 | + } |
| 67 | + |
| 68 | + public void addElapsedTime(float dt) { |
| 69 | + this.elapsedTime += dt; |
| 70 | + } |
| 71 | + |
| 72 | + public void resetElapsedTime() { |
| 73 | + this.elapsedTime = 0f; |
| 74 | + } |
| 75 | + |
| 76 | + public void decrementRemainingTicks() { |
| 77 | + this.remainingTicks--; |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isExpired() { |
| 81 | + return this.remainingTicks <= 0; |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +```java |
| 87 | +package scot.oskar.hytaletemplate.systems; |
| 88 | + |
| 89 | +import com.hypixel.hytale.component.ArchetypeChunk; |
| 90 | +import com.hypixel.hytale.component.CommandBuffer; |
| 91 | +import com.hypixel.hytale.component.ComponentType; |
| 92 | +import com.hypixel.hytale.component.Ref; |
| 93 | +import com.hypixel.hytale.component.Store; |
| 94 | +import com.hypixel.hytale.component.SystemGroup; |
| 95 | +import com.hypixel.hytale.component.query.Query; |
| 96 | +import com.hypixel.hytale.component.system.tick.EntityTickingSystem; |
| 97 | +import com.hypixel.hytale.server.core.modules.entity.damage.Damage; |
| 98 | +import com.hypixel.hytale.server.core.modules.entity.damage.DamageCause; |
| 99 | +import com.hypixel.hytale.server.core.modules.entity.damage.DamageModule; |
| 100 | +import com.hypixel.hytale.server.core.modules.entity.damage.DamageSystems; |
| 101 | +import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; |
| 102 | +import javax.annotation.Nonnull; |
| 103 | +import javax.annotation.Nullable; |
| 104 | +import scot.oskar.hytaletemplate.components.PoisonComponent; |
| 105 | + |
| 106 | +public class PoisonSystem extends EntityTickingSystem<EntityStore> { |
| 107 | + |
| 108 | + private final ComponentType<EntityStore, PoisonComponent> poisonComponentType; |
| 109 | + |
| 110 | + public PoisonSystem(ComponentType<EntityStore, PoisonComponent> poisonComponentType) { |
| 111 | + this.poisonComponentType = poisonComponentType; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void tick(float dt, int index, @Nonnull ArchetypeChunk<EntityStore> archetypeChunk, |
| 116 | + @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) { |
| 117 | + |
| 118 | + PoisonComponent poison = archetypeChunk.getComponent(index, poisonComponentType); |
| 119 | + Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index); |
| 120 | + |
| 121 | + poison.addElapsedTime(dt); |
| 122 | + |
| 123 | + if (poison.getElapsedTime() >= poison.getTickInterval()) { |
| 124 | + poison.resetElapsedTime(); |
| 125 | + |
| 126 | + Damage damage = new Damage(Damage.NULL_SOURCE, DamageCause.OUT_OF_WORLD, poison.getDamagePerTick()); |
| 127 | + DamageSystems.executeDamage(ref, commandBuffer, damage); |
| 128 | + |
| 129 | + poison.decrementRemainingTicks(); |
| 130 | + } |
| 131 | + |
| 132 | + if (poison.isExpired()) { |
| 133 | + commandBuffer.removeComponent(ref, poisonComponentType); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Nullable |
| 138 | + @Override |
| 139 | + public SystemGroup<EntityStore> getGroup() { |
| 140 | + return DamageModule.get().getGatherDamageGroup(); |
| 141 | + } |
| 142 | + |
| 143 | + @Nonnull |
| 144 | + @Override |
| 145 | + public Query<EntityStore> getQuery() { |
| 146 | + return Query.and(this.poisonComponentType); |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +```java |
| 152 | +package scot.oskar.hytaletemplate.commands; |
| 153 | + |
| 154 | +public class ExampleCommand extends AbstractPlayerCommand { |
| 155 | + |
| 156 | + public ExampleCommand() { |
| 157 | + super("test", "Super test command!"); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + protected void execute(@Nonnull CommandContext commandContext, @Nonnull Store<EntityStore> store, |
| 162 | + @Nonnull Ref<EntityStore> ref, @Nonnull PlayerRef playerRef, @Nonnull World world) { |
| 163 | + Player player = store.getComponent(ref, Player.getComponentType()); |
| 164 | + PoisonComponent poison = new PoisonComponent(3f, 0.5f, 8); |
| 165 | + store.addComponent(ref, ExamplePlugin.get().getPoisonComponentType(), poison); |
| 166 | + player.sendMessage(Message.raw("You have been poisoned!").color(Color.GREEN).bold(true)); |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +```java |
| 172 | +package scot.oskar.hytaletemplate; |
| 173 | + |
| 174 | +public final class ExamplePlugin extends JavaPlugin { |
| 175 | + |
| 176 | + private static ExamplePlugin instance; |
| 177 | + private ComponentType<EntityStore, PoisonComponent> poisonComponent; |
| 178 | + |
| 179 | + public ExamplePlugin(@Nonnull JavaPluginInit init) { |
| 180 | + super(init); |
| 181 | + instance = this; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + protected void setup() { |
| 186 | + this.getCommandRegistry().registerCommand(new ExampleCommand()); |
| 187 | + this.getEventRegistry().registerGlobal(PlayerReadyEvent.class, ExampleEvent::onPlayerReady); |
| 188 | + this.getEventRegistry().registerGlobal(PlayerChatEvent.class, ChatFormatter::onPlayerChat); |
| 189 | + |
| 190 | + this.poisonComponent = this.getEntityStoreRegistry() |
| 191 | + .registerComponent(PoisonComponent.class, PoisonComponent::new); |
| 192 | + this.getEntityStoreRegistry().registerSystem(new PoisonSystem(this.poisonComponent)); |
| 193 | + } |
| 194 | + |
| 195 | + public ComponentType<EntityStore, PoisonComponent> getPoisonComponentType() { |
| 196 | + return poisonComponent; |
| 197 | + } |
| 198 | + |
| 199 | + public static ExamplePlugin get() { |
| 200 | + return instance; |
| 201 | + } |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +The query uses only `poisonComponentType` which means the system will process any entity with a `PoisonComponent`, not just players. This makes it flexible for poisoning NPCs, mobs, or any living entity. The system places itself in the `GatherDamageGroup` so the damage it creates flows through the full damage pipeline including armor reduction and invulnerability checks. |
| 206 | + |
0 commit comments