RemoveOp class

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

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

implements: Consumer<PrefabUtil.PasteRegion>, BiConsumer<Void, Throwable>

Поля (14)

МодификаторыТипИмя
protected static final ArrayBlockingQueue<PrefabRemoveAction.RemoveOp> POOL
protected PrefabRemoveContext context
protected WorldEvent event
protected final FastRandom random
protected final ObjectList<Ref<EntityStore>> refs
return
protected IPrefabBuffer snapshot
protected final ComponentType<EntityStore, TrackedEntityComponent> trackerComponentType
PrefabRemoveAction.RemoveOp var0
Ref var10
Store var2
IPrefabBuffer var3
EntitySection var8
protected World world

Методы (11)

МодификаторыВозвратСигнатура
public void acceptpublic void accept(@Nullable PrefabUtil.PasteRegion var1)
public void acceptpublic void accept(@Nullable Void var1, @Nullable Throwable var2)
protected static PrefabRemoveAction.RemoveOp acquireprotected static PrefabRemoveAction.RemoveOp acquire()
protected void clearprotected void clear()
for for(PrefabUtil.PasteRegion.Section var7 : var1.sections)
abstract for for(int var9 = 0; var9 < this.refs.size()
if if(var3 != null)
if if(var8 != null)
protected boolean matchTrackerprotected boolean matchTracker(@Nullable TrackedEntityComponent var1)
protected static void releaseprotected static void release(@Nonnull PrefabRemoveAction.RemoveOp var0)
abstract release release(this)

Исходный код

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

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.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.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.ComponentType;
class="kw">import com.hypixel.hytale.component.Ref;
class="kw">import com.hypixel.hytale.component.RemoveReason;
class="kw">import com.hypixel.hytale.component.Store;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.math.util.FastRandom;
class="kw">import com.hypixel.hytale.server.core.asset.type.blocktype.config.Rotation;
class="kw">import com.hypixel.hytale.server.core.prefab.PrefabRotation;
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.chunk.section.EntitySection;
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 it.unimi.dsi.fastutil.objects.ObjectArrayList;
class="kw">import it.unimi.dsi.fastutil.objects.ObjectList;
class="kw">import java.util.concurrent.ArrayBlockingQueue;
class="kw">import java.util.concurrent.ThreadLocalRandom;
class="kw">import java.util.concurrent.TimeUnit;
class="kw">import java.util.function.BiConsumer;
class="kw">import java.util.function.Consumer;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public record PrefabRemoveAction(@Nonnull ContextKey prefabKey) class="kw">implements EventAction {
   class="kw">public class="kw">static class="kw">final String ID = "PrefabRemoveAction";
   class="kw">public class="kw">static class="kw">final RecordCodec<PrefabRemoveAction> CODEC = RecordCodec.builder(PrefabRemoveAction.class)
      .documentation("An action that rolls back a previously pasted prefab in the world and any entities spawned by it.")
      .append(
         new KeyedCodec<>("PrefabKey", ContextKey.CODEC, true),
         PrefabRemoveAction::prefabKey,
         var0 -> var0.documentation("The variable name of the prefab that this action should remove.").addValidator(Validators.nonNull())
      )
      .build(PrefabRemoveAction::new);
   class="kw">private class="kw">static class="kw">final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();
   class="kw">private class="kw">static class="kw">final long TIMEOUT_SECONDS = 30L;

   class="kw">public PrefabRemoveAction {
   }

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

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

   class="kw">public class="kw">static boolean apply(@Nonnull World var0, @Nonnull WorldEvent var1, @Nonnull PrefabRemoveContext var2) {
      IPrefabBuffer var3 = PrefabSnapshotUtil.getSnapshotPrefabOrNull(var0, var2.snapshot());
      if (var3 == null) {
         ((HytaleLogger.Api)LOGGER.atWarning()).log("Action failed: Snapshot prefab not found: %s", var2.snapshot());
         class="kw">return false;
      } else {
         PrefabRemoveAction.RemoveOp var4 = PrefabRemoveAction.RemoveOp.acquire();
         var4.world = var0;
         var4.event = var1;
         var4.context = var2;
         var4.snapshot = var3;
         int var5 = 4;
         PrefabUtil.loadPasteRegionAsync(var3, var0, var2.origin(), PrefabRotation.ROTATION_0, 4)
            .handle((var0x, var1x) -> (PrefabUtil.PasteRegion)var0x)
            .thenAcceptAsync(var4, var0)
            .whenComplete(var4)
            .orTimeout(30L, TimeUnit.SECONDS);
         class="kw">return true;
      }
   }

   class="kw">protected class="kw">static class RemoveOp class="kw">implements Consumer<PrefabUtil.PasteRegion>, BiConsumer<Void, Throwable> {
      class="kw">protected class="kw">static class="kw">final ArrayBlockingQueue<PrefabRemoveAction.RemoveOp> POOL = new ArrayBlockingQueue<>(16);
      @Nullable
      class="kw">protected World world;
      @Nullable
      class="kw">protected WorldEvent event;
      @Nullable
      class="kw">protected IPrefabBuffer snapshot;
      @Nullable
      class="kw">protected PrefabRemoveContext context;
      class="kw">protected class="kw">final FastRandom random = new FastRandom();
      class="kw">protected class="kw">final ObjectList<Ref<EntityStore>> refs = new ObjectArrayList();
      class="kw">protected class="kw">final ComponentType<EntityStore, TrackedEntityComponent> trackerComponentType = TrackedEntityComponent.getComponentType();

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

      class="kw">protected void clear() {
         this.world = null;
         this.event = null;
         this.snapshot = null;
         this.context = null;
         this.refs.clear();
      }

      class="kw">public void accept(@Nullable PrefabUtil.PasteRegion var1) {
         assert this.world != null : "RemoveOP not configured correctly";
         assert this.event != null : "RemoveOP not configured correctly";
         assert this.snapshot != null : "RemoveOP not configured correctly";
         assert this.context != null : "RemoveOP not configured correctly";

         try {
            if (var1 == null || !var1.isFullyLoaded()) {
               ((HytaleLogger.Api)PrefabRemoveAction.LOGGER.atWarning()).log("Failed to load paste region to rollback snapshot: %s", this.context.snapshot());
               class="kw">return;
            }

            Store var2 = this.world.getEntityStore().getStore();
            this.random.setSeed(ThreadLocalRandom.current().nextLong());
            IPrefabBuffer var3 = PrefabSnapshotUtil.getAssetPrefabOrNull(this.context.prefab());
            if (var3 != null) {
               PrefabUtil.remove(var3, this.world, this.context.origin(), this.context.rotation().getRotation(), this.random, 0, 0, 0.05, var1);
            }

            PrefabUtil.paste(
               this.snapshot,
               this.world,
               this.context.origin(),
               Rotation.None,
               this.random,
               1,
               0,
               var1,
               PrefabUtil.NOOP_BLOCK_ENTITY_CONSUMER,
               PrefabUtil.NOOP_ENTITY_CONSUMER,
               var2
            );

            for (PrefabUtil.PasteRegion.Section var7 : var1.sections) {
               EntitySection var8 = var7.entities();
               if (var8 != null) {
                  try {
                     this.refs.addAll(var8.getEntityReferences());

                     for (int var9 = 0; var9 < this.refs.size(); var9++) {
                        Ref var10 = (Ref<EntityStore>)this.refs.get(var9);
                        if (var10.isValid() && this.matchTracker(var2.getComponent(var10, this.trackerComponentType))) {
                           var2.removeEntity(var10, RemoveReason.REMOVE);
                        }
                     }
                  } class="kw">finally {
                     this.refs.clear();
                  }
               }
            }

            PrefabSnapshotUtil.deleteSnapshot(this.world, this.context.snapshot());
         } class="kw">finally {
            this.clear();
         }
      }

      class="kw">public void accept(@Nullable Void var1, @Nullable Throwable var2) {
         release(this);
      }

      class="kw">protected boolean matchTracker(@Nullable TrackedEntityComponent var1) {
         assert this.event != null;
         assert this.context != null;
         class="kw">return var1 != null
            && var1.getUUID().equals(this.event.uuid)
            && (var1.getContextKey() == null || var1.getContextKey().equals(this.context.contextKey()));
      }

      @Nonnull
      class="kw">protected class="kw">static PrefabRemoveAction.RemoveOp acquire() {
         PrefabRemoveAction.RemoveOp var0 = POOL.poll();
         class="kw">return var0 == null ? new PrefabRemoveAction.RemoveOp() : var0;
      }

      class="kw">protected class="kw">static void release(@Nonnull PrefabRemoveAction.RemoveOp var0) {
         try {
            var0.clear();
         } class="kw">finally {
            POOL.offer(var0);
         }
      }
   }
}