CoopResident class

Пакет: com.hypixel.hytale.builtin.adventure.farming.states

Файл: com/hypixel/hytale/builtin/adventure/farming/states/CoopBlock.java

Поля (5)

МодификаторыТипИмя
public static final BuilderCodec<CoopBlock.CoopResident> CODEC
protected boolean deployedToWorld
protected Instant lastProduced
protected CapturedNPCMetadata metadata
protected PersistentRef persistentRef

Методы (7)

МодификаторыВозвратСигнатура
public boolean getDeployedToWorldpublic boolean getDeployedToWorld()
public Instant getLastProducedpublic Instant getLastProduced()
public CapturedNPCMetadata getMetadatapublic CapturedNPCMetadata getMetadata()
public PersistentRef getPersistentRefpublic PersistentRef getPersistentRef()
public void setDeployedToWorldpublic void setDeployedToWorld(boolean var1)
public void setLastProducedpublic void setLastProduced(Instant var1)
public void setPersistentRefpublic void setPersistentRef(@Nullable PersistentRef var1)

Исходный код

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

class="kw">import com.hypixel.hytale.builtin.adventure.farming.FarmingPlugin;
class="kw">import com.hypixel.hytale.builtin.adventure.farming.FarmingUtil;
class="kw">import com.hypixel.hytale.builtin.adventure.farming.component.CoopResidentComponent;
class="kw">import com.hypixel.hytale.builtin.adventure.farming.config.FarmingCoopAsset;
class="kw">import com.hypixel.hytale.builtin.tagset.TagSetPlugin;
class="kw">import com.hypixel.hytale.builtin.tagset.config.NPCGroup;
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.array.ArrayCodec;
class="kw">import com.hypixel.hytale.component.AddReason;
class="kw">import com.hypixel.hytale.component.Component;
class="kw">import com.hypixel.hytale.component.ComponentType;
class="kw">import com.hypixel.hytale.component.Holder;
class="kw">import com.hypixel.hytale.component.Ref;
class="kw">import com.hypixel.hytale.component.Store;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.math.range.IntRange;
class="kw">import com.hypixel.hytale.math.util.MathUtil;
class="kw">import com.hypixel.hytale.math.vector.Rotation3f;
class="kw">import com.hypixel.hytale.math.vector.Vector3dUtil;
class="kw">import com.hypixel.hytale.math.vector.Vector3iUtil;
class="kw">import com.hypixel.hytale.server.core.asset.type.item.config.ItemDrop;
class="kw">import com.hypixel.hytale.server.core.asset.type.item.config.ItemDropList;
class="kw">import com.hypixel.hytale.server.core.entity.UUIDComponent;
class="kw">import com.hypixel.hytale.server.core.entity.reference.PersistentRef;
class="kw">import com.hypixel.hytale.server.core.inventory.ItemStack;
class="kw">import com.hypixel.hytale.server.core.inventory.container.EmptyItemContainer;
class="kw">import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
class="kw">import com.hypixel.hytale.server.core.inventory.container.SimpleItemContainer;
class="kw">import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent;
class="kw">import com.hypixel.hytale.server.core.modules.entity.item.ItemComponent;
class="kw">import com.hypixel.hytale.server.core.modules.time.WorldTimeResource;
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.npc.NPCPlugin;
class="kw">import com.hypixel.hytale.server.npc.asset.builder.Builder;
class="kw">import com.hypixel.hytale.server.npc.entities.NPCEntity;
class="kw">import com.hypixel.hytale.server.npc.metadata.CapturedNPCMetadata;
class="kw">import com.hypixel.hytale.server.npc.role.Role;
class="kw">import com.hypixel.hytale.server.spawning.ISpawnableWithModel;
class="kw">import com.hypixel.hytale.server.spawning.SpawnTestResult;
class="kw">import com.hypixel.hytale.server.spawning.SpawningContext;
class="kw">import it.unimi.dsi.fastutil.Pair;
class="kw">import it.unimi.dsi.fastutil.objects.ObjectArrayList;
class="kw">import it.unimi.dsi.fastutil.objects.ObjectListIterator;
class="kw">import java.time.Duration;
class="kw">import java.time.Instant;
class="kw">import java.time.LocalDateTime;
class="kw">import java.time.temporal.ChronoUnit;
class="kw">import java.util.ArrayList;
class="kw">import java.util.Arrays;
class="kw">import java.util.List;
class="kw">import java.util.Map;
class="kw">import java.util.UUID;
class="kw">import java.util.concurrent.ThreadLocalRandom;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;
class="kw">import org.joml.Vector3d;
class="kw">import org.joml.Vector3i;

