ItemCondition class

Пакет: com.hypixel.hytale.builtin.triggervolumes.effect.builtin

Файл: com/hypixel/hytale/builtin/triggervolumes/effect/builtin/ItemCondition.java

extends: TriggerCondition

Поля (26)

МодификаторыТипИмя
IN_HAND, HOTBAR, INVENTORY, CARRIED
break
break
break
Store var2
int var2
BsonDocument var2
Ref var3
Store var3
CarriedBlock var3
InventoryComponent.Tool var3
byte var3
BsonValue var3
ItemCondition var4
int var4
Ref var4
ItemStack var4
InventoryComponent.Hotbar var4
ItemStack var4
String var4
int var5
InventoryComponent.Hotbar var5
int var5
InventoryComponent.Hotbar var6
ItemStackSlotTransaction var6
CombinedItemContainer var7

Методы (18)

МодификаторыВозвратСигнатура
Location Location()
private void consumeFromActiveInventoryvoid consumeFromActiveInventory(@Nonnull TriggerContext var1, int var2)
private boolean consumeFromActiveSlotboolean consumeFromActiveSlot(@Nonnull ActiveSlotInventoryComponent var1, int var2)
private void consumeFromContainervoid consumeFromContainer(@Nonnull ItemContainer var1, int var2)
private void consumeMatchedItemsvoid consumeMatchedItems(@Nonnull TriggerContext var1, int var2)
private int countStackint countStack(@Nullable ItemStack var1)
private int countStacksint countStacks(@Nonnull ItemContainer var1)
if if(this.emptyInventory)
if if(var2 != 0)
if if(var5 != null)
if if(var4 != null)
if if(var3 == -1)
if if(var2 == null)
if if(var3 == null)
private boolean matchesboolean matches(int var1, int var2)
private boolean matchesMetadataboolean matchesMetadata(@Nonnull ItemStack var1)
return switchreturn switch(this.comparison)
switch switch(this.location != null ? this.location : ItemCondition.Location.IN_HAND)

Исходный код

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

class="kw">import com.hypixel.hytale.builtin.triggervolumes.effect.TriggerCondition;
class="kw">import com.hypixel.hytale.builtin.triggervolumes.effect.TriggerContext;
class="kw">import com.hypixel.hytale.builtin.triggervolumes.effect.TriggerEventType;
class="kw">import com.hypixel.hytale.builtin.triggervolumes.effect.builtin.conditions.PlayerCountCondition;
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.component.Ref;
class="kw">import com.hypixel.hytale.component.Store;
class="kw">import com.hypixel.hytale.server.core.entity.entities.Player;
class="kw">import com.hypixel.hytale.server.core.inventory.ActiveSlotInventoryComponent;
class="kw">import com.hypixel.hytale.server.core.inventory.InventoryComponent;
class="kw">import com.hypixel.hytale.server.core.inventory.ItemStack;
class="kw">import com.hypixel.hytale.server.core.inventory.container.CombinedItemContainer;
class="kw">import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
class="kw">import com.hypixel.hytale.server.core.inventory.transaction.ItemStackSlotTransaction;
class="kw">import com.hypixel.hytale.server.core.modules.interaction.components.CarriedBlock;
class="kw">import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;
class="kw">import org.bson.BsonDocument;
class="kw">import org.bson.BsonString;
class="kw">import org.bson.BsonValue;

