EntityCollector class

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

Файл: com/hypixel/hytale/builtin/adventure/worldevents/action/PrefabPasteAction.java

Поля (5)

МодификаторыТипИмя
protected static final ThreadLocal<PrefabPasteAction.EntityCollector> LOCAL
protected final Consumer<Holder<ChunkStore>> blocks
protected ContextKey contextKey
protected final Consumer<Holder<EntityStore>> entities
protected UUID uuid

Методы (2)

МодификаторыВозвратСигнатура
protected void trackBlockprotected void trackBlock(@Nonnull Holder<ChunkStore> var1)
protected void trackEntityprotected void trackEntity(@Nonnull Holder<EntityStore> var1)

Исходный код

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

class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.component.TrackedBlockComponent;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.component.TrackedEntityComponent;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.ContextKey;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.PrefabPasteContext;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.PrefabRemoveContext;
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.util.PrefabSnapshotUtil;
class="kw">import com.hypixel.hytale.codec.Codec;
class="kw">import com.hypixel.hytale.codec.KeyedCodec;
class="kw">import com.hypixel.hytale.codec.record.RecordCodec;
class="kw">import com.hypixel.hytale.codec.validation.Validators;
class="kw">import com.hypixel.hytale.component.CommandBuffer;
class="kw">import com.hypixel.hytale.component.Holder;
class="kw">import com.hypixel.hytale.component.Ref;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.server.core.prefab.selection.buffer.impl.IPrefabBuffer;
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 com.hypixel.hytale.server.core.util.PrefabUtil;
class="kw">import com.hypixel.hytale.server.core.util.UUIDUtil;
class="kw">import java.util.UUID;
class="kw">import java.util.concurrent.ThreadLocalRandom;
class="kw">import java.util.function.Consumer;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public record PrefabPasteAction(@Nonnull ContextKey prefabKey, boolean permanent) class="kw">implements EventAction {
   class="kw">public class="kw">static class="kw">final String ID = "PrefabPasteAction";
   class="kw">public class="kw">static class="kw">final RecordCodec<PrefabPasteAction> CODEC = RecordCodec.builder(PrefabPasteAction.class)
      .documentation("An action that pastes a prefab into the world.")
      .append(
         new KeyedCodec<>("PrefabKey", ContextKey.CODEC, true),
         PrefabPasteAction::prefabKey,
         var0 -> var0.documentation("The variable name of the prefab that this action should paste.").addValidator(Validators.nonNull())
      )
      .append(
         new KeyedCodec<>("Permanent", Codec.BOOLEAN),
         PrefabPasteAction::permanent,
         var0 -> var0.documentation(
            "Whether the prefab should be pasted permanently in the world. If false, the prefab and its entities will be removed the world rolled back to its previous state when the event ends."
         )
      )
      .build(PrefabPasteAction::new);
   class="kw">private class="kw">static class="kw">final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();

   class="kw">public PrefabPasteAction {
   }

   @Override
   class="kw">public boolean apply(@Nonnull Ref<EntityStore> var1, @Nonnull CommandBuffer<EntityStore> var2, @Nonnull WorldEvent var3) {
      World var4 = var2.getExternalData().getWorld();
      PrefabPasteContext var5 = var3.context.consume(this.prefabKey, PrefabPasteContext.class);
      if (var5 == null) {
         ((HytaleLogger.Api)LOGGER.atWarning()).log("Action failed: PrefabPasteContext missing for key: %s", this.prefabKey.getName());
         class="kw">return false;
      }

      IPrefabBuffer var6 = PrefabSnapshotUtil.getAssetPrefabOrNull(var5.path());
      if (var6 == null) {
         ((HytaleLogger.Api)LOGGER.atWarning()).log("Action failed: Prefab not found: %s", var5.path());
         class="kw">return false;
      }

      PrefabUtil.PasteRegion var7 = PrefabUtil.loadPasteRegionIfInMemory(var6, var4, var5.origin(), var5.rotation());
      if (!var7.isFullyLoaded()) {
         ((HytaleLogger.Api)LOGGER.atWarning()).log("Action failed: Paste region for prefab: %s is not fully loaded", var5.path());
         class="kw">return false;
      }

      if (!this.permanent) {
         UUID var8 = PrefabSnapshotUtil.createSnapshot(var4, var7, var6, var5.origin(), var5.rotation());
         var3.context.put(this.prefabKey, new PrefabRemoveContext(var8, this.prefabKey, var5.path(), var5.origin(), var5.rotation()));
      }

      PrefabPasteAction.EntityCollector var9 = PrefabPasteAction.EntityCollector.LOCAL.get();
      var9.uuid = var3.uuid;
      var9.contextKey = this.prefabKey;
      PrefabUtil.paste(var6, var4, var5.origin(), var5.rotation().getRotation(), ThreadLocalRandom.current(), 1, 0, var7, var9.blocks, var9.entities, var2);
      class="kw">return true;
   }

   @Override
   class="kw">public void validate(@Nonnull Validator var1) {
      var1.addReferencedKey("PrefabPasteAction", "PrefabKey", this.prefabKey);
   }

   class="kw">protected class="kw">static class EntityCollector {
      class="kw">protected class="kw">static class="kw">final ThreadLocal<PrefabPasteAction.EntityCollector> LOCAL = ThreadLocal.withInitial(PrefabPasteAction.EntityCollector::new);
      class="kw">protected class="kw">final Consumer<Holder<ChunkStore>> blocks = this::trackBlock;
      class="kw">protected class="kw">final Consumer<Holder<EntityStore>> entities = this::trackEntity;
      @Nonnull
      class="kw">protected UUID uuid = UUIDUtil.EMPTY_UUID;
      @Nullable
      class="kw">protected ContextKey contextKey = null;

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

      class="kw">protected void trackBlock(@Nonnull Holder<ChunkStore> var1) {
         var1.putComponent(TrackedBlockComponent.getComponentType(), new TrackedBlockComponent(this.uuid, this.contextKey));
      }

      class="kw">protected void trackEntity(@Nonnull Holder<EntityStore> var1) {
         var1.putComponent(TrackedEntityComponent.getComponentType(), new TrackedEntityComponent(this.uuid, this.contextKey));
      }
   }
}