class="kw">public class CoopBlock class="kw">implements Component<ChunkStore> {
   @Nonnull
   class="kw">public class="kw">static class="kw">final String STATE_PRODUCE = "Produce_Ready";
   @Nonnull
   class="kw">public class="kw">static class="kw">final BuilderCodec<CoopBlock> CODEC = BuilderCodec.builder(CoopBlock.class, CoopBlock::new)
      .append(new KeyedCodec<>("FarmingCoopId", Codec.STRING, true), (var0, var1) -> var0.coopAssetId = var1, var0 -> var0.coopAssetId)
      .add()
      .append(
         new KeyedCodec<>("Residents", new ArrayCodec<>(CoopBlock.CoopResident.CODEC, CoopBlock.CoopResident[]::new)),
         (var0, var1) -> var0.residents = new ArrayList<>(Arrays.asList(var1)),
         var0 -> var0.residents.toArray(CoopBlock.CoopResident[]::new)
      )
      .add()
      .append(new KeyedCodec<>("Storage", ItemContainer.CODEC), (var0, var1) -> var0.itemContainer = var1, var0 -> var0.itemContainer)
      .add()
      .build();
   class="kw">protected String coopAssetId;
   @Nonnull
   class="kw">protected List<CoopBlock.CoopResident> residents = new ObjectArrayList();
   @Nonnull
   class="kw">protected ItemContainer itemContainer = EmptyItemContainer.INSTANCE;

   class="kw">public class="kw">static ComponentType<ChunkStore, CoopBlock> getComponentType() {
      class="kw">return FarmingPlugin.get().getCoopBlockStateComponentType();
   }

   class="kw">public CoopBlock() {
      List var1 = new ObjectArrayList();
      this.itemContainer = ItemContainer.ensureContainerCapacity(this.itemContainer, (short)5, SimpleItemContainer::new, var1);
   }

   @Nullable
   class="kw">public FarmingCoopAsset getCoopAsset() {
      class="kw">return FarmingCoopAsset.getAssetMap().getAsset(this.coopAssetId);
   }

   class="kw">public CoopBlock(@Nonnull String var1, @Nonnull List<CoopBlock.CoopResident> var2, @Nonnull ItemContainer var3) {
      this.coopAssetId = var1;
      this.residents.addAll(var2);
      this.itemContainer = var3.clone();
      List var4 = new ObjectArrayList();
      this.itemContainer = ItemContainer.ensureContainerCapacity(this.itemContainer, (short)5, SimpleItemContainer::new, var4);
   }

   class="kw">public boolean tryPutResident(@Nonnull CapturedNPCMetadata var1, @Nonnull WorldTimeResource var2) {
      FarmingCoopAsset var3 = this.getCoopAsset();
      if (var3 == null) {
         class="kw">return false;
      }

      if (this.residents.size() >= var3.getMaxResidents()) {
         class="kw">return false;
      }

      if (!this.getCoopAcceptsNPC(var1.getNpcNameKey())) {
         class="kw">return false;
      }

      this.residents.add(new CoopBlock.CoopResident(var1, null, var2.getGameTime()));
      class="kw">return true;
   }

