package com.hypixel.hytale.server.core.modules.entity.damage;
import com.hypixel.hytale.component.Archetype;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.system.RefChangeSystem;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.asset.type.gameplay.respawn.RespawnController;
import com.hypixel.hytale.server.core.entity.InteractionManager;
import com.hypixel.hytale.server.core.entity.effect.EffectControllerComponent;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatValue;
import com.hypixel.hytale.server.core.modules.interaction.InteractionModule;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import javax.annotation.Nonnull;
public class RespawnSystems {
public RespawnSystems() {
}
public abstract static class OnRespawnSystem extends RefChangeSystem<EntityStore, DeathComponent> {
public OnRespawnSystem() {
}
@Nonnull
public ComponentType<EntityStore, DeathComponent> componentType() {
return DeathComponent.getComponentType();
}
public void onComponentAdded(@Nonnull Ref<EntityStore> ref, @Nonnull DeathComponent component, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
}
public void onComponentSet(@Nonnull Ref<EntityStore> ref, DeathComponent oldComponent, @Nonnull DeathComponent newComponent, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
}
}
public static class ResetStatsRespawnSystem extends OnRespawnSystem {
@Nonnull
private static final Query<EntityStore> QUERY = Archetype.of(Player.getComponentType(), EntityStatMap.getComponentType());
public ResetStatsRespawnSystem() {
}
@Nonnull
public Query<EntityStore> getQuery() {
return QUERY;
}
public void onComponentRemoved(@Nonnull Ref<EntityStore> ref, @Nonnull DeathComponent component, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
EntityStatMap entityStatMapComponent = (EntityStatMap)store.getComponent(ref, EntityStatMap.getComponentType());
assert entityStatMapComponent != null;
for(int index = 0; index < entityStatMapComponent.size(); ++index) {
EntityStatValue value = entityStatMapComponent.get(index);
if (value != null) {
entityStatMapComponent.resetStatValue(index);
}
}
}
}
public static class ResetPlayerRespawnSystem extends OnRespawnSystem {
public ResetPlayerRespawnSystem() {
}
@Nonnull
public Query<EntityStore> getQuery() {
return Player.getComponentType();
}
public void onComponentRemoved(@Nonnull Ref<EntityStore> ref, @Nonnull DeathComponent component, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
Player playerComponent = (Player)store.getComponent(ref, Player.getComponentType());
assert playerComponent != null;
playerComponent.setLastSpawnTimeNanos(System.nanoTime());
}
}
public static class ClearEntityEffectsRespawnSystem extends OnRespawnSystem {
public ClearEntityEffectsRespawnSystem() {
}
@Nonnull
public Query<EntityStore> getQuery() {
return EffectControllerComponent.getComponentType();
}
public void onComponentRemoved(@Nonnull Ref<EntityStore> ref, @Nonnull DeathComponent component, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
EffectControllerComponent effectControllerComponent = (EffectControllerComponent)commandBuffer.getComponent(ref, EffectControllerComponent.getComponentType());
assert effectControllerComponent != null;
effectControllerComponent.clearEffects(ref, commandBuffer);
}
}
public static class ClearInteractionsRespawnSystem extends OnRespawnSystem {
public ClearInteractionsRespawnSystem() {
}
public Query<EntityStore> getQuery() {
return InteractionModule.get().getInteractionManagerComponent();
}
public void onComponentRemoved(@Nonnull Ref<EntityStore> ref, @Nonnull DeathComponent component, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
InteractionManager interactionManagerComponent = (InteractionManager)store.getComponent(ref, InteractionModule.get().getInteractionManagerComponent());
interactionManagerComponent.clear();
}
}
public static class RespawnControllerRespawnSystem extends OnRespawnSystem {
public RespawnControllerRespawnSystem() {
}
@Nonnull
public Query<EntityStore> getQuery() {
return Player.getComponentType();
}
public void onComponentRemoved(@Nonnull Ref<EntityStore> ref, @Nonnull DeathComponent component, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
World world = ((EntityStore)store.getExternalData()).getWorld();
RespawnController respawnController = world.getDeathConfig().getRespawnController();
respawnController.respawnPlayer(world, ref, commandBuffer);
}
}
public static class CheckBrokenItemsRespawnSystem extends OnRespawnSystem {
public CheckBrokenItemsRespawnSystem() {
}
@Nonnull
public Query<EntityStore> getQuery() {
return Player.getComponentType();
}
public void onComponentRemoved(@Nonnull Ref<EntityStore> ref, @Nonnull DeathComponent component, @Nonnull Store<EntityStore> store, @Nonnull CommandBuffer<EntityStore> commandBuffer) {
Player playerComponent = (Player)commandBuffer.getComponent(ref, Player.getComponentType());
assert playerComponent != null;
if (playerComponent.getInventory().containsBrokenItem()) {
playerComponent.sendMessage(Message.translation("server.general.repair.itemBrokenOnRespawn").color("#ff5555"));
}
}
}
}