class="kw">public class ItemCondition class="kw">extends TriggerCondition {
   @Nonnull
   class="kw">public class="kw">static class="kw">final BuilderCodec<ItemCondition> CODEC = BuilderCodec.builder(ItemCondition.class, ItemCondition::new, BASE_CODEC)
      .append(new KeyedCodec<>("Item", Codec.STRING), (var0, var1) -> var0.itemId = var1, var0 -> var0.itemId)
      .add()
      .append(new KeyedCodec<>("Location", new EnumCodec<>(ItemCondition.Location.class), false), (var0, var1) -> var0.location = var1, var0 -> var0.location)
      .add()
      .append(new KeyedCodec<>("Quantity", Codec.INTEGER, false), (var0, var1) -> var0.quantity = var1, var0 -> var0.quantity)
      .add()
      .append(new KeyedCodec<>("Consume", Codec.BOOLEAN, false), (var0, var1) -> var0.consume = var1, var0 -> var0.consume)
      .add()
      .append(
         new KeyedCodec<>("Comparison", new EnumCodec<>(PlayerCountCondition.Comparison.class), false),
         (var0, var1) -> var0.comparison = var1,
         var0 -> var0.comparison
      )
      .add()
      .append(new KeyedCodec<>("EmptyInventory", Codec.BOOLEAN, false), (var0, var1) -> var0.emptyInventory = var1, var0 -> var0.emptyInventory)
      .add()
      .append(new KeyedCodec<>("MetadataKey", Codec.STRING, false), (var0, var1) -> var0.metadataKey = var1, var0 -> var0.metadataKey)
      .add()
      .append(new KeyedCodec<>("MetadataValue", Codec.STRING, false), (var0, var1) -> var0.metadataValue = var1, var0 -> var0.metadataValue)
      .add()
      .build();
   @Nullable
   class="kw">private String itemId;
   @Nonnull
   class="kw">private ItemCondition.Location location = ItemCondition.Location.IN_HAND;
   class="kw">private int quantity = 1;
   class="kw">private boolean consume;
   @Nonnull
   class="kw">private PlayerCountCondition.Comparison comparison = PlayerCountCondition.Comparison.AT_LEAST;
   class="kw">private boolean emptyInventory;
   @Nonnull
   class="kw">private String metadataKey = "";
   @Nonnull
   class="kw">private String metadataValue = "";

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

   @Nonnull
   class="kw">public class="kw">static ItemCondition create(@Nonnull TriggerEventType var0, @Nonnull String var1, @Nonnull ItemCondition.Location var2, int var3) {
      ItemCondition var4 = new ItemCondition();
      var4.setEventType(var0);
      var4.itemId = var1;
      var4.location = var2;
      var4.quantity = var3;
      class="kw">return var4;
   }

   @Override
   class="kw">public boolean test(@Nonnull TriggerContext var1) {
      Store var2 = var1.getStore();
      Ref var3 = var1.getEntityRef();
      if (var2.getComponent(var3, Player.getComponentType()) == null) {
         class="kw">return false;
      }

      if (this.emptyInventory) {
         CombinedItemContainer var7 = InventoryComponent.getCombined(var2, var3, InventoryComponent.EVERYTHING);
         class="kw">return var7.countItemStacks(var0 -> !ItemStack.isEmpty(var0)) == 0;
      }

      if (this.itemId != null && !this.itemId.isBlank()) {
         int var4 = Math.max(0, this.quantity);

         int var5 = class="kw">switch (this.location != null ? this.location : ItemCondition.Location.IN_HAND) {
            case IN_HAND -> this.countStack(InventoryComponent.getItemInHand(var2, var3));
            case HOTBAR -> {
               InventoryComponent.Hotbar var6 = var2.getComponent(var3, InventoryComponent.Hotbar.getComponentType());
               yield var6 != null ? this.countStacks(var6.getInventory()) : 0;
            }
            case INVENTORY -> this.countStacks(InventoryComponent.getCombined(var2, var3, InventoryComponent.EVERYTHING));
            case CARRIED -> this.countStack(this.getCarriedItemStack(var2, var3));
         };
         class="kw">return this.matches(var5, var4);
      } else {
         class="kw">return true;
      }
   }

   class="kw">private boolean matches(int var1, int var2) {
      class="kw">return class="kw">switch (this.comparison) {
         case AT_LEAST -> var1 >= var2;
         case AT_MOST -> var1 <= var2;
         case EXACTLY -> var1 == var2;
         case NOT_EQUALS -> var1 != var2;
         case MORE_THAN -> var1 > var2;
         case LESS_THAN -> var1 < var2;
      };
   }

   @Override
   class="kw">public void applyOnAccept(@Nonnull TriggerContext var1) {
      if (this.consume && !this.emptyInventory && this.itemId != null && !this.itemId.isBlank()) {
         int var2 = Math.max(0, this.quantity);
         if (var2 != 0) {
            this.consumeMatchedItems(var1, var2);
         }
      }
   }

