Config class

Пакет: com.hypixel.hytale.builtin.adventure.worldevents.condition

Файл: com/hypixel/hytale/builtin/adventure/worldevents/condition/EventEndCondition.java

implements: EventCondition.Config

Поля (5)

МодификаторыТипИмя
public static final BuilderCodec<EventEndCondition.Config> CODEC
protected static final EventEndCondition.Config EMPTY
protected ContextKey eventKey
protected EventEndCondition.EndMode mode
EventEndCondition var1

Методы (2)

МодификаторыВозвратСигнатура
public EventCondition createpublic EventCondition create()
public void validatepublic void validate(@Nonnull Validator var1)

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.builtin.adventure.worldevents.condition;

class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.component.EventEndEcsEvent;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.ContextKey;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.EventContext;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.event.Validator;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.event.WorldEvent;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.system.ConditionEventHandler;
class="kw">import com.hypixel.hytale.codec.KeyedCodec;
class="kw">import com.hypixel.hytale.codec.builder.BuilderCodec;
class="kw">import com.hypixel.hytale.codec.codecs.EnumCodec;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.server.core.universe.world.World;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public class EventEndCondition class="kw">implements EventCondition.Event<EventEndEcsEvent> {
   class="kw">public class="kw">static class="kw">final String ID = "EventEndCondition";
   class="kw">public class="kw">static class="kw">final EventCondition.Type TYPE = EventCondition.Type.nonTicking("EventEndCondition");
   class="kw">public class="kw">static class="kw">final BuilderCodec<EventEndCondition> CODEC = BuilderCodec.builder(EventEndCondition.class, EventEndCondition::new)
      .append(new KeyedCodec<>("Result", EventEndCondition.State.CODEC), (var0, var1) -> var0.result = var1, var0 -> var0.result)
      .add()
      .build();
   class="kw">private class="kw">static class="kw">final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();
   @Nonnull
   class="kw">protected EventEndCondition.Config config = EventEndCondition.Config.EMPTY;
   @Nullable
   class="kw">protected EventContext context;
   @Nonnull
   class="kw">protected EventEndCondition.State result = EventEndCondition.State.PENDING;

   class="kw">public EventEndCondition() {
   }

   @Override
   class="kw">public boolean test() {
      class="kw">return this.result != EventEndCondition.State.PENDING;
   }

   @Override
   class="kw">public void reset() {
      this.result = EventEndCondition.State.PENDING;
   }

   @Nonnull
   @Override
   class="kw">public Class<EventEndEcsEvent> eventType() {
      class="kw">return EventEndEcsEvent.class;
   }

   class="kw">public void handle(@Nonnull World var1, @Nonnull EventEndEcsEvent var2) {
      if (this.context != null) {
         if (var2.getWorldEvent().uuid.equals(this.context.id())) {
            if (var2.getReason() == EventEndEcsEvent.Reason.FAIL) {
               assert this.config.eventKey != null;
               ((HytaleLogger.Api)LOGGER.atWarning()).log("Condition failed: Event %s ended with an error", this.config.eventKey.getName());
               this.result = EventEndCondition.State.FAILED;
               class="kw">return;
            }
            this.result = class="kw">switch (this.config.mode) {
               case ITERATION -> EventEndCondition.State.COMPLETE;
               case TERMINATION -> getTerminationState(var2.getWorldEvent());
            };
         }
      }
   }

   @Override
   class="kw">public boolean onStart(@Nonnull WorldEvent var1) {
      if (this.config.eventKey == null) {
         ((HytaleLogger.Api)LOGGER.atWarning()).log("Condition failed: eventKey not set");
         class="kw">return false;
      } else {
         this.context = var1.context.get(this.config.eventKey, EventContext.class);
         if (this.context == null) {
            ((HytaleLogger.Api)LOGGER.atWarning()).log("Condition failed: EventContext not found for key: %s", this.config.eventKey.getName());
            class="kw">return false;
         } else {
            class="kw">return true;
         }
      }
   }

   @Override
   class="kw">public boolean onComplete(@Nonnull WorldEvent var1) {
      if (this.result == EventEndCondition.State.FAILED) {
         assert this.config.eventKey != null;
         ((HytaleLogger.Api)LOGGER.atWarning()).log("Condition failed: Event %s did not end successfully", this.config.eventKey.getName());
         class="kw">return false;
      } else {
         class="kw">return true;
      }
   }

   @Nonnull
   class="kw">protected class="kw">static EventEndCondition.State getTerminationState(@Nonnull WorldEvent var0) {
      if (var0.repeatCount == 0) {
         class="kw">return EventEndCondition.State.COMPLETE;
      } else if (var0.repeatCount == -1) {
         ((HytaleLogger.Api)LOGGER.atWarning()).log("Event %s cannot terminate, it is configured to repeat indefinitely", var0.uuid);
         class="kw">return EventEndCondition.State.FAILED;
      } else {
         class="kw">return EventEndCondition.State.PENDING;
      }
   }

   class="kw">public class="kw">static class Config class="kw">implements EventCondition.Config {
      class="kw">public class="kw">static class="kw">final BuilderCodec<EventEndCondition.Config> CODEC = BuilderCodec.builder(EventEndCondition.Config.class, EventEndCondition.Config::new)
         .append(new KeyedCodec<>("EventKey", ContextKey.CODEC), (var0, var1) -> var0.eventKey = var1, var0 -> var0.eventKey)
         .documentation("The variable name of the event that this condition should wait for to end.")
         .add()
         .<EventEndCondition.EndMode>append(new KeyedCodec<>("EndMode", EventEndCondition.EndMode.CODEC), (var0, var1) -> var0.mode = var1, var0 -> var0.mode)
         .documentation("Controls what should be considered the end of the event.")
         .add()
         .build();
      class="kw">protected class="kw">static class="kw">final EventEndCondition.Config EMPTY = new EventEndCondition.Config();
      @Nullable
      class="kw">protected ContextKey eventKey;
      @Nonnull
      class="kw">protected EventEndCondition.EndMode mode = EventEndCondition.EndMode.TERMINATION;

      class="kw">public Config() {
      }

      @Nonnull
      @Override
      class="kw">public EventCondition create() {
         EventEndCondition var1 = new EventEndCondition();
         var1.config = this;
         class="kw">return var1;
      }

      @Override
      class="kw">public void validate(@Nonnull Validator var1) {
         var1.addReferencedKey("EventEndCondition", "EventKey", this.eventKey);
      }
   }

   class="kw">public enum EndMode {
      ITERATION,
      TERMINATION;

      class="kw">public class="kw">static class="kw">final EnumCodec<EventEndCondition.EndMode> CODEC = new EnumCodec<>(EventEndCondition.EndMode.class);

      EndMode() {
      }

      class="kw">static {
         CODEC.documentKey(ITERATION, "Condition completes whenever the referenced event reaches the end of an iteration.");
         CODEC.documentKey(TERMINATION, "Condition completes when the referenced event completes its class="kw">final iteration and terminates.");
      }
   }

   class="kw">public class="kw">static class Handler class="kw">extends ConditionEventHandler.EntityHandler<EventEndEcsEvent> {
      class="kw">public Handler() {
         super(EventEndEcsEvent.class);
      }
   }

   class="kw">public enum State {
      PENDING,
      COMPLETE,
      FAILED;

      class="kw">public class="kw">static class="kw">final EnumCodec<EventEndCondition.State> CODEC = new EnumCodec<>(EventEndCondition.State.class);

      State() {
      }
   }
}