Result class

Пакет: com.hypixel.hytale.server.core.modules.interaction.interaction.config.selector

Файл: com/hypixel/hytale/server/core/modules/interaction/interaction/config/selector/RaycastSelector.java

Поля (3)

МодификаторыТипИмя
public double distance
public Vector4d hitPosition
public Ref<EntityStore> match

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.server.core.modules.interaction.interaction.config.selector;

class="kw">import com.hypixel.hytale.assetstore.AssetRegistry;
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.component.CommandBuffer;
class="kw">import com.hypixel.hytale.component.Ref;
class="kw">import com.hypixel.hytale.math.util.HashUtil;
class="kw">import com.hypixel.hytale.math.vector.Vector3dUtil;
class="kw">import com.hypixel.hytale.protocol.BlockMaterial;
class="kw">import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
class="kw">import com.hypixel.hytale.server.core.modules.collision.CollisionMath;
class="kw">import com.hypixel.hytale.server.core.modules.debug.DebugUtils;
class="kw">import com.hypixel.hytale.server.core.modules.entity.component.BoundingBox;
class="kw">import com.hypixel.hytale.server.core.modules.entity.component.HeadRotation;
class="kw">import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
class="kw">import com.hypixel.hytale.server.core.modules.interaction.interaction.config.none.SelectInteraction;
class="kw">import com.hypixel.hytale.server.core.universe.world.chunk.CachedAccessor;
class="kw">import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
class="kw">import com.hypixel.hytale.server.core.util.TargetUtil;
class="kw">import it.unimi.dsi.fastutil.ints.IntSet;
class="kw">import java.util.function.BiConsumer;
class="kw">import java.util.function.Predicate;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;
class="kw">import org.joml.Vector2d;
class="kw">import org.joml.Vector3d;
class="kw">import org.joml.Vector3f;
class="kw">import org.joml.Vector3i;
class="kw">import org.joml.Vector4d;

class="kw">public class RaycastSelector class="kw">extends SelectorType {
   class="kw">public class="kw">static class="kw">final BuilderCodec<RaycastSelector> CODEC = BuilderCodec.builder(RaycastSelector.class, RaycastSelector::new, BASE_CODEC)
      .appendInherited(
         new KeyedCodec<>("Offset", Vector3dUtil.CODEC),
         (var0, var1) -> var0.offset.set(var1),
         var0 -> var0.offset,
         (var0, var1) -> var0.offset.set(var1.offset)
      )
      .documentation("The offset of the area to search for targets in.")
      .add()
      .<Integer>appendInherited(
         new KeyedCodec<>("Distance", Codec.INTEGER),
         (var0, var1) -> var0.distance = var1,
         var0 -> var0.distance,
         (var0, var1) -> var0.distance = var1.distance
      )
      .documentation("The max search distance for the raycast")
      .add()
      .appendInherited(
         new KeyedCodec<>("IgnoreFluids", Codec.BOOLEAN),
         (var0, var1) -> var0.ignoreFluids = var1,
         var0 -> var0.ignoreFluids,
         (var0, var1) -> var0.ignoreFluids = var1.ignoreFluids
      )
      .add()
      .appendInherited(
         new KeyedCodec<>("IgnoreEmptyCollisionMaterial", Codec.BOOLEAN),
         (var0, var1) -> var0.ignoreEmptyCollisionMaterial = var1,
         var0 -> var0.ignoreEmptyCollisionMaterial,
         (var0, var1) -> var0.ignoreEmptyCollisionMaterial = var1.ignoreEmptyCollisionMaterial
      )
      .add()
      .<String>appendInherited(
         new KeyedCodec<>("BlockTag", Codec.STRING), (var0, var1) -> var0.blockTag = var1, var0 -> var0.blockTag, (var0, var1) -> var0.blockTag = var1.blockTag
      )
      .documentation("The required tag for the block to have to match for the raycast to hit them")
      .add()
      .afterDecode(var0 -> {
         if (var0.blockTag != null) {
            var0.blockTagIndex = AssetRegistry.getOrCreateTagIndex(var0.blockTag);
         }
      })
      .build();
   class="kw">protected class="kw">final Vector3d offset = new Vector3d();
   class="kw">protected int distance = 30;
   class="kw">protected boolean ignoreFluids = false;
   class="kw">protected boolean ignoreEmptyCollisionMaterial = false;
   @Nullable
   class="kw">protected String blockTag;
   class="kw">protected int blockTagIndex = Integer.MIN_VALUE;

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

   @Nonnull
   @Override
   class="kw">public Selector newSelector() {
      class="kw">return new RaycastSelector.RuntimeSelector();
   }

   @Nonnull
   class="kw">public Vector3f getOffset() {
      class="kw">return new Vector3f((float)this.offset.x, (float)this.offset.y, (float)this.offset.z);
   }

   @Nonnull
   class="kw">public Vector3d selectTargetPosition(@Nonnull CommandBuffer<EntityStore> var1, @Nonnull Ref<EntityStore> var2) {
      TransformComponent var3 = var1.getComponent(var2, TransformComponent.getComponentType());
      Vector3d var4 = var3.getPosition();
      if (this.offset.x != 0.0 || this.offset.y != 0.0 || this.offset.z != 0.0) {
         var4 = new Vector3d(this.offset);
         HeadRotation var5 = var1.getComponent(var2, HeadRotation.getComponentType());
         var4.rotateY(var5.getRotation().yaw());
         var4.add(var3.getPosition());
      }

      class="kw">return var4;
   }

   class="kw">public com.hypixel.hytale.protocol.Selector toPacket() {
      class="kw">return new com.hypixel.hytale.protocol.RaycastSelector(
         this.getOffset(), this.distance, this.blockTagIndex, this.ignoreFluids, this.ignoreEmptyCollisionMaterial
      );
   }