   class="kw">private void consumeMatchedItems(@Nonnull TriggerContext var1, int var2) {
      class="kw">switch (this.location != null ? this.location : ItemCondition.Location.IN_HAND) {
         case IN_HAND:
            this.consumeFromActiveInventory(var1, var2);
            break;
         case HOTBAR:
            InventoryComponent.Hotbar var5 = var1.getStore().getComponent(var1.getEntityRef(), InventoryComponent.Hotbar.getComponentType());
            if (var5 != null) {
               this.consumeFromContainer(var5.getInventory(), var2);
            }
            break;
         case INVENTORY:
            this.consumeFromContainer(InventoryComponent.getCombined(var1.getStore(), var1.getEntityRef(), InventoryComponent.EVERYTHING), var2);
            break;
         case CARRIED:
            Store var3 = var1.getStore();
            Ref var4 = var1.getEntityRef();
            if (this.countStack(this.getCarriedItemStack(var3, var4)) > 0) {
               var3.removeComponent(var4, CarriedBlock.getComponentType());
            }
      }
   }

   @Nullable
   class="kw">private ItemStack getCarriedItemStack(@Nonnull Store<EntityStore> var1, @Nonnull Ref<EntityStore> var2) {
      CarriedBlock var3 = var1.getComponent(var2, CarriedBlock.getComponentType());
      class="kw">return var3 != null ? var3.toItemStack() : null;
   }

   class="kw">private void consumeFromContainer(@Nonnull ItemContainer var1, int var2) {
      for (short var3 = 0; var3 < var1.getCapacity() && var2 > 0; var3++) {
         ItemStack var4 = var1.getItemStack(var3);
         if (this.countStack(var4) != 0) {
            int var5 = Math.min(var2, var4.getQuantity());
            ItemStackSlotTransaction var6 = var1.removeItemStackFromSlot(var3, var4, var5);
            if (var6.succeeded() && !ItemStack.isEmpty(var6.getOutput())) {
               var2 -= var6.getOutput().getQuantity();
            }
         }
      }
   }

   class="kw">private void consumeFromActiveInventory(@Nonnull TriggerContext var1, int var2) {
      InventoryComponent.Tool var3 = var1.getStore().getComponent(var1.getEntityRef(), InventoryComponent.Tool.getComponentType());
      if (var3 == null || !var3.isUsingToolsItem() || !this.consumeFromActiveSlot(var3, var2)) {
         InventoryComponent.Hotbar var4 = var1.getStore().getComponent(var1.getEntityRef(), InventoryComponent.Hotbar.getComponentType());
         if (var4 != null) {
            this.consumeFromActiveSlot(var4, var2);
         }
      }
   }

   class="kw">private boolean consumeFromActiveSlot(@Nonnull ActiveSlotInventoryComponent var1, int var2) {
      byte var3 = var1.getActiveSlot();
      if (var3 == -1) {
         class="kw">return false;
      }

      ItemStack var4 = var1.getInventory().getItemStack(var3);
      if (this.countStack(var4) < var2) {
         class="kw">return false;
      }

      var1.getInventory().removeItemStackFromSlot(var3, var4, var2);
      class="kw">return true;
   }

   class="kw">private int countStacks(@Nonnull ItemContainer var1) {
      class="kw">return var1.countItemStacks(var1x -> this.itemId.equals(var1x.getItemId()) && this.matchesMetadata(var1x));
   }

   class="kw">private int countStack(@Nullable ItemStack var1) {
      class="kw">return !ItemStack.isEmpty(var1) && this.itemId.equals(var1.getItemId()) && this.matchesMetadata(var1) ? var1.getQuantity() : 0;
   }

   class="kw">private boolean matchesMetadata(@Nonnull ItemStack var1) {
      if (this.metadataKey.isBlank()) {
         class="kw">return true;
      }

      BsonDocument var2 = var1.getMetadata();
      if (var2 == null) {
         class="kw">return false;
      }

      BsonValue var3 = var2.get(this.metadataKey);
      if (var3 == null) {
         class="kw">return false;
      }

      if (this.metadataValue.isBlank()) {
         class="kw">return true;
      }

      String var4 = var3 class="kw">instanceof BsonString var5 ? var5.getValue() : var3.toString();
      class="kw">return this.metadataValue.equals(var4);
   }

   class="kw">public enum Location {
      IN_HAND,
      HOTBAR,
      INVENTORY,
      CARRIED;

      Location() {
      }
   }
}