   class="kw">public boolean tryPutWildResidentFromWild(
      @Nonnull Store<EntityStore> var1, @Nonnull Ref<EntityStore> var2, @Nonnull WorldTimeResource var3, @Nonnull Vector3i var4
   ) {
      FarmingCoopAsset var5 = this.getCoopAsset();
      if (var5 == null) {
         class="kw">return false;
      }

      NPCEntity var6 = var1.getComponent(var2, NPCEntity.getComponentType());
      if (var6 == null) {
         class="kw">return false;
      }

      CoopResidentComponent var7 = var1.getComponent(var2, CoopResidentComponent.getComponentType());
      if (var7 != null) {
         class="kw">return false;
      }

      if (!this.getCoopAcceptsNPC(var6.getRoleName())) {
         class="kw">return false;
      }

      if (this.residents.size() >= var5.getMaxResidents()) {
         class="kw">return false;
      }

      var7 = var1.ensureAndGetComponent(var2, CoopResidentComponent.getComponentType());
      var7.setCoopLocation(var4);
      UUIDComponent var8 = var1.getComponent(var2, UUIDComponent.getComponentType());
      if (var8 == null) {
         class="kw">return false;
      }

      PersistentRef var9 = new PersistentRef();
      var9.setEntity(var2, var8.getUuid());
      CapturedNPCMetadata var10 = FarmingUtil.generateCapturedNPCMetadata(var1, var2, var6.getRoleName());
      CoopBlock.CoopResident var11 = new CoopBlock.CoopResident(var10, var9, var3.getGameTime());
      var11.deployedToWorld = true;
      this.residents.add(var11);
      class="kw">return true;
   }

   class="kw">public boolean getCoopAcceptsNPC(String var1) {
      TagSetPlugin.TagSetLookup var2 = TagSetPlugin.get(NPCGroup.class);
      int var3 = NPCPlugin.get().getIndex(var1);
      FarmingCoopAsset var4 = this.getCoopAsset();
      if (var4 == null) {
         class="kw">return false;
      }

      int[] var5 = var4.getAcceptedNpcGroupIndexes();
      if (var5 == null) {
         class="kw">return true;
      }

      for (int var9 : var5) {
         if (var2.tagInSet(var9, var3)) {
            class="kw">return true;
         }
      }

      class="kw">return false;
   }

   class="kw">public boolean generateProduceToInventory(@Nonnull WorldTimeResource var1) {
      Instant var2 = var1.getGameTime();
      FarmingCoopAsset var3 = this.getCoopAsset();
      if (var3 == null) {
         class="kw">return false;
      }

      Map var4 = var3.getProduceDrops();
      if (var4.isEmpty()) {
         class="kw">return false;
      }

      ThreadLocalRandom var5 = ThreadLocalRandom.current();
      boolean var6 = false;
      List var7 = new ArrayList<>();

      for (CoopBlock.CoopResident var9 : this.residents) {
         Instant var10 = var9.getLastProduced();
         if (var10 == null) {
            var9.setLastProduced(var2);
            var6 = true;
         } else {
            CapturedNPCMetadata var11 = var9.getMetadata();
            String var12 = var11.getNpcNameKey();
            String var13 = var4.get(var12);
            if (var13 != null) {
               ItemDropList var14 = ItemDropList.getAssetMap().getAsset(var13);
               if (var14 != null) {
                  Duration var15 = Duration.between(var10, var2);
                  long var16 = var15.toHours();
                  int var18 = MathUtil.ceil((float)var16 / WorldTimeResource.HOURS_PER_DAY);
                  List var19 = new ArrayList<>();

                  for (int var20 = 0; var20 < var18; var20++) {
                     var14.getContainer().populateDrops(var19, var5::nextDouble, var13);

                     for (ItemDrop var22 : var19) {
                        if (var22 != null && var22.getItemId() != null) {
                           int var23 = var22.getRandomQuantity(var5);
                           if (var23 > 0) {
                              var7.add(new ItemStack(var22.getItemId(), var23, var22.getMetadata()));
                           }
                        } else {
                           ((HytaleLogger.Api)HytaleLogger.forEnclosingClass().atWarning())
                              .log("Tried to create ItemDrop for non-existent item in drop list id '%s'", var13);
                        }
                     }

                     var19.clear();
                  }

                  if (!var2.equals(var10)) {
                     var9.setLastProduced(var2);
                     var6 = true;
                  }
               }
            }
         }
      }

