Example #1
0
  @Override
  public void onInteract(Entity entity, Action type) {
    super.onInteract(entity, type);
    if (type == Action.RIGHT_CLICK && entity.has(HeadComponent.class)) {
      BlockIterator iterator = entity.get(HeadComponent.class).getBlockView();
      if (iterator == null || !iterator.hasNext()) {
        return;
      }
      Block block = iterator.next().translate(BlockFace.TOP);
      if (this.canPlace(block, (short) 0)) {
        Cause<Entity> cause;
        if (entity instanceof Player) {
          cause = new PlayerBreakCause((Player) entity, block);
        } else {
          cause = new EntityCause(entity);
        }
        this.onPlacement(block, (short) 0, cause);

        // TODO Subtract from inventory component
        // Subtract item
        if (!entity.getData().get(VanillaData.GAMEMODE).equals(GameMode.SURVIVAL)) {
          return;
        }
        PlayerQuickbar inv = entity.get(PlayerInventory.class).getQuickbar();
        if (inv != null) {
          inv.addAmount(inv.getCurrentSlot(), -1);
        }
      }
    }
  }
Example #2
0
  public int getDamageBonus(Entity damaged, ItemStack heldItem) {
    // These enchantments conflict with each other, so only one is possible per item
    int damage = 0;
    if (Enchantment.hasEnchantment(heldItem, Enchantments.BANE_OF_ARTHROPODS)) {
      if (damaged.has(Spider.class) || damaged.has(Silverfish.class)) {
        damage =
            rand.nextInt(
                Enchantment.getEnchantmentLevel(heldItem, Enchantments.BANE_OF_ARTHROPODS) * 4);
      }
    } else if (Enchantment.hasEnchantment(heldItem, Enchantments.SHARPNESS)) {
      damage = rand.nextInt(Enchantment.getEnchantmentLevel(heldItem, Enchantments.SHARPNESS) * 3);
    } else if (Enchantment.hasEnchantment(heldItem, Enchantments.SMITE)) {
      if (damaged.has(Skeleton.class) || damaged.has(Zombie.class)) {
        damage = rand.nextInt(Enchantment.getEnchantmentLevel(heldItem, Enchantments.SMITE) * 4);
      }
    }

    // These enchantments must give at least one health point of extra damage
    if (damage == 0) {
      damage = 1;
    }
    return damage;
  }