PageAction enum

Пакет: com.hypixel.hytale.builtin.portals.ui

Файл: com/hypixel/hytale/builtin/portals/ui/PortalDeviceActivePage.java

Поля (2)

МодификаторыТипИмя
public static final Codec<PortalDeviceActivePage.PageAction> CODEC
ClosePortal

Исходный код

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

class="kw">import com.hypixel.hytale.builtin.instances.removal.InstanceDataResource;
class="kw">import com.hypixel.hytale.builtin.portals.components.PortalDevice;
class="kw">import com.hypixel.hytale.builtin.portals.components.PortalDeviceConfig;
class="kw">import com.hypixel.hytale.builtin.portals.resources.PortalWorld;
class="kw">import com.hypixel.hytale.codec.Codec;
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.common.util.CompletableFutureUtil;
class="kw">import com.hypixel.hytale.component.ComponentAccessor;
class="kw">import com.hypixel.hytale.component.Ref;
class="kw">import com.hypixel.hytale.component.Store;
class="kw">import com.hypixel.hytale.protocol.packets.interface_.CustomPageLifetime;
class="kw">import com.hypixel.hytale.protocol.packets.interface_.CustomUIEventBindingType;
class="kw">import com.hypixel.hytale.server.core.Message;
class="kw">import com.hypixel.hytale.server.core.asset.type.portalworld.PortalType;
class="kw">import com.hypixel.hytale.server.core.entity.UUIDComponent;
class="kw">import com.hypixel.hytale.server.core.entity.entities.player.pages.InteractiveCustomUIPage;
class="kw">import com.hypixel.hytale.server.core.ui.builder.EventData;
class="kw">import com.hypixel.hytale.server.core.ui.builder.UICommandBuilder;
class="kw">import com.hypixel.hytale.server.core.ui.builder.UIEventBuilder;
class="kw">import com.hypixel.hytale.server.core.universe.PlayerRef;
class="kw">import com.hypixel.hytale.server.core.universe.Universe;
class="kw">import com.hypixel.hytale.server.core.universe.world.World;
class="kw">import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
class="kw">import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
class="kw">import java.util.UUID;
class="kw">import java.util.concurrent.CompletableFuture;
class="kw">import javax.annotation.Nonnull;

class="kw">public class PortalDeviceActivePage class="kw">extends InteractiveCustomUIPage<PortalDeviceActivePage.PageData> {
   @Nonnull
   class="kw">private class="kw">final PortalDeviceConfig config;
   @Nonnull
   class="kw">private class="kw">final Ref<ChunkStore> blockRef;

   class="kw">public PortalDeviceActivePage(@Nonnull PlayerRef var1, @Nonnull PortalDeviceConfig var2, @Nonnull Ref<ChunkStore> var3) {
      super(var1, CustomPageLifetime.CanDismissOrCloseThroughInteraction, PortalDeviceActivePage.PageData.CODEC);
      this.config = var2;
      this.blockRef = var3;
   }

   @Override
   class="kw">public void build(@Nonnull Ref<EntityStore> var1, @Nonnull UICommandBuilder var2, @Nonnull UIEventBuilder var3, @Nonnull Store<EntityStore> var4) {
      PortalDeviceActivePage.State var5 = this.computeState(var1, var4);
      if (var5 != PortalDeviceActivePage.Error.INVALID_BLOCK) {
         var2.append("Pages/PortalDeviceActive.ui");
         if (var5 == PortalDeviceActivePage.Error.PORTAL_LOADING) {
            var2.set("#Error.Visible", true);
            var2.set("#ErrorTitle.Visible", false);
            var2.set("#ErrorLabel.Text", Message.translation("server.customUI.portalDevice.loading"));
         } else if (var5 class="kw">instanceof PortalDeviceActivePage.PortalIsOpen(World var6, PortalWorld var7, boolean var8)) {
            PortalType var18 = var7.getPortalType();
            var2.set("#PortalPanel.Visible", true);
            if (var8) {
               var2.set("#Died.Visible", true);
            }

            var2.set("#PortalTitle.TextSpans", Message.translation("server.customUI.portalDevice.portalTitle").param("name", var18.getDisplayName()));
            var2.set("#PortalDescription.TextSpans", PortalDeviceSummonPage.createDescription(var18, var7.getTimeLimitSeconds()));
            Message var21 = createPlayerCountMsg(var6);
            var2.set("#PlayersInside.TextSpans", var21);
            double var11 = var7.getRemainingSeconds(var6);
            if (var11 < var7.getTimeLimitSeconds()) {
               int var13 = (int)Math.round(var11 / 60.0);
               Message var14 = var13 <= 1
                  ? Message.translation("server.customUI.portalDevice.lessThanAMinute")
                  : Message.translation("server.customUI.portalDevice.remainingMinutes").param("time", var13);
               var2.set(
                  "#RemainingDuration.TextSpans",
                  Message.translation("server.customUI.portalDevice.remainingDuration").param("remaining", var14.color("#ea4fa46b"))
               );
            } else {
               var2.set("#PortalIsOpen.Visible", true);
               var2.set("#RemainingDuration.TextSpans", Message.translation("server.customUI.portalDevice.beTheFirst").color("#ea4fa46b"));
            }

            var3.addEventBinding(
               CustomUIEventBindingType.Activating,
               "#ClosePortalButton",
               new EventData().append("Action", PortalDeviceActivePage.PageAction.ClosePortal),
               false
            );
         } else {
            var2.set("#Error.Visible", true);
            var2.set("#ErrorLabel.Text", Message.translation("server.customUI.portalDevice.unknownError").param("state", var5.toString()));
         }
      }
   }