      if (!var7.isEmpty()) {
         this.itemContainer.addItemStacks(var7);
         var6 = true;
      }

      class="kw">return var6;
   }

   class="kw">public void gatherProduceFromContainer(@Nonnull ItemContainer var1) {
      for (ItemStack var3 : this.itemContainer.removeAllItemStacks()) {
         var1.addItemStack(var3);
      }
   }

   class="kw">public boolean ensureSpawnResidentsInWorld(@Nonnull World var1, @Nonnull Store<EntityStore> var2, @Nonnull Vector3d var3, @Nonnull Vector3d var4) {
      FarmingCoopAsset var5 = this.getCoopAsset();
      if (var5 == null) {
         class="kw">return false;
      }

      float var6 = (float) (Math.PI * 2) / var5.getMaxResidents();
      Vector3d var7 = var4;
      boolean var8 = false;
      SpawningContext var9 = new SpawningContext();

      for (CoopBlock.CoopResident var11 : this.residents) {
         CapturedNPCMetadata var12 = var11.getMetadata();
         String var13 = var12.getNpcNameKey();
         int var14 = NPCPlugin.get().getIndex(var13);
         boolean var15 = var11.getDeployedToWorld();
         PersistentRef var16 = var11.getPersistentRef();
         if (!var15 && var16 == null) {
            Vector3d var17 = new Vector3d().set(var3).add(var7);
            Builder var18 = NPCPlugin.get().tryGetCachedValidRole(var14);
            if (var18 != null) {
               var9.setSpawnable((ISpawnableWithModel)var18);
               if (var9.set(var1, var17.x, var17.y, var17.z) && var9.canSpawn() == SpawnTestResult.TEST_OK) {
                  Pair var19 = NPCPlugin.get().spawnEntity(var2, var14, var9.newPosition(), Rotation3f.IDENTITY, null, null);
                  if (var19 == null) {
                     var11.setPersistentRef(null);
                     var11.setDeployedToWorld(false);
                  } else {
                     Ref var20 = (Ref<EntityStore>)var19.first();
                     NPCEntity var21 = (NPCEntity)var19.second();
                     var21.getLeashPoint().set(var3);
                     if (var20 != null && var20.isValid()) {
                        UUIDComponent var22 = var2.getComponent(var20, UUIDComponent.getComponentType());
                        if (var22 == null) {
                           var11.setPersistentRef(null);
                           var11.setDeployedToWorld(false);
                        } else {
                           CoopResidentComponent var23 = new CoopResidentComponent();
                           var23.setCoopLocation(Vector3dUtil.toVector3i(var3));
                           var2.addComponent(var20, CoopResidentComponent.getComponentType(), var23);
                           PersistentRef var24 = new PersistentRef();
                           var24.setEntity(var20, var22.getUuid());
                           var11.setPersistentRef(var24);
                           var11.setDeployedToWorld(true);
                           var8 = true;
                           var7 = var7.rotateY(var6);
                        }
                     } else {
                        var11.setPersistentRef(null);
                        var11.setDeployedToWorld(false);
                     }
                  }
               }
            }
         }
      }

      class="kw">return var8;
   }

   class="kw">public boolean ensureNoResidentsInWorld(@Nonnull Store<EntityStore> var1) {
      FarmingCoopAsset var2 = this.getCoopAsset();
      if (var2 == null) {
         class="kw">return false;
      }