   class="kw">private class="kw">static class Result {
      class="kw">public Ref<EntityStore> match;
      class="kw">public double distance = Double.MAX_VALUE;
      @Nonnull
      class="kw">public Vector4d hitPosition = new Vector4d().zero();

      class="kw">private Result() {
      }
   }

   class="kw">private class RuntimeSelector class="kw">implements Selector {
      class="kw">private class="kw">final RaycastSelector.Result bestMatch = new RaycastSelector.Result();
      class="kw">private class="kw">final Vector2d minMax = new Vector2d();
      @Nullable
      class="kw">private Vector3i blockPosition;

      class="kw">private RuntimeSelector() {
      }

      @Override
      class="kw">public void tick(@Nonnull CommandBuffer<EntityStore> var1, @Nonnull Ref<EntityStore> var2, float var3, float var4) {
         Vector3d var5 = RaycastSelector.this.selectTargetPosition(var1, var2);
         HeadRotation var6 = var1.getComponent(var2, HeadRotation.getComponentType());
         Vector3d var7 = Vector3dUtil.setYawPitch(var6.getRotation().yaw(), var6.getRotation().pitch(), new Vector3d());
         IntSet var8 = RaycastSelector.this.blockTag == null ? null : BlockType.getAssetMap().getIndexesForTag(RaycastSelector.this.blockTagIndex);
         if (SelectInteraction.SHOW_VISUAL_DEBUG) {
            Vector3d var9 = new Vector3d(var7).mul(RaycastSelector.this.distance);
            Vector3f var10 = new Vector3f(
               (float)HashUtil.random(var2.getIndex(), this.hashCode(), 10L),
               (float)HashUtil.random(var2.getIndex(), this.hashCode(), 11L),
               (float)HashUtil.random(var2.getIndex(), this.hashCode(), 12L)
            );
            DebugUtils.addArrow(var1.getExternalData().getWorld(), var5, var9, var10, 5.0F, 1);
         }

         this.blockPosition = TargetUtil.getTargetBlock(var1.getExternalData().getWorld(), (var2x, var3x) -> {
            if (var2x == 0) {
               class="kw">return false;
            }

            if (RaycastSelector.this.ignoreFluids || RaycastSelector.this.ignoreEmptyCollisionMaterial) {
               BlockType var4 = BlockType.getAssetMap().getAsset(var2x);
               if (RaycastSelector.this.ignoreFluids && var4.getMaterial() == BlockMaterial.Empty && var3x != 0) {
                  class="kw">return false;
               }

               if (RaycastSelector.this.ignoreEmptyCollisionMaterial && var4.getMaterial() == BlockMaterial.Empty) {
                  class="kw">return false;
               }
            }

            class="kw">return var8 == null || var8.contains(var2x);
         }, var5.x, var5.y, var5.z, var7.x, var7.y, var7.z, RaycastSelector.this.distance);
         Vector3d var12 = new Vector3d(
            var5.x + var7.x * RaycastSelector.this.distance * 0.5,
            var5.y + var7.y * RaycastSelector.this.distance * 0.5,
            var5.z + var7.z * RaycastSelector.this.distance * 0.5
         );
         Selector.selectNearbyEntities(var1, var12, RaycastSelector.this.distance * 0.6, var4x -> {
            BoundingBox var5 = var1.getComponent(var4x, BoundingBox.getComponentType());
            if (var5 != null) {
               TransformComponent var6 = var1.getComponent(var4x, TransformComponent.getComponentType());
               Vector3d var7 = var6.getPosition();
               if (CollisionMath.intersectRayAABB(var5, var7, var7.x(), var7.y(), var7.z(), var5.getBoundingBox(), this.minMax)) {
                  double var8 = var5.x + var7.x * this.minMax.x;
                  double var10 = var5.y + var7.y * this.minMax.x;
                  double var12 = var5.z + var7.z * this.minMax.x;
                  double var14 = var5.distanceSquared(var8, var10, var12);
                  if (!(var14 >= this.bestMatch.distance)) {
                     this.bestMatch.match = var4x;
                     this.bestMatch.distance = var14;
                     this.bestMatch.hitPosition.set(var8, var10, var12, 0.0);
                  }
               }
            }
         }, var1x -> !var1x.equals(var2));
         if (this.bestMatch.match != null && this.blockPosition != null) {
            double var13 = var5.distanceSquared(this.blockPosition.x + 0.5, this.blockPosition.y + 0.5, this.blockPosition.z + 0.5);
            if (!(var13 < this.bestMatch.distance)) {
               this.blockPosition = null;
            }
         }
      }

      @Override
      class="kw">public void selectTargetEntities(
         @Nonnull CommandBuffer<EntityStore> var1,
         @Nonnull Ref<EntityStore> var2,
         @Nonnull BiConsumer<Ref<EntityStore>, Vector4d> var3,
         Predicate<Ref<EntityStore>> var4
      ) {
         if (this.blockPosition == null && this.bestMatch.match != null) {
            if (this.bestMatch.match.isValid()) {
               var3.accept(this.bestMatch.match, this.bestMatch.hitPosition);
            }
         }
      }

      @Override
      class="kw">public void selectTargetBlocks(@Nonnull CommandBuffer<EntityStore> var1, @Nonnull Ref<EntityStore> var2, @Nonnull Selector.BlockConsumer var3) {
         if (this.blockPosition != null) {
            CachedAccessor var4 = SelectorBlockScan.accessorAt(
               var1.getExternalData().getWorld(), this.blockPosition.x, this.blockPosition.y, this.blockPosition.z, 1.0
            );
            SelectorBlockScan.accept(var4, this.blockPosition.x, this.blockPosition.y, this.blockPosition.z, var3);
         }
      }
   }
}