aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/tools/Inventory.java
blob: 8f1e2f52fa6a1fe36eed500cbf4ebb747d630207 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ftbsc.bscv.tools;

import ftbsc.bscv.ICommons;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;

import java.util.Collection;
import java.util.List;

public class Inventory implements ICommons {

   public static final int HOTBAR_SIZE = 9;

   public static List<Slot> hotbar(ClientPlayerEntity player) {
      return player.inventoryMenu.slots.subList(36, 45);
   }

   // TODO this is hideous, there must be a proper way??????
   private static String enchId(Enchantment ench) {
      return ench.getDescriptionId().replace("enchantment.", "").replace(".", ":");
   }

   public static int getEnchLevel(ItemStack item, Enchantment enchant) {
      for (INBT tag : item.getEnchantmentTags()) {
         if (tag instanceof CompoundNBT) {
            CompoundNBT compound = (CompoundNBT) tag;
            String ench_id = compound.getString("id");
            if (Inventory.enchId(enchant).equals(ench_id)) {
               return compound.getInt("lvl");
            }
         }
      }
      return 0;
   }

   public static double itemAttackSpeed(ItemStack item) {
      Collection<AttributeModifier> speed_attrs =
         item.getAttributeModifiers(EquipmentSlotType.MAINHAND)
            .get(Attributes.ATTACK_SPEED);
      if (speed_attrs.isEmpty()) return 0.;
      return Math.abs(speed_attrs.iterator().next().getAmount());
   }

   public static double itemAttackDamage(ItemStack item) {
      Collection<AttributeModifier> damage_attrs =
         item.getAttributeModifiers(EquipmentSlotType.MAINHAND)
            .get(Attributes.ATTACK_DAMAGE);
      if (damage_attrs.isEmpty()) return 0.;
      return Math.abs(damage_attrs.iterator().next().getAmount());
   }

   public static double itemDPS(ItemStack item) {
      double damage = Inventory.itemAttackDamage(item);
      double speed  = Inventory.itemAttackSpeed(item);

      int sharpness = getEnchLevel(item, Enchantments.SHARPNESS);
      if (sharpness > 0) {
         damage += 0.5 * Math.max(0, sharpness - 1) + 1.;
      }

      return damage / (1. + speed);
   }
}