      boolean var3 = false;
      ObjectArrayList var4 = new ObjectArrayList();

      for (CoopBlock.CoopResident var6 : this.residents) {
         PersistentRef var7 = var6.getPersistentRef();
         if (var7 != null) {
            Ref var8 = var7.getEntity(var1);
            if (var8 == null) {
               var4.add(var6);
            } else {
               CoopResidentComponent var9 = var1.getComponent(var8, CoopResidentComponent.getComponentType());
               if (var9 == null) {
                  var4.add(var6);
               } else {
                  DeathComponent var10 = var1.getComponent(var8, DeathComponent.getComponentType());
                  if (var10 != null) {
                     var4.add(var6);
                  } else if (!this.getCoopAcceptsNPC(var6.metadata.getNpcNameKey())) {
                     var4.add(var6);
                  } else {
                     ComponentType var11 = NPCEntity.getComponentType();
                     if (var11 != null && var6.persistentRef != null) {
                        NPCEntity var12 = var1.getComponent(var8, var11);
                        if (var12 != null && !var6.getMetadata().getNpcNameKey().equals(var12.getRoleName())) {
                           CapturedNPCMetadata var13 = FarmingUtil.generateCapturedNPCMetadata(var1, var8, var12.getRoleName());
                           if (var13 != null) {
                              var6.metadata = var13;
                              var3 = true;
                           }
                        }
                     }

                     var9.setMarkedForDespawn(true);
                     var6.setPersistentRef(null);
                     var6.setDeployedToWorld(false);
                     var3 = true;
                  }
               }
            }
         }
      }

      for (ObjectListIterator var15 = var4.iterator(); var15.hasNext(); var3 = true) {
         CoopBlock.CoopResident var16 = (CoopBlock.CoopResident)var15.next();
         this.residents.remove(var16);
      }

      class="kw">return var3;
   }

   class="kw">public boolean shouldResidentsBeInCoop(@Nonnull WorldTimeResource var1) {
      FarmingCoopAsset var2 = this.getCoopAsset();
      if (var2 == null) {
         class="kw">return true;
      }

      IntRange var3 = var2.getResidentRoamTime();
      if (var3 == null) {
         class="kw">return true;
      }

      int var4 = var1.getCurrentHour();
      class="kw">return !var3.includes(var4);
   }

   @Nullable
   class="kw">public Instant getNextScheduledTick(@Nonnull WorldTimeResource var1) {
      Instant var2 = var1.getGameTime();
      LocalDateTime var3 = var1.getGameDateTime();
      int var4 = var1.getCurrentHour();
      int var5 = var3.getMinute();
      FarmingCoopAsset var6 = this.getCoopAsset();
      if (var6 == null) {
         class="kw">return null;
      }

      IntRange var7 = var6.getResidentRoamTime();
      if (var7 == null) {
         class="kw">return null;
      }

      int var8 = 0;
      int var9 = var7.getInclusiveMin();
      int var10 = var7.getInclusiveMax();
      if (var6.getResidentRoamTime().includes(var4)) {
         var8 = var6.getResidentRoamTime().getInclusiveMax() + 1 - var4;
      } else if (var4 > var10) {
         var8 = WorldTimeResource.HOURS_PER_DAY - var4 + var9;
      } else {
         var8 = var9 - var4;
      }

      class="kw">return var2.plus(var8 * 60L - var5, ChronoUnit.MINUTES);
   }

   class="kw">public void handleResidentDespawn(@Nonnull UUID var1) {
      CoopBlock.CoopResident var2 = null;

      for (CoopBlock.CoopResident var4 : this.residents) {
         if (var4.persistentRef != null && var1.equals(var4.persistentRef.getUuid())) {
            var2 = var4;
            break;
         }
      }

      if (var2 != null) {
         this.residents.remove(var2);
      }
   }