   class="kw">public void handleDataEvent(@Nonnull Ref<EntityStore> var1, @Nonnull Store<EntityStore> var2, @Nonnull PortalDeviceActivePage.PageData var3) {
      if (var3.action == PortalDeviceActivePage.PageAction.ClosePortal) {
         if (!(this.computeState(var1, var2) class="kw">instanceof PortalDeviceActivePage.PortalIsOpen var4)) {
            class="kw">return;
         }

         World var8 = var4.world();
         if (var8.getPlayerCount() > 0) {
            PlayerRef var6 = var2.getComponent(var1, PlayerRef.getComponentType());
            if (var6 != null) {
               var6.sendMessage(Message.translation("server.customUI.portalDevice.cannotCloseOccupied"));
            }

            UICommandBuilder var7 = new UICommandBuilder();
            var7.set("#CannotCloseLabel.Visible", true);
            this.sendUpdate(var7);
            class="kw">return;
         }

         this.close();
         closePortalWorld(var8);
      }
   }

   class="kw">private class="kw">static void closePortalWorld(@Nonnull World var0) {
      String var1 = var0.getName();
      var0.execute(() -> {
         if (var0.getPlayerCount() <= 0) {
            InstanceDataResource var2 = var0.getChunkStore().getStore().getResource(InstanceDataResource.getResourceType());
            if (!var2.isRemoving()) {
               var2.setRemoving(true);
               CompletableFutureUtil._catch(CompletableFuture.runAsync(() -> Universe.get().removeWorld(var1)));
            }
         }
      });
   }

   @Nonnull
   class="kw">private class="kw">static Message createPlayerCountMsg(@Nonnull World var0) {
      int var1 = var0.getPlayerCount();
      String var2 = "#ea4fa46b";
      if (var1 == 0) {
         class="kw">return Message.translation("server.customUI.portalDevice.playersInside")
            .param("count", Message.translation("server.customUI.portalDevice.playersInsideNone").color(var2));
      }

      if (var1 > 4) {
         class="kw">return Message.translation("server.customUI.portalDevice.playersInside").param("count", Message.raw(var1 + "!").color(var2));
      }

      Message var3 = Message.translation("server.customUI.portalDevice.playersInside").param("count", Message.raw(var1 + "!").color(var2));

      for (PlayerRef var6 : var0.getPlayerRefs()) {
         var3.insert(Message.raw("- ").color("#6b6b6b6b")).insert(var6.getUsername());
      }

      class="kw">return var3;
   }

   @Nonnull
   class="kw">private PortalDeviceActivePage.State computeState(@Nonnull Ref<EntityStore> var1, @Nonnull ComponentAccessor<EntityStore> var2) {
      if (!this.blockRef.isValid()) {
         class="kw">return PortalDeviceActivePage.Error.INVALID_BLOCK;
      }

      Store var3 = this.blockRef.getStore();
      PortalDevice var4 = var3.getComponent(this.blockRef, PortalDevice.getComponentType());
      if (var4 == null) {
         class="kw">return PortalDeviceActivePage.Error.INVALID_BLOCK;
      }

      if (var4.isLoadingWorld()) {
         class="kw">return PortalDeviceActivePage.Error.PORTAL_LOADING;
      }

      World var5 = var4.getDestinationWorld();
      if (var5 == null) {
         class="kw">return PortalDeviceActivePage.Error.INVALID_WORLD;
      }

      Store var6 = var5.getEntityStore().getStore();
      PortalWorld var7 = var6.getResource(PortalWorld.getResourceType());
      if (!var7.exists()) {
         class="kw">return PortalDeviceActivePage.Error.DESTINATION_NOT_FRAGMENT;
      }

      UUIDComponent var8 = var2.getComponent(var1, UUIDComponent.getComponentType());
      if (var8 == null) {
         class="kw">return PortalDeviceActivePage.Error.INACTIVE_PORTAL;
      }

      UUID var9 = var8.getUuid();
      boolean var10 = var7.getDiedInWorld().contains(var9);
      class="kw">return new PortalDeviceActivePage.PortalIsOpen(var5, var7, var10);
   }

   class="kw">private enum Error class="kw">implements PortalDeviceActivePage.State {
      INVALID_BLOCK,
      INVALID_WORLD,
      DESTINATION_NOT_FRAGMENT,
      INACTIVE_PORTAL,
      PORTAL_LOADING;

      Error() {
      }
   }

   class="kw">public enum PageAction {
      ClosePortal;

      @Nonnull
      class="kw">public class="kw">static class="kw">final Codec<PortalDeviceActivePage.PageAction> CODEC = new EnumCodec<>(PortalDeviceActivePage.PageAction.class);

      PageAction() {
      }
   }

   class="kw">protected class="kw">static class PageData {
      @Nonnull
      class="kw">private class="kw">static class="kw">final String KEY_ACTION = "Action";
      @Nonnull
      class="kw">public class="kw">static class="kw">final BuilderCodec<PortalDeviceActivePage.PageData> CODEC = BuilderCodec.builder(
            PortalDeviceActivePage.PageData.class, PortalDeviceActivePage.PageData::new
         )
         .append(new KeyedCodec<>("Action", PortalDeviceActivePage.PageAction.CODEC), (var0, var1) -> var0.action = var1, var0 -> var0.action)
         .add()
         .build();
      class="kw">public PortalDeviceActivePage.PageAction action;

      class="kw">protected PageData() {
      }
   }

   class="kw">private record PortalIsOpen(World world, PortalWorld portalWorld, boolean diedInside) class="kw">implements PortalDeviceActivePage.State {
      class="kw">private PortalIsOpen {
      }
   }

   class="kw">private sealed class="kw">interface State permits PortalDeviceActivePage.PortalIsOpen, PortalDeviceActivePage.Error {
   }
}