   class="kw">public void handleBlockBroken(@Nonnull World var1, @Nonnull WorldTimeResource var2, @Nonnull Store<EntityStore> var3, int var4, int var5, int var6) {
      Vector3i var7 = new Vector3i(var4, var5, var6);
      var1.execute(() -> this.ensureSpawnResidentsInWorld(var1, var3, Vector3iUtil.toVector3d(var7), new Vector3d().set(Vector3dUtil.FORWARD)));
      this.generateProduceToInventory(var2);
      Vector3d var8 = new Vector3d(var4 + 0.5F, var5, var6 + 0.5F);
      Holder[] var9 = ItemComponent.generateItemDrops(var3, this.itemContainer.removeAllItemStacks(), var8, Rotation3f.IDENTITY);
      if (var9.length > 0) {
         var1.execute(() -> var3.addEntities(var9, AddReason.SPAWN));
      }

      var1.execute(() -> {
         for (CoopBlock.CoopResident var3 : this.residents) {
            PersistentRef var4 = var3.getPersistentRef();
            if (var4 != null) {
               Ref var5 = var4.getEntity(var3);
               if (var5 == null) {
                  class="kw">return;
               }

               var3.tryRemoveComponent(var5, CoopResidentComponent.getComponentType());
            }
         }
      });
   }

   class="kw">public boolean hasProduce() {
      class="kw">return !this.itemContainer.isEmpty();
   }

   @Override
   class="kw">public Component<ChunkStore> clone() {
      class="kw">return new CoopBlock(this.coopAssetId, this.residents, this.itemContainer);
   }

   class="kw">public class="kw">static class CoopResident {
      @Nonnull
      class="kw">public class="kw">static class="kw">final BuilderCodec<CoopBlock.CoopResident> CODEC = BuilderCodec.builder(CoopBlock.CoopResident.class, CoopBlock.CoopResident::new)
         .append(new KeyedCodec<>("Metadata", CapturedNPCMetadata.CODEC), (var0, var1) -> var0.metadata = var1, var0 -> var0.metadata)
         .add()
         .append(new KeyedCodec<>("PersistentRef", PersistentRef.CODEC), (var0, var1) -> var0.persistentRef = var1, var0 -> var0.persistentRef)
         .add()
         .append(new KeyedCodec<>("DeployedToWorld", Codec.BOOLEAN), (var0, var1) -> var0.deployedToWorld = var1, var0 -> var0.deployedToWorld)
         .add()
         .append(new KeyedCodec<>("LastHarvested", Codec.INSTANT), (var0, var1) -> var0.lastProduced = var1, var0 -> var0.lastProduced)
         .add()
         .build();
      class="kw">protected CapturedNPCMetadata metadata;
      @Nullable
      class="kw">protected PersistentRef persistentRef;
      class="kw">protected boolean deployedToWorld;
      class="kw">protected Instant lastProduced;

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

      class="kw">public CoopResident(CapturedNPCMetadata var1, @Nullable PersistentRef var2, @Nonnull Instant var3) {
         this.metadata = var1;
         this.persistentRef = var2;
         this.lastProduced = var3;
      }

      class="kw">public CapturedNPCMetadata getMetadata() {
         class="kw">return this.metadata;
      }

      @Nullable
      class="kw">public PersistentRef getPersistentRef() {
         class="kw">return this.persistentRef;
      }

      class="kw">public void setPersistentRef(@Nullable PersistentRef var1) {
         this.persistentRef = var1;
      }

      class="kw">public boolean getDeployedToWorld() {
         class="kw">return this.deployedToWorld;
      }

      class="kw">public void setDeployedToWorld(boolean var1) {
         this.deployedToWorld = var1;
      }

      class="kw">public Instant getLastProduced() {
         class="kw">return this.lastProduced;
      }

      class="kw">public void setLastProduced(Instant var1) {
         this.lastProduced = var1;
      }